blob: ef7aa09c29af58a4c148d443ba6566a3af7360aa [file] [log] [blame]
Primiano Tuccice720022017-10-30 12:50:06 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Primiano Tuccid7d1be02017-10-30 17:41:34 +000017#include "base/test/test_task_runner.h"
Primiano Tuccice720022017-10-30 12:50:06 +000018
19#include <stdio.h>
20#include <unistd.h>
21
Primiano Tuccie73ac932017-11-08 18:11:17 +000022#include <chrono>
23
Primiano Tuccid7d1be02017-10-30 17:41:34 +000024#include "base/logging.h"
Primiano Tuccice720022017-10-30 12:50:06 +000025
Primiano Tuccice720022017-10-30 12:50:06 +000026namespace perfetto {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000027namespace base {
Primiano Tuccice720022017-10-30 12:50:06 +000028
Primiano Tuccid7d1be02017-10-30 17:41:34 +000029TestTaskRunner::TestTaskRunner() = default;
Primiano Tuccice720022017-10-30 12:50:06 +000030
Primiano Tucci8242ad42017-11-14 11:26:52 +000031TestTaskRunner::~TestTaskRunner() = default;
Primiano Tuccice720022017-10-30 12:50:06 +000032
33void TestTaskRunner::Run() {
Sami Kyostilaeaed9562017-11-22 12:48:10 +000034 PERFETTO_DCHECK_THREAD(thread_checker_);
Primiano Tuccie73ac932017-11-08 18:11:17 +000035 for (;;)
Sami Kyostilaeaed9562017-11-22 12:48:10 +000036 task_runner_.Run();
Primiano Tuccie73ac932017-11-08 18:11:17 +000037}
38
39void TestTaskRunner::RunUntilIdle() {
Sami Kyostilaeaed9562017-11-22 12:48:10 +000040 PERFETTO_DCHECK_THREAD(thread_checker_);
41 task_runner_.PostTask(std::bind(&TestTaskRunner::QuitIfIdle, this));
42 task_runner_.Run();
43}
44
45void TestTaskRunner::QuitIfIdle() {
46 PERFETTO_DCHECK_THREAD(thread_checker_);
47 if (task_runner_.IsIdleForTesting()) {
48 task_runner_.Quit();
49 return;
50 }
51 task_runner_.PostTask(std::bind(&TestTaskRunner::QuitIfIdle, this));
Primiano Tuccie73ac932017-11-08 18:11:17 +000052}
53
54void TestTaskRunner::RunUntilCheckpoint(const std::string& checkpoint,
55 int timeout_ms) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +000056 PERFETTO_DCHECK_THREAD(thread_checker_);
Primiano Tucci3052b1a2017-11-14 10:51:01 +000057 if (checkpoints_.count(checkpoint) == 0) {
58 fprintf(stderr, "[TestTaskRunner] Checkpoint \"%s\" does not exist.\n",
59 checkpoint.c_str());
60 abort();
61 }
Sami Kyostilaeaed9562017-11-22 12:48:10 +000062 if (checkpoints_[checkpoint])
63 return;
Primiano Tuccice720022017-10-30 12:50:06 +000064
Sami Kyostilaeaed9562017-11-22 12:48:10 +000065 task_runner_.PostDelayedTask(
66 [this, checkpoint] {
67 if (checkpoints_[checkpoint])
68 return;
69 fprintf(stderr, "[TestTaskRunner] Failed to reach checkpoint \"%s\"\n",
70 checkpoint.c_str());
71 abort();
72 },
73 timeout_ms);
74
75 pending_checkpoint_ = checkpoint;
76 task_runner_.Run();
Primiano Tuccice720022017-10-30 12:50:06 +000077}
78
Primiano Tuccie73ac932017-11-08 18:11:17 +000079std::function<void()> TestTaskRunner::CreateCheckpoint(
80 const std::string& checkpoint) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +000081 PERFETTO_DCHECK_THREAD(thread_checker_);
Primiano Tuccie73ac932017-11-08 18:11:17 +000082 PERFETTO_DCHECK(checkpoints_.count(checkpoint) == 0);
83 auto checkpoint_iter = checkpoints_.emplace(checkpoint, false);
Sami Kyostilaeaed9562017-11-22 12:48:10 +000084 return [this, checkpoint_iter] {
85 checkpoint_iter.first->second = true;
86 if (pending_checkpoint_ == checkpoint_iter.first->first) {
87 pending_checkpoint_.clear();
88 task_runner_.Quit();
Primiano Tuccie73ac932017-11-08 18:11:17 +000089 }
Sami Kyostilaeaed9562017-11-22 12:48:10 +000090 };
Primiano Tuccice720022017-10-30 12:50:06 +000091}
92
93// TaskRunner implementation.
94void TestTaskRunner::PostTask(std::function<void()> closure) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +000095 task_runner_.PostTask(std::move(closure));
Primiano Tuccice720022017-10-30 12:50:06 +000096}
97
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000098void TestTaskRunner::PostDelayedTask(std::function<void()> closure,
99 int delay_ms) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +0000100 task_runner_.PostDelayedTask(std::move(closure), delay_ms);
Sami Kyostila2c6c2f52017-11-21 16:08:16 +0000101}
102
Primiano Tuccice720022017-10-30 12:50:06 +0000103void TestTaskRunner::AddFileDescriptorWatch(int fd,
104 std::function<void()> callback) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +0000105 task_runner_.AddFileDescriptorWatch(fd, std::move(callback));
Primiano Tuccice720022017-10-30 12:50:06 +0000106}
107
108void TestTaskRunner::RemoveFileDescriptorWatch(int fd) {
Sami Kyostilaeaed9562017-11-22 12:48:10 +0000109 task_runner_.RemoveFileDescriptorWatch(fd);
Primiano Tuccice720022017-10-30 12:50:06 +0000110}
111
Primiano Tuccid7d1be02017-10-30 17:41:34 +0000112} // namespace base
Primiano Tuccice720022017-10-30 12:50:06 +0000113} // namespace perfetto