Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef RTC_BASE_TASK_QUEUE_FOR_TEST_H_ |
| 12 | #define RTC_BASE_TASK_QUEUE_FOR_TEST_H_ |
| 13 | |
Danil Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 16 | #include "absl/cleanup/cleanup.h" |
Danil Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 18 | #include "api/function_view.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 08:04:57 | [diff] [blame] | 19 | #include "api/task_queue/task_queue_base.h" |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 20 | #include "api/task_queue/task_queue_factory.h" |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/event.h" |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 23 | |
Danil Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 26 | inline void SendTask(TaskQueueBase* task_queue, |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 27 | rtc::FunctionView<void()> task) { |
Danil Chapovalov | 2aaef45 | 2022-08-12 13:55:11 | [diff] [blame] | 28 | if (task_queue->IsCurrent()) { |
| 29 | task(); |
| 30 | return; |
| 31 | } |
| 32 | |
Danil Chapovalov | eb90e6f | 2019-10-15 08:04:57 | [diff] [blame] | 33 | rtc::Event event; |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 34 | absl::Cleanup cleanup = [&event] { event.Set(); }; |
| 35 | task_queue->PostTask([task, cleanup = std::move(cleanup)] { task(); }); |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 | [diff] [blame] | 36 | RTC_CHECK(event.Wait(/*give_up_after=*/rtc::Event::kForever, |
| 37 | /*warn_after=*/TimeDelta::Seconds(10))); |
Danil Chapovalov | eb90e6f | 2019-10-15 08:04:57 | [diff] [blame] | 38 | } |
| 39 | |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 40 | class TaskQueueForTest { |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 41 | public: |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 42 | explicit TaskQueueForTest( |
| 43 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue); |
| 44 | explicit TaskQueueForTest( |
| 45 | absl::string_view name = "TestQueue", |
| 46 | TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL); |
Danil Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 47 | TaskQueueForTest(const TaskQueueForTest&) = delete; |
| 48 | TaskQueueForTest& operator=(const TaskQueueForTest&) = delete; |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 49 | ~TaskQueueForTest(); |
| 50 | |
| 51 | bool IsCurrent() const { return impl_->IsCurrent(); } |
| 52 | |
| 53 | // Returns non-owning pointer to the task queue implementation. |
| 54 | TaskQueueBase* Get() { return impl_.get(); } |
| 55 | |
| 56 | void PostTask( |
| 57 | absl::AnyInvocable<void() &&> task, |
| 58 | const webrtc::Location& location = webrtc::Location::Current()) { |
| 59 | impl_->PostTask(std::move(task), location); |
| 60 | } |
| 61 | void PostDelayedTask( |
| 62 | absl::AnyInvocable<void() &&> task, |
| 63 | webrtc::TimeDelta delay, |
| 64 | const webrtc::Location& location = webrtc::Location::Current()) { |
| 65 | impl_->PostDelayedTask(std::move(task), delay, location); |
| 66 | } |
| 67 | void PostDelayedHighPrecisionTask( |
| 68 | absl::AnyInvocable<void() &&> task, |
| 69 | webrtc::TimeDelta delay, |
| 70 | const webrtc::Location& location = webrtc::Location::Current()) { |
| 71 | impl_->PostDelayedHighPrecisionTask(std::move(task), delay, location); |
| 72 | } |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 73 | |
| 74 | // A convenience, test-only method that blocks the current thread while |
| 75 | // a task executes on the task queue. |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 76 | void SendTask(rtc::FunctionView<void()> task) { |
| 77 | ::webrtc::SendTask(Get(), task); |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 78 | } |
Marina Ciocea | ce1320c | 2020-05-04 12:26:05 | [diff] [blame] | 79 | |
| 80 | // Wait for the completion of all tasks posted prior to the |
| 81 | // WaitForPreviouslyPostedTasks() call. |
| 82 | void WaitForPreviouslyPostedTasks() { |
Danil Chapovalov | 2aaef45 | 2022-08-12 13:55:11 | [diff] [blame] | 83 | RTC_DCHECK(!Get()->IsCurrent()); |
Marina Ciocea | ce1320c | 2020-05-04 12:26:05 | [diff] [blame] | 84 | // Post an empty task on the queue and wait for it to finish, to ensure |
| 85 | // that all already posted tasks on the queue get executed. |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 86 | SendTask([]() {}); |
Marina Ciocea | ce1320c | 2020-05-04 12:26:05 | [diff] [blame] | 87 | } |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> impl_; |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 91 | }; |
Danil Chapovalov | d26a916 | 2019-03-19 17:08:37 | [diff] [blame] | 92 | |
| 93 | } // namespace webrtc |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 94 | |
| 95 | #endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_ |