blob: 40fc9c8a7ea466f9b2c97b376b3ab71549104741 [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"
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020017#include "api/task_queue/task_queue_base.h"
Tommi68561562018-02-13 19:47:50 +010018#include "rtc_base/checks.h"
19#include "rtc_base/event.h"
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020020#include "rtc_base/location.h"
Tommi68561562018-02-13 19:47:50 +010021#include "rtc_base/task_queue.h"
Danil Chapovalov1aa75812019-03-05 11:11:35 +010022#include "rtc_base/task_utils/to_queued_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/thread_annotations.h"
Tommi68561562018-02-13 19:47:50 +010024
Danil Chapovalovd26a9162019-03-19 18:08:37 +010025namespace webrtc {
26
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020027template <typename Closure>
28void SendTask(TaskQueueBase* task_queue, Closure&& task, rtc::Location loc) {
29 RTC_CHECK(!task_queue->IsCurrent())
30 << "Called SendTask to a queue from the same queue at " << loc.ToString();
31 rtc::Event event;
32 task_queue->PostTask(
33 ToQueuedTask(std::forward<Closure>(task), [&event] { event.Set(); }));
34 RTC_CHECK(event.Wait(/*give_up_after_ms=*/120'000, /*warn_after_ms=*/10'000))
35 << "Waited too long at " << loc.ToString();
36}
37
Danil Chapovalovd26a9162019-03-19 18:08:37 +010038class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue {
Tommi68561562018-02-13 19:47:50 +010039 public:
Sebastian Jansson3ba5b9e2019-03-21 09:29:27 +010040 using rtc::TaskQueue::TaskQueue;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010041 explicit TaskQueueForTest(absl::string_view name = "TestQueue",
42 Priority priority = Priority::NORMAL);
43 TaskQueueForTest(const TaskQueueForTest&) = delete;
44 TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
45 ~TaskQueueForTest() = default;
Tommi68561562018-02-13 19:47:50 +010046
47 // A convenience, test-only method that blocks the current thread while
48 // a task executes on the task queue.
49 // This variant is specifically for posting custom QueuedTask derived
50 // implementations that tests do not want to pass ownership of over to the
51 // task queue (i.e. the Run() method always returns |false|.).
52 template <class Closure>
53 void SendTask(Closure* task) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020054 RTC_CHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010055 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010056 PostTask(ToQueuedTask(
57 [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); },
58 [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010059 event.Wait(rtc::Event::kForever);
60 }
61
62 // A convenience, test-only method that blocks the current thread while
63 // a task executes on the task queue.
64 template <class Closure>
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020065 void SendTask(Closure&& task, rtc::Location loc) {
66 ::webrtc::SendTask(Get(), std::forward<Closure>(task), loc);
Tommi68561562018-02-13 19:47:50 +010067 }
Tommi68561562018-02-13 19:47:50 +010068};
Danil Chapovalovd26a9162019-03-19 18:08:37 +010069
70} // namespace webrtc
Tommi68561562018-02-13 19:47:50 +010071
72#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_