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