xref: /aosp_15_r20/external/perfetto/src/base/test/test_task_runner.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
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 SRC_BASE_TEST_TEST_TASK_RUNNER_H_
18 #define SRC_BASE_TEST_TEST_TASK_RUNNER_H_
19 
20 #include <functional>
21 #include <list>
22 #include <map>
23 #include <string>
24 
25 #include "perfetto/base/build_config.h"
26 #include "perfetto/base/compiler.h"
27 #include "perfetto/ext/base/thread_checker.h"
28 #include "perfetto/ext/base/unix_task_runner.h"
29 
30 namespace perfetto {
31 namespace base {
32 
33 class TestTaskRunner : public TaskRunner {
34  public:
35   TestTaskRunner();
36   ~TestTaskRunner() override;
37 
38   void RunUntilIdle();
39   void PERFETTO_NORETURN Run();
40 
41   std::function<void()> CreateCheckpoint(const std::string& checkpoint);
42   void RunUntilCheckpoint(const std::string& checkpoint,
43                           uint32_t timeout_ms = 5000);
44 
45   // Pretends (for the purposes of running delayed tasks) that time advanced by
46   // `ms`. Run until then.
47   void AdvanceTimeAndRunUntilIdle(uint32_t ms);
48 
49   // TaskRunner implementation.
50   void PostTask(std::function<void()> closure) override;
51   void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override;
52   void AddFileDescriptorWatch(PlatformHandle,
53                               std::function<void()> callback) override;
54   void RemoveFileDescriptorWatch(PlatformHandle) override;
55   bool RunsTasksOnCurrentThread() const override;
56 
57  private:
58   TestTaskRunner(const TestTaskRunner&) = delete;
59   TestTaskRunner& operator=(const TestTaskRunner&) = delete;
60 
61   void QuitIfIdle();
62 
63   std::string pending_checkpoint_;
64   std::map<std::string, bool> checkpoints_;
65 
66   base::UnixTaskRunner task_runner_;
67   ThreadChecker thread_checker_;
68 };
69 
70 }  // namespace base
71 }  // namespace perfetto
72 
73 #endif  // SRC_BASE_TEST_TEST_TASK_RUNNER_H_
74