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