blob: 0becc80975c06010fe7bdb9e613e5efc78efd74a [file] [log] [blame]
Tommi68561562018-02-13 19:47:50 +01001/*
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
Tommi68561562018-02-13 19:47:50 +010014#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/constructor_magic.h"
Tommi68561562018-02-13 19:47:50 +010016#include "rtc_base/event.h"
17#include "rtc_base/task_queue.h"
Danil Chapovalov1aa75812019-03-05 11:11:35 +010018#include "rtc_base/task_utils/to_queued_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/thread_annotations.h"
Tommi68561562018-02-13 19:47:50 +010020
21namespace rtc {
22namespace test {
23class 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öllerc572ff32018-11-07 08:43:50 +010037 rtc::Event event;
Danil Chapovalov1aa75812019-03-05 11:11:35 +010038 PostTask(webrtc::ToQueuedTask(
Tommi68561562018-02-13 19:47:50 +010039 [&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öllerc572ff32018-11-07 08:43:50 +010051 rtc::Event event;
Danil Chapovalov1aa75812019-03-05 11:11:35 +010052 PostTask(webrtc::ToQueuedTask(std::forward<Closure>(task),
53 [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010054 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_