blob: 7844dc4ad9727909541275fdd4513bd3170a82b6 [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>
Danil Chapovalov82a3f0a2019-10-21 09:24:27 +020028void SendTask(rtc::Location loc, TaskQueueBase* task_queue, Closure&& task) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020029 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(); }));
Danil Chapovalov562a37f2019-10-16 19:39:09 +020034 RTC_CHECK(event.Wait(/*give_up_after_ms=*/rtc::Event::kForever,
35 /*warn_after_ms=*/10'000))
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020036 << "Waited too long at " << loc.ToString();
37}
38
Danil Chapovalovd26a9162019-03-19 18:08:37 +010039class RTC_LOCKABLE TaskQueueForTest : public rtc::TaskQueue {
Tommi68561562018-02-13 19:47:50 +010040 public:
Sebastian Jansson3ba5b9e2019-03-21 09:29:27 +010041 using rtc::TaskQueue::TaskQueue;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010042 explicit TaskQueueForTest(absl::string_view name = "TestQueue",
43 Priority priority = Priority::NORMAL);
44 TaskQueueForTest(const TaskQueueForTest&) = delete;
45 TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
46 ~TaskQueueForTest() = default;
Tommi68561562018-02-13 19:47:50 +010047
48 // A convenience, test-only method that blocks the current thread while
49 // a task executes on the task queue.
50 // This variant is specifically for posting custom QueuedTask derived
51 // implementations that tests do not want to pass ownership of over to the
52 // task queue (i.e. the Run() method always returns |false|.).
53 template <class Closure>
54 void SendTask(Closure* task) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020055 RTC_CHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010056 rtc::Event event;
Danil Chapovalovd26a9162019-03-19 18:08:37 +010057 PostTask(ToQueuedTask(
58 [&task] { RTC_CHECK_EQ(false, static_cast<QueuedTask*>(task)->Run()); },
59 [&event] { event.Set(); }));
Tommi68561562018-02-13 19:47:50 +010060 event.Wait(rtc::Event::kForever);
61 }
62
63 // A convenience, test-only method that blocks the current thread while
64 // a task executes on the task queue.
65 template <class Closure>
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020066 void SendTask(Closure&& task, rtc::Location loc) {
Danil Chapovalov82a3f0a2019-10-21 09:24:27 +020067 ::webrtc::SendTask(loc, Get(), std::forward<Closure>(task));
Tommi68561562018-02-13 19:47:50 +010068 }
Tommi68561562018-02-13 19:47:50 +010069};
Danil Chapovalovd26a9162019-03-19 18:08:37 +010070
71} // namespace webrtc
Tommi68561562018-02-13 19:47:50 +010072
73#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_