blob: b54b1daefa5fd4c31e57c343809fb35d7f9dda6e [file] [log] [blame]
Tommi68561562018-02-13 18:47:501/*
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 17:08:3714#include <utility>
15
Danil Chapovalov5286dcf2022-07-18 15:04:5616#include "absl/cleanup/cleanup.h"
Danil Chapovalovd26a9162019-03-19 17:08:3717#include "absl/strings/string_view.h"
Danil Chapovalov5286dcf2022-07-18 15:04:5618#include "api/function_view.h"
Danil Chapovaloveb90e6f2019-10-15 08:04:5719#include "api/task_queue/task_queue_base.h"
Danil Chapovalov0206a972024-01-19 13:08:5320#include "api/task_queue/task_queue_factory.h"
Tommi68561562018-02-13 18:47:5021#include "rtc_base/checks.h"
22#include "rtc_base/event.h"
Tommi68561562018-02-13 18:47:5023
Danil Chapovalovd26a9162019-03-19 17:08:3724namespace webrtc {
25
Danil Chapovalove519f382022-08-11 10:26:0926inline void SendTask(TaskQueueBase* task_queue,
Danil Chapovalov5286dcf2022-07-18 15:04:5627 rtc::FunctionView<void()> task) {
Danil Chapovalov2aaef452022-08-12 13:55:1128 if (task_queue->IsCurrent()) {
29 task();
30 return;
31 }
32
Danil Chapovaloveb90e6f2019-10-15 08:04:5733 rtc::Event event;
Danil Chapovalov5286dcf2022-07-18 15:04:5634 absl::Cleanup cleanup = [&event] { event.Set(); };
35 task_queue->PostTask([task, cleanup = std::move(cleanup)] { task(); });
Markus Handell1d5be492022-08-18 13:49:0936 RTC_CHECK(event.Wait(/*give_up_after=*/rtc::Event::kForever,
37 /*warn_after=*/TimeDelta::Seconds(10)));
Danil Chapovaloveb90e6f2019-10-15 08:04:5738}
39
Danil Chapovalov0206a972024-01-19 13:08:5340class TaskQueueForTest {
Tommi68561562018-02-13 18:47:5041 public:
Danil Chapovalov0206a972024-01-19 13:08:5342 explicit TaskQueueForTest(
43 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue);
44 explicit TaskQueueForTest(
45 absl::string_view name = "TestQueue",
46 TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL);
Danil Chapovalovd26a9162019-03-19 17:08:3747 TaskQueueForTest(const TaskQueueForTest&) = delete;
48 TaskQueueForTest& operator=(const TaskQueueForTest&) = delete;
Danil Chapovalov0206a972024-01-19 13:08:5349 ~TaskQueueForTest();
50
51 bool IsCurrent() const { return impl_->IsCurrent(); }
52
53 // Returns non-owning pointer to the task queue implementation.
54 TaskQueueBase* Get() { return impl_.get(); }
55
56 void PostTask(
57 absl::AnyInvocable<void() &&> task,
58 const webrtc::Location& location = webrtc::Location::Current()) {
59 impl_->PostTask(std::move(task), location);
60 }
61 void PostDelayedTask(
62 absl::AnyInvocable<void() &&> task,
63 webrtc::TimeDelta delay,
64 const webrtc::Location& location = webrtc::Location::Current()) {
65 impl_->PostDelayedTask(std::move(task), delay, location);
66 }
67 void PostDelayedHighPrecisionTask(
68 absl::AnyInvocable<void() &&> task,
69 webrtc::TimeDelta delay,
70 const webrtc::Location& location = webrtc::Location::Current()) {
71 impl_->PostDelayedHighPrecisionTask(std::move(task), delay, location);
72 }
Tommi68561562018-02-13 18:47:5073
74 // A convenience, test-only method that blocks the current thread while
75 // a task executes on the task queue.
Danil Chapovalove519f382022-08-11 10:26:0976 void SendTask(rtc::FunctionView<void()> task) {
77 ::webrtc::SendTask(Get(), task);
Tommi68561562018-02-13 18:47:5078 }
Marina Cioceace1320c2020-05-04 12:26:0579
80 // Wait for the completion of all tasks posted prior to the
81 // WaitForPreviouslyPostedTasks() call.
82 void WaitForPreviouslyPostedTasks() {
Danil Chapovalov2aaef452022-08-12 13:55:1183 RTC_DCHECK(!Get()->IsCurrent());
Marina Cioceace1320c2020-05-04 12:26:0584 // Post an empty task on the queue and wait for it to finish, to ensure
85 // that all already posted tasks on the queue get executed.
Danil Chapovalove519f382022-08-11 10:26:0986 SendTask([]() {});
Marina Cioceace1320c2020-05-04 12:26:0587 }
Danil Chapovalov0206a972024-01-19 13:08:5388
89 private:
90 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> impl_;
Tommi68561562018-02-13 18:47:5091};
Danil Chapovalovd26a9162019-03-19 17:08:3792
93} // namespace webrtc
Tommi68561562018-02-13 18:47:5094
95#endif // RTC_BASE_TASK_QUEUE_FOR_TEST_H_