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 // TaskRunner implementation. 46 void PostTask(std::function<void()> closure) override; 47 void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override; 48 void AddFileDescriptorWatch(PlatformHandle, 49 std::function<void()> callback) override; 50 void RemoveFileDescriptorWatch(PlatformHandle) override; 51 bool RunsTasksOnCurrentThread() const override; 52 53 private: 54 TestTaskRunner(const TestTaskRunner&) = delete; 55 TestTaskRunner& operator=(const TestTaskRunner&) = delete; 56 57 void QuitIfIdle(); 58 59 std::string pending_checkpoint_; 60 std::map<std::string, bool> checkpoints_; 61 62 base::UnixTaskRunner task_runner_; 63 ThreadChecker thread_checker_; 64 }; 65 66 } // namespace base 67 } // namespace perfetto 68 69 #endif // SRC_BASE_TEST_TEST_TASK_RUNNER_H_ 70