blob: e578a61bbd06e6907db7d5fd699ed017f56567ba [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:
Danil Chapovalovd26a9162019-03-19 18:08:37 +010027 explicit TaskQueueForTest(absl::string_view name = "TestQueue",
28 Priority priority = Priority::NORMAL);
29 TaskQueueForTest(const TaskQueueForTest&) = delete;
30 TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
31 ~TaskQueueForTest() = default;
Tommi68561562018-02-13 19:47:50 +010032
33 // A convenience, test-only method that blocks the current thread while
34 // a task executes on the task queue.
35 // This variant is specifically for posting custom QueuedTask derived
36 // implementations that tests do not want to pass ownership of over to the
37 // task queue (i.e. the Run() method always returns |false|.).
38 template <class Closure>
39 void SendTask(Closure* task) {
40 RTC_DCHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010041 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010042 PostTask(ToQueuedTask(
43 [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); },
44 [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010045 event.Wait(rtc::Event::kForever);
46 }
47
48 // A convenience, test-only method that blocks the current thread while
49 // a task executes on the task queue.
50 template <class Closure>
51 void SendTask(Closure&& task) {
52 RTC_DCHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010053 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010054 PostTask(
55 ToQueuedTask(std::forward<Closure>(task), [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010056 event.Wait(rtc::Event::kForever);
57 }
Tommi68561562018-02-13 19:47:50 +010058};
Danil Chapovalovd26a9162019-03-19 18:08:37 +010059
60} // namespace webrtc
Tommi68561562018-02-13 19:47:50 +010061
62#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_