blob: 70c58fb516c1aa6ad43edfeaa93bd3cab2daca1a [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
14#include <utility>
15
16#include "rtc_base/checks.h"
17#include "rtc_base/event.h"
18#include "rtc_base/task_queue.h"
19
20namespace rtc {
21namespace test {
22class 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());
36 rtc::Event event(false, false);
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());
50 rtc::Event event(false, false);
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_