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