blob: b6f04cd8f66ea61c3ff4369e49bbbe49caf3d5b9 [file] [log] [blame]
Danil Chapovalov33b716f2019-01-22 18:15:37 +01001/*
2 * Copyright 2019 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#include "api/task_queue/task_queue_test.h"
11
12#include "absl/memory/memory.h"
13#include "absl/strings/string_view.h"
14#include "rtc_base/event.h"
15#include "rtc_base/task_queue.h"
Steve Antonf3802842019-01-24 19:07:40 -080016#include "rtc_base/time_utils.h"
Danil Chapovalov33b716f2019-01-22 18:15:37 +010017
18namespace webrtc {
19namespace {
20
21std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
Danil Chapovalov710f3d32019-02-06 16:00:44 +010022 const std::unique_ptr<webrtc::TaskQueueFactory>& factory,
Danil Chapovalov33b716f2019-01-22 18:15:37 +010023 absl::string_view task_queue_name,
24 TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL) {
25 return factory->CreateTaskQueue(task_queue_name, priority);
26}
27
28TEST_P(TaskQueueTest, Construct) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010029 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
30 auto queue = CreateTaskQueue(factory, "Construct");
Danil Chapovalov33b716f2019-01-22 18:15:37 +010031 EXPECT_FALSE(queue->IsCurrent());
32}
33
34TEST_P(TaskQueueTest, PostAndCheckCurrent) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010035 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +010036 rtc::Event event;
Danil Chapovalov710f3d32019-02-06 16:00:44 +010037 auto queue = CreateTaskQueue(factory, "PostAndCheckCurrent");
Danil Chapovalov33b716f2019-01-22 18:15:37 +010038
39 // We're not running a task, so there shouldn't be a current queue.
40 EXPECT_FALSE(queue->IsCurrent());
41 EXPECT_FALSE(TaskQueueBase::Current());
42
43 queue->PostTask(rtc::NewClosure([&event, &queue] {
44 EXPECT_TRUE(queue->IsCurrent());
45 event.Set();
46 }));
47 EXPECT_TRUE(event.Wait(1000));
48}
49
50TEST_P(TaskQueueTest, PostCustomTask) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010051 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +010052 rtc::Event ran;
Danil Chapovalov710f3d32019-02-06 16:00:44 +010053 auto queue = CreateTaskQueue(factory, "PostCustomImplementation");
Danil Chapovalov33b716f2019-01-22 18:15:37 +010054
55 class CustomTask : public QueuedTask {
56 public:
57 explicit CustomTask(rtc::Event* ran) : ran_(ran) {}
58
59 private:
60 bool Run() override {
61 ran_->Set();
62 return false; // Do not allow the task to be deleted by the queue.
63 }
64
65 rtc::Event* const ran_;
66 } my_task(&ran);
67
68 queue->PostTask(absl::WrapUnique(&my_task));
69 EXPECT_TRUE(ran.Wait(1000));
70}
71
72TEST_P(TaskQueueTest, PostDelayedZero) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010073 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +010074 rtc::Event event;
Danil Chapovalov710f3d32019-02-06 16:00:44 +010075 auto queue = CreateTaskQueue(factory, "PostDelayedZero");
Danil Chapovalov33b716f2019-01-22 18:15:37 +010076
77 queue->PostDelayedTask(rtc::NewClosure([&event] { event.Set(); }), 0);
78 EXPECT_TRUE(event.Wait(1000));
79}
80
81TEST_P(TaskQueueTest, PostFromQueue) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010082 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +010083 rtc::Event event;
Danil Chapovalov710f3d32019-02-06 16:00:44 +010084 auto queue = CreateTaskQueue(factory, "PostFromQueue");
Danil Chapovalov33b716f2019-01-22 18:15:37 +010085
86 queue->PostTask(rtc::NewClosure([&event, &queue] {
87 queue->PostTask(rtc::NewClosure([&event] { event.Set(); }));
88 }));
89 EXPECT_TRUE(event.Wait(1000));
90}
91
92TEST_P(TaskQueueTest, PostDelayed) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +010093 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +010094 rtc::Event event;
Danil Chapovalov710f3d32019-02-06 16:00:44 +010095 auto queue =
96 CreateTaskQueue(factory, "PostDelayed", TaskQueueFactory::Priority::HIGH);
Danil Chapovalov33b716f2019-01-22 18:15:37 +010097
98 int64_t start = rtc::TimeMillis();
99 queue->PostDelayedTask(rtc::NewClosure([&event, &queue] {
100 EXPECT_TRUE(queue->IsCurrent());
101 event.Set();
102 }),
103 100);
104 EXPECT_TRUE(event.Wait(1000));
105 int64_t end = rtc::TimeMillis();
106 // These tests are a little relaxed due to how "powerful" our test bots can
107 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
108 // which is why we have a little bit of leeway backwards as well.
109 EXPECT_GE(end - start, 90u);
110 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
111}
112
113TEST_P(TaskQueueTest, PostMultipleDelayed) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100114 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
115 auto queue = CreateTaskQueue(factory, "PostMultipleDelayed");
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100116
117 std::vector<rtc::Event> events(100);
118 for (int i = 0; i < 100; ++i) {
119 rtc::Event* event = &events[i];
120 queue->PostDelayedTask(rtc::NewClosure([event, &queue] {
121 EXPECT_TRUE(queue->IsCurrent());
122 event->Set();
123 }),
124 i);
125 }
126
127 for (rtc::Event& e : events)
128 EXPECT_TRUE(e.Wait(1000));
129}
130
131TEST_P(TaskQueueTest, PostDelayedAfterDestruct) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100132 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100133 rtc::Event run;
134 rtc::Event deleted;
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100135 auto queue = CreateTaskQueue(factory, "PostDelayedAfterDestruct");
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100136 queue->PostDelayedTask(
137 rtc::NewClosure([&run] { run.Set(); }, [&deleted] { deleted.Set(); }),
138 100);
139 // Destroy the queue.
140 queue = nullptr;
141 // Task might outlive the TaskQueue, but still should be deleted.
142 EXPECT_TRUE(deleted.Wait(200));
143 EXPECT_FALSE(run.Wait(0)); // and should not run.
144}
145
146TEST_P(TaskQueueTest, PostAndReuse) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100147 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100148 rtc::Event event;
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100149 auto post_queue = CreateTaskQueue(factory, "PostQueue");
150 auto reply_queue = CreateTaskQueue(factory, "ReplyQueue");
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100151
152 int call_count = 0;
153
154 class ReusedTask : public QueuedTask {
155 public:
156 ReusedTask(int* counter, TaskQueueBase* reply_queue, rtc::Event* event)
157 : counter_(*counter), reply_queue_(reply_queue), event_(*event) {
158 EXPECT_EQ(counter_, 0);
159 }
160
161 private:
162 bool Run() override {
163 if (++counter_ == 1) {
164 reply_queue_->PostTask(absl::WrapUnique(this));
165 // At this point, the object is owned by reply_queue_ and it's
166 // theoratically possible that the object has been deleted (e.g. if
167 // posting wasn't possible). So, don't touch any member variables here.
168
169 // Indicate to the current queue that ownership has been transferred.
170 return false;
171 } else {
172 EXPECT_EQ(counter_, 2);
173 EXPECT_TRUE(reply_queue_->IsCurrent());
174 event_.Set();
175 return true; // Indicate that the object should be deleted.
176 }
177 }
178
179 int& counter_;
180 TaskQueueBase* const reply_queue_;
181 rtc::Event& event_;
182 };
183
184 auto task =
185 absl::make_unique<ReusedTask>(&call_count, reply_queue.get(), &event);
186 post_queue->PostTask(std::move(task));
187 EXPECT_TRUE(event.Wait(1000));
188}
189
190// Tests posting more messages than a queue can queue up.
191// In situations like that, tasks will get dropped.
192TEST_P(TaskQueueTest, PostALot) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100193 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100194 // To destruct the event after the queue has gone out of scope.
195 rtc::Event event;
196
197 int tasks_executed = 0;
198 int tasks_cleaned_up = 0;
199 static const int kTaskCount = 0xffff;
200
201 {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100202 auto queue = CreateTaskQueue(factory, "PostALot");
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100203
204 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
205 // So here we post a total of 0xffff+1 messages, which triggers a failure
206 // case inside of the libevent queue implementation.
207
208 queue->PostTask(
209 rtc::NewClosure([&event] { event.Wait(rtc::Event::kForever); }));
210 for (int i = 0; i < kTaskCount; ++i)
211 queue->PostTask(
212 rtc::NewClosure([&tasks_executed] { ++tasks_executed; },
213 [&tasks_cleaned_up] { ++tasks_cleaned_up; }));
214 event.Set(); // Unblock the first task.
215 }
216
217 EXPECT_GE(tasks_cleaned_up, tasks_executed);
218 EXPECT_EQ(tasks_cleaned_up, kTaskCount);
219}
220
Artem Titov01f64e02019-01-31 13:31:09 +0100221// Test posting two tasks that have shared state not protected by a
222// lock. The TaskQueue should guarantee memory read-write order and
223// FIFO task execution order, so the second task should always see the
224// changes that were made by the first task.
225//
226// If the TaskQueue doesn't properly synchronize the execution of
227// tasks, there will be a data race, which is undefined behavior. The
228// EXPECT calls may randomly catch this, but to make the most of this
229// unit test, run it under TSan or some other tool that is able to
230// directly detect data races.
231TEST_P(TaskQueueTest, PostTwoWithSharedUnprotectedState) {
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100232 std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()();
Artem Titov01f64e02019-01-31 13:31:09 +0100233 struct SharedState {
234 // First task will set this value to 1 and second will assert it.
235 int state = 0;
236 } state;
237
Danil Chapovalov710f3d32019-02-06 16:00:44 +0100238 auto queue = CreateTaskQueue(factory, "PostTwoWithSharedUnprotectedState");
Artem Titov01f64e02019-01-31 13:31:09 +0100239 rtc::Event done;
240 queue->PostTask(rtc::NewClosure([&state, &queue, &done] {
241 // Post tasks from queue to guarantee, that 1st task won't be
242 // executed before the second one will be posted.
243 queue->PostTask(rtc::NewClosure([&state] { state.state = 1; }));
244 queue->PostTask(rtc::NewClosure([&state, &done] {
245 EXPECT_EQ(state.state, 1);
246 done.Set();
247 }));
248 // Check, that state changing tasks didn't start yet.
249 EXPECT_EQ(state.state, 0);
250 }));
251 EXPECT_TRUE(done.Wait(1000));
252}
253
Danil Chapovalov33b716f2019-01-22 18:15:37 +0100254} // namespace
255} // namespace webrtc