blob: 260827ff78d4c100b49ca8faedebfbe9267400f4 [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
Danil Chapovalov5286dcf2022-07-18 17:04:56 +020016#include "absl/cleanup/cleanup.h"
Danil Chapovalovd26a9162019-03-19 18:08:37 +010017#include "absl/strings/string_view.h"
Danil Chapovalov5286dcf2022-07-18 17:04:56 +020018#include "api/function_view.h"
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020019#include "api/task_queue/task_queue_base.h"
Tommi68561562018-02-13 19:47:50 +010020#include "rtc_base/checks.h"
21#include "rtc_base/event.h"
22#include "rtc_base/task_queue.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 Chapovalove519f382022-08-11 12:26:09 +020027inline void SendTask(TaskQueueBase* task_queue,
Danil Chapovalov5286dcf2022-07-18 17:04:56 +020028 rtc::FunctionView<void()> task) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020029 RTC_CHECK(!task_queue->IsCurrent())
Danil Chapovalove519f382022-08-11 12:26:09 +020030 << "Called SendTask to a queue from the same queue";
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020031 rtc::Event event;
Danil Chapovalov5286dcf2022-07-18 17:04:56 +020032 absl::Cleanup cleanup = [&event] { event.Set(); };
33 task_queue->PostTask([task, cleanup = std::move(cleanup)] { task(); });
Danil Chapovalov562a37f2019-10-16 19:39:09 +020034 RTC_CHECK(event.Wait(/*give_up_after_ms=*/rtc::Event::kForever,
Danil Chapovalove519f382022-08-11 12:26:09 +020035 /*warn_after_ms=*/10'000));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020036}
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.
Danil Chapovalove519f382022-08-11 12:26:09 +020049 void SendTask(rtc::FunctionView<void()> task) {
50 ::webrtc::SendTask(Get(), task);
Tommi68561562018-02-13 19:47:50 +010051 }
Marina Cioceace1320c2020-05-04 14:26:05 +020052
53 // Wait for the completion of all tasks posted prior to the
54 // WaitForPreviouslyPostedTasks() call.
55 void WaitForPreviouslyPostedTasks() {
56 // Post an empty task on the queue and wait for it to finish, to ensure
57 // that all already posted tasks on the queue get executed.
Danil Chapovalove519f382022-08-11 12:26:09 +020058 SendTask([]() {});
Marina Cioceace1320c2020-05-04 14:26:05 +020059 }
Tommi68561562018-02-13 19:47:50 +010060};
Danil Chapovalovd26a9162019-03-19 18:08:37 +010061
62} // namespace webrtc
Tommi68561562018-02-13 19:47:50 +010063
64#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_