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 | |
| 16 | #include "absl/strings/string_view.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 17 | #include "api/task_queue/task_queue_base.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/event.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 20 | #include "rtc_base/location.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 21 | #include "rtc_base/task_queue.h" |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 22 | #include "rtc_base/task_utils/to_queued_task.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/thread_annotations.h" |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 24 | |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 27 | template <typename Closure> |
Danil Chapovalov | 82a3f0a | 2019-10-21 09:24:27 +0200 | [diff] [blame] | 28 | void SendTask(rtc::Location loc, TaskQueueBase* task_queue, Closure&& task) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 29 | RTC_CHECK(!task_queue->IsCurrent()) |
| 30 | << "Called SendTask to a queue from the same queue at " << loc.ToString(); |
| 31 | rtc::Event event; |
| 32 | task_queue->PostTask( |
| 33 | ToQueuedTask(std::forward<Closure>(task), [&event] { event.Set(); })); |
Danil Chapovalov | 562a37f | 2019-10-16 19:39:09 +0200 | [diff] [blame] | 34 | RTC_CHECK(event.Wait(/*give_up_after_ms=*/rtc::Event::kForever, |
| 35 | /*warn_after_ms=*/10'000)) |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 36 | << "Waited too long at " << loc.ToString(); |
| 37 | } |
| 38 | |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 39 | class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue { |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 40 | public: |
Sebastian Jansson | 3ba5b9e | 2019-03-21 09:29:27 +0100 | [diff] [blame] | 41 | using rtc::TaskQueue::TaskQueue; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 42 | explicit TaskQueueForTest(absl::string_view name = "TestQueue", |
| 43 | Priority priority = Priority::NORMAL); |
| 44 | TaskQueueForTest(const TaskQueueForTest&) = delete; |
| 45 | TaskQueueForTest& operator=(const TaskQueueForTest&) = delete; |
| 46 | ~TaskQueueForTest() = default; |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 47 | |
| 48 | // A convenience, test-only method that blocks the current thread while |
| 49 | // a task executes on the task queue. |
| 50 | // This variant is specifically for posting custom QueuedTask derived |
| 51 | // implementations that tests do not want to pass ownership of over to the |
| 52 | // task queue (i.e. the Run() method always returns |false|.). |
| 53 | template <class Closure> |
| 54 | void SendTask(Closure* task) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 55 | RTC_CHECK(!IsCurrent()); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 56 | rtc::Event event; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 57 | PostTask(ToQueuedTask( |
| 58 | [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); }, |
| 59 | [&event] { event.Set(); })); |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 60 | event.Wait(rtc::Event::kForever); |
| 61 | } |
| 62 | |
| 63 | // A convenience, test-only method that blocks the current thread while |
| 64 | // a task executes on the task queue. |
| 65 | template <class Closure> |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 66 | void SendTask(Closure&& task, rtc::Location loc) { |
Danil Chapovalov | 82a3f0a | 2019-10-21 09:24:27 +0200 | [diff] [blame] | 67 | ::webrtc::SendTask(loc, Get(), std::forward<Closure>(task)); |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 68 | } |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 69 | }; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 70 | |
| 71 | } // namespace webrtc |
Tommi | 6856156 | 2018-02-13 19:47:50 +0100 | [diff] [blame] | 72 | |
| 73 | #endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_ |