blob: be91c50480b17b51ee212d9f2b168e4644d1a642 [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
Danil Chapovalovd26a9162019-03-19 18:08:37 +010014#include <utility>
15
16#include "absl/strings/string_view.h"
Tommi68561562018-02-13 19:47:50 +010017#include "rtc_base/checks.h"
18#include "rtc_base/event.h"
19#include "rtc_base/task_queue.h"
Danil Chapovalov1aa75812019-03-05 11:11:35 +010020#include "rtc_base/task_utils/to_queued_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/thread_annotations.h"
Tommi68561562018-02-13 19:47:50 +010022
Danil Chapovalovd26a9162019-03-19 18:08:37 +010023namespace webrtc {
24
25class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue {
Tommi68561562018-02-13 19:47:50 +010026 public:
Sebastian Jansson3ba5b9e2019-03-21 09:29:27 +010027 using rtc::TaskQueue::TaskQueue;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010028 explicit TaskQueueForTest(absl::string_view name = "TestQueue",
29 Priority priority = Priority::NORMAL);
30 TaskQueueForTest(const TaskQueueForTest&) = delete;
31 TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
32 ~TaskQueueForTest() = default;
Tommi68561562018-02-13 19:47:50 +010033
34 // A convenience, test-only method that blocks the current thread while
35 // a task executes on the task queue.
36 // This variant is specifically for posting custom QueuedTask derived
37 // implementations that tests do not want to pass ownership of over to the
38 // task queue (i.e. the Run() method always returns |false|.).
39 template <class Closure>
40 void SendTask(Closure* task) {
41 RTC_DCHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010042 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010043 PostTask(ToQueuedTask(
44 [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); },
45 [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010046 event.Wait(rtc::Event::kForever);
47 }
48
49 // A convenience, test-only method that blocks the current thread while
50 // a task executes on the task queue.
51 template <class Closure>
52 void SendTask(Closure&& task) {
53 RTC_DCHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010054 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010055 PostTask(
56 ToQueuedTask(std::forward<Closure>(task), [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010057 event.Wait(rtc::Event::kForever);
58 }
Tommi68561562018-02-13 19:47:50 +010059};
Danil Chapovalovd26a9162019-03-19 18:08:37 +010060
61} // namespace webrtc
Tommi68561562018-02-13 19:47:50 +010062
63#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_