blob: 6c56594ead10646600ba2026040fd39eafa61fcd [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
17#ifndef TRACING_SRC_TEST_TEST_TASK_RUNNER_H_
18#define TRACING_SRC_TEST_TEST_TASK_RUNNER_H_
19
20#include <sys/select.h>
21
22#include <functional>
23#include <list>
24#include <map>
Primiano Tuccie73ac932017-11-08 18:11:17 +000025#include <string>
Primiano Tuccice720022017-10-30 12:50:06 +000026
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080027#include "perfetto_base/build_config.h"
28#include "perfetto_base/thread_checker.h"
29#include "perfetto_base/unix_task_runner.h"
Primiano Tuccice720022017-10-30 12:50:06 +000030
Sami Kyostila73d41c82017-11-24 18:38:46 +000031#if BUILDFLAG(OS_ANDROID)
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080032#include "perfetto_base/android_task_runner.h"
Sami Kyostila73d41c82017-11-24 18:38:46 +000033#endif
34
Primiano Tuccice720022017-10-30 12:50:06 +000035namespace perfetto {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000036namespace base {
Primiano Tuccice720022017-10-30 12:50:06 +000037
Sami Kyostila73d41c82017-11-24 18:38:46 +000038#if BUILDFLAG(OS_ANDROID)
39using PlatformTaskRunner = AndroidTaskRunner;
40#else
41using PlatformTaskRunner = UnixTaskRunner;
42#endif
43
Primiano Tuccice720022017-10-30 12:50:06 +000044class TestTaskRunner : public TaskRunner {
45 public:
46 TestTaskRunner();
47 ~TestTaskRunner() override;
48
Primiano Tuccie73ac932017-11-08 18:11:17 +000049 void RunUntilIdle();
50 void __attribute__((__noreturn__)) Run();
Primiano Tuccice720022017-10-30 12:50:06 +000051
Primiano Tuccie73ac932017-11-08 18:11:17 +000052 std::function<void()> CreateCheckpoint(const std::string& checkpoint);
53 void RunUntilCheckpoint(const std::string& checkpoint, int timeout_ms = 5000);
Primiano Tuccice720022017-10-30 12:50:06 +000054
55 // TaskRunner implementation.
56 void PostTask(std::function<void()> closure) override;
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000057 void PostDelayedTask(std::function<void()>, int delay_ms) override;
Primiano Tuccice720022017-10-30 12:50:06 +000058 void AddFileDescriptorWatch(int fd, std::function<void()> callback) override;
59 void RemoveFileDescriptorWatch(int fd) override;
60
61 private:
62 TestTaskRunner(const TestTaskRunner&) = delete;
63 TestTaskRunner& operator=(const TestTaskRunner&) = delete;
64
Sami Kyostilaeaed9562017-11-22 12:48:10 +000065 void QuitIfIdle();
Primiano Tuccice720022017-10-30 12:50:06 +000066
Sami Kyostilaeaed9562017-11-22 12:48:10 +000067 std::string pending_checkpoint_;
Primiano Tuccie73ac932017-11-08 18:11:17 +000068 std::map<std::string, bool> checkpoints_;
Sami Kyostilaeaed9562017-11-22 12:48:10 +000069
Sami Kyostila73d41c82017-11-24 18:38:46 +000070 PlatformTaskRunner task_runner_;
Sami Kyostilaeaed9562017-11-22 12:48:10 +000071 ThreadChecker thread_checker_;
Primiano Tuccice720022017-10-30 12:50:06 +000072};
73
Primiano Tuccid7d1be02017-10-30 17:41:34 +000074} // namespace base
Primiano Tuccice720022017-10-30 12:50:06 +000075} // namespace perfetto
76
77#endif // TRACING_SRC_TEST_TEST_TASK_RUNNER_H_