blob: dd27f6440931dc3aa345239d64b0168ba4614a96 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/thread_annotations.h"
Tommi68561562018-02-13 19:47:50 +010019
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());
Niels Möllerc572ff32018-11-07 08:43:50 +010036 rtc::Event event;
Tommi68561562018-02-13 19:47:50 +010037 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öllerc572ff32018-11-07 08:43:50 +010050 rtc::Event event;
Tommi68561562018-02-13 19:47:50 +010051 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_