Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [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 18:08:37 +0100 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 16 | #include "absl/cleanup/cleanup.h" |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 18 | #include "api/function_view.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 19 | #include "api/task_queue/task_queue_base.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/event.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 22 | #include "rtc_base/location.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 23 | #include "rtc_base/task_queue.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 24 | #include "rtc_base/thread_annotations.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 25 | |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 26 | namespace webrtc { |
| 27 | |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 28 | inline void SendTask(rtc::Location loc, |
| 29 | TaskQueueBase* task_queue, |
| 30 | rtc::FunctionView<void()> task) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 31 | RTC_CHECK(!task_queue->IsCurrent()) |
| 32 | << "Called SendTask to a queue from the same queue at " << loc.ToString(); |
| 33 | rtc::Event event; |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 34 | absl::Cleanup cleanup = [&event] { event.Set(); }; |
| 35 | task_queue->PostTask([task, cleanup = std::move(cleanup)] { task(); }); |
Danil Chapovalov | 562a37f | 2019-10-16 19:39:09 +0200 | [diff] [blame] | 36 | RTC_CHECK(event.Wait(/*give_up_after_ms=*/rtc::Event::kForever, |
| 37 | /*warn_after_ms=*/10'000)) |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 38 | << "Waited too long at " << loc.ToString(); |
| 39 | } |
| 40 | |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 41 | class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue { |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 42 | public: |
Sebastian Jansson | 3ba5b9e | 2019-03-21 09:29:27 +0100 | [diff] [blame] | 43 | using rtc::TaskQueue::TaskQueue; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 44 | explicit TaskQueueForTest(absl::string_view name = "TestQueue", |
| 45 | Priority priority = Priority::NORMAL); |
| 46 | TaskQueueForTest(const TaskQueueForTest&) = delete; |
| 47 | TaskQueueForTest& operator=(const TaskQueueForTest&) = delete; |
| 48 | ~TaskQueueForTest() = default; |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 49 | |
| 50 | // A convenience, test-only method that blocks the current thread while |
| 51 | // a task executes on the task queue. |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 52 | void SendTask(rtc::FunctionView<void()> task, rtc::Location loc) { |
| 53 | ::webrtc::SendTask(loc, Get(), task); |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 54 | } |
Marina Ciocea | ce1320c | 2020-05-04 14:26:05 +0200 | [diff] [blame] | 55 | |
| 56 | // Wait for the completion of all tasks posted prior to the |
| 57 | // WaitForPreviouslyPostedTasks() call. |
| 58 | void WaitForPreviouslyPostedTasks() { |
| 59 | // Post an empty task on the queue and wait for it to finish, to ensure |
| 60 | // that all already posted tasks on the queue get executed. |
| 61 | SendTask([]() {}, RTC_FROM_HERE); |
| 62 | } |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 63 | }; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 64 | |
| 65 | } // namespace webrtc |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 66 | |
| 67 | #endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_ |