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 <sys/select.h> 21 22 #include <functional> 23 #include <list> 24 #include <map> 25 #include <string> 26 27 #include "perfetto/base/build_config.h" 28 #include "perfetto/base/thread_checker.h" 29 #include "perfetto/base/unix_task_runner.h" 30 31 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \ 32 !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD) 33 #include "perfetto/base/android_task_runner.h" 34 #endif 35 36 namespace perfetto { 37 namespace base { 38 39 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \ 40 !PERFETTO_BUILDFLAG(PERFETTO_EMBEDDER_BUILD) 41 using PlatformTaskRunner = AndroidTaskRunner; 42 #else 43 using PlatformTaskRunner = UnixTaskRunner; 44 #endif 45 46 class TestTaskRunner : public TaskRunner { 47 public: 48 TestTaskRunner(); 49 ~TestTaskRunner() override; 50 51 void RunUntilIdle(); 52 void __attribute__((__noreturn__)) Run(); 53 54 std::function<void()> CreateCheckpoint(const std::string& checkpoint); 55 void RunUntilCheckpoint(const std::string& checkpoint, 56 uint32_t timeout_ms = 5000); 57 58 // TaskRunner implementation. 59 void PostTask(std::function<void()> closure) override; 60 void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override; 61 void AddFileDescriptorWatch(int fd, std::function<void()> callback) override; 62 void RemoveFileDescriptorWatch(int fd) override; 63 bool RunsTasksOnCurrentThread() const override; 64 65 private: 66 TestTaskRunner(const TestTaskRunner&) = delete; 67 TestTaskRunner& operator=(const TestTaskRunner&) = delete; 68 69 void QuitIfIdle(); 70 71 std::string pending_checkpoint_; 72 std::map<std::string, bool> checkpoints_; 73 74 PlatformTaskRunner task_runner_; 75 ThreadChecker thread_checker_; 76 }; 77 78 } // namespace base 79 } // namespace perfetto 80 81 #endif // SRC_BASE_TEST_TEST_TASK_RUNNER_H_ 82