blob: 432a0d5579b91945c43d70cb9c89794717203f13 [file] [log] [blame]
tommic06b1332016-05-14 11:31:40 -07001/*
2 * Copyright 2016 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#include <memory>
12#include <vector>
13
14#include "webrtc/base/bind.h"
15#include "webrtc/base/event.h"
16#include "webrtc/base/gunit.h"
17#include "webrtc/base/task_queue.h"
18#include "webrtc/base/timeutils.h"
19
20namespace rtc {
21
22namespace {
23void CheckCurrent(const char* expected_queue, Event* signal, TaskQueue* queue) {
24 EXPECT_TRUE(TaskQueue::IsCurrent(expected_queue));
25 EXPECT_TRUE(queue->IsCurrent());
26 if (signal)
27 signal->Set();
28}
29
30} // namespace
31
32TEST(TaskQueueTest, Construct) {
33 static const char kQueueName[] = "Construct";
34 TaskQueue queue(kQueueName);
35 EXPECT_FALSE(queue.IsCurrent());
36}
37
38TEST(TaskQueueTest, PostAndCheckCurrent) {
39 static const char kQueueName[] = "PostAndCheckCurrent";
tommi8c80c6e2017-02-23 00:34:52 -080040 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070041 TaskQueue queue(kQueueName);
42
43 // We're not running a task, so there shouldn't be a current queue.
44 EXPECT_FALSE(queue.IsCurrent());
45 EXPECT_FALSE(TaskQueue::Current());
46
tommic06b1332016-05-14 11:31:40 -070047 queue.PostTask(Bind(&CheckCurrent, kQueueName, &event, &queue));
48 EXPECT_TRUE(event.Wait(1000));
49}
50
51TEST(TaskQueueTest, PostCustomTask) {
52 static const char kQueueName[] = "PostCustomImplementation";
tommic06b1332016-05-14 11:31:40 -070053 Event event(false, false);
tommi8c80c6e2017-02-23 00:34:52 -080054 TaskQueue queue(kQueueName);
tommic06b1332016-05-14 11:31:40 -070055
56 class CustomTask : public QueuedTask {
57 public:
58 explicit CustomTask(Event* event) : event_(event) {}
59
60 private:
61 bool Run() override {
62 event_->Set();
63 return false; // Never allows the task to be deleted by the queue.
64 }
65
66 Event* const event_;
67 } my_task(&event);
68
69 // Please don't do this in production code! :)
70 queue.PostTask(std::unique_ptr<QueuedTask>(&my_task));
71 EXPECT_TRUE(event.Wait(1000));
72}
73
74TEST(TaskQueueTest, PostLambda) {
75 static const char kQueueName[] = "PostLambda";
tommi8c80c6e2017-02-23 00:34:52 -080076 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070077 TaskQueue queue(kQueueName);
78
tommic06b1332016-05-14 11:31:40 -070079 queue.PostTask([&event]() { event.Set(); });
80 EXPECT_TRUE(event.Wait(1000));
81}
82
tommiede07592017-02-27 07:16:10 -080083TEST(TaskQueueTest, PostDelayedZero) {
84 static const char kQueueName[] = "PostDelayedZero";
85 Event event(false, false);
86 TaskQueue queue(kQueueName);
87
88 queue.PostDelayedTask([&event]() { event.Set(); }, 0);
89 EXPECT_TRUE(event.Wait(1000));
90}
91
tommic06b1332016-05-14 11:31:40 -070092TEST(TaskQueueTest, PostFromQueue) {
93 static const char kQueueName[] = "PostFromQueue";
tommi8c80c6e2017-02-23 00:34:52 -080094 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070095 TaskQueue queue(kQueueName);
96
tommic06b1332016-05-14 11:31:40 -070097 queue.PostTask(
98 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
99 EXPECT_TRUE(event.Wait(1000));
100}
101
tommic5b435d2016-10-31 02:17:11 -0700102TEST(TaskQueueTest, PostDelayed) {
tommic06b1332016-05-14 11:31:40 -0700103 static const char kQueueName[] = "PostDelayed";
tommi8c80c6e2017-02-23 00:34:52 -0800104 Event event(false, false);
tommi5bdee472017-03-03 05:20:12 -0800105 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
tommic06b1332016-05-14 11:31:40 -0700106
tommic06b1332016-05-14 11:31:40 -0700107 uint32_t start = Time();
108 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
109 EXPECT_TRUE(event.Wait(1000));
110 uint32_t end = Time();
tommic5b435d2016-10-31 02:17:11 -0700111 // These tests are a little relaxed due to how "powerful" our test bots can
tommi67fcad82016-11-16 10:50:24 -0800112 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommic5b435d2016-10-31 02:17:11 -0700113 // which is why we have a little bit of leeway backwards as well.
tommi67fcad82016-11-16 10:50:24 -0800114 EXPECT_GE(end - start, 90u);
115 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommic06b1332016-05-14 11:31:40 -0700116}
117
118TEST(TaskQueueTest, PostMultipleDelayed) {
119 static const char kQueueName[] = "PostMultipleDelayed";
120 TaskQueue queue(kQueueName);
121
122 std::vector<std::unique_ptr<Event>> events;
tommif9d91542017-02-17 02:47:11 -0800123 for (int i = 0; i < 100; ++i) {
tommic06b1332016-05-14 11:31:40 -0700124 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
125 queue.PostDelayedTask(
126 Bind(&CheckCurrent, kQueueName, events.back().get(), &queue), 10);
127 }
128
129 for (const auto& e : events)
tommif9d91542017-02-17 02:47:11 -0800130 EXPECT_TRUE(e->Wait(1000));
tommic06b1332016-05-14 11:31:40 -0700131}
132
133TEST(TaskQueueTest, PostDelayedAfterDestruct) {
134 static const char kQueueName[] = "PostDelayedAfterDestruct";
135 Event event(false, false);
136 {
137 TaskQueue queue(kQueueName);
138 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
139 }
140 EXPECT_FALSE(event.Wait(200)); // Task should not run.
141}
142
143TEST(TaskQueueTest, PostAndReply) {
144 static const char kPostQueue[] = "PostQueue";
145 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800146 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700147 TaskQueue post_queue(kPostQueue);
148 TaskQueue reply_queue(kReplyQueue);
149
tommic06b1332016-05-14 11:31:40 -0700150 post_queue.PostTaskAndReply(
151 Bind(&CheckCurrent, kPostQueue, nullptr, &post_queue),
152 Bind(&CheckCurrent, kReplyQueue, &event, &reply_queue), &reply_queue);
153 EXPECT_TRUE(event.Wait(1000));
154}
155
156TEST(TaskQueueTest, PostAndReuse) {
157 static const char kPostQueue[] = "PostQueue";
158 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800159 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700160 TaskQueue post_queue(kPostQueue);
161 TaskQueue reply_queue(kReplyQueue);
162
163 int call_count = 0;
164
165 class ReusedTask : public QueuedTask {
166 public:
167 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
168 : counter_(counter), reply_queue_(reply_queue), event_(event) {
169 EXPECT_EQ(0, *counter_);
170 }
171
172 private:
173 bool Run() override {
174 if (++(*counter_) == 1) {
175 std::unique_ptr<QueuedTask> myself(this);
176 reply_queue_->PostTask(std::move(myself));
177 // At this point, the object is owned by reply_queue_ and it's
178 // theoratically possible that the object has been deleted (e.g. if
179 // posting wasn't possible). So, don't touch any member variables here.
180
181 // Indicate to the current queue that ownership has been transferred.
182 return false;
183 } else {
184 EXPECT_EQ(2, *counter_);
185 EXPECT_TRUE(reply_queue_->IsCurrent());
186 event_->Set();
187 return true; // Indicate that the object should be deleted.
188 }
189 }
190
191 int* const counter_;
192 TaskQueue* const reply_queue_;
193 Event* const event_;
194 };
195
tommic06b1332016-05-14 11:31:40 -0700196 std::unique_ptr<QueuedTask> task(
197 new ReusedTask(&call_count, &reply_queue, &event));
198
199 post_queue.PostTask(std::move(task));
200 EXPECT_TRUE(event.Wait(1000));
201}
202
203TEST(TaskQueueTest, PostAndReplyLambda) {
204 static const char kPostQueue[] = "PostQueue";
205 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800206 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700207 TaskQueue post_queue(kPostQueue);
208 TaskQueue reply_queue(kReplyQueue);
209
tommic06b1332016-05-14 11:31:40 -0700210 bool my_flag = false;
211 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
212 [&event]() { event.Set(); }, &reply_queue);
213 EXPECT_TRUE(event.Wait(1000));
214 EXPECT_TRUE(my_flag);
215}
216
tommi8c80c6e2017-02-23 00:34:52 -0800217// This test covers a particular bug that we had in the libevent implementation
218// where we could hit a deadlock while trying to post a reply task to a queue
219// that was being deleted. The test isn't guaranteed to hit that case but it's
220// written in a way that makes it likely and by running with --gtest_repeat=1000
221// the bug would occur. Alas, now it should be fixed.
222TEST(TaskQueueTest, PostAndReplyDeadlock) {
223 Event event(false, false);
224 TaskQueue post_queue("PostQueue");
225 TaskQueue reply_queue("ReplyQueue");
226
227 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
228 &reply_queue);
229 EXPECT_TRUE(event.Wait(1000));
230}
231
tommic06b1332016-05-14 11:31:40 -0700232void TestPostTaskAndReply(TaskQueue* work_queue,
233 const char* work_queue_name,
234 Event* event) {
235 ASSERT_FALSE(work_queue->IsCurrent());
236 work_queue->PostTaskAndReply(
237 Bind(&CheckCurrent, work_queue_name, nullptr, work_queue),
238 NewClosure([event]() { event->Set(); }));
239}
240
241// Does a PostTaskAndReply from within a task to post and reply to the current
242// queue. All in all there will be 3 tasks posted and run.
243TEST(TaskQueueTest, PostAndReply2) {
244 static const char kQueueName[] = "PostAndReply2";
245 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi8c80c6e2017-02-23 00:34:52 -0800246 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700247 TaskQueue queue(kQueueName);
248 TaskQueue work_queue(kWorkQueueName);
249
tommic06b1332016-05-14 11:31:40 -0700250 queue.PostTask(
251 Bind(&TestPostTaskAndReply, &work_queue, kWorkQueueName, &event));
252 EXPECT_TRUE(event.Wait(1000));
253}
254
255// Tests posting more messages than a queue can queue up.
256// In situations like that, tasks will get dropped.
257TEST(TaskQueueTest, PostALot) {
258 // To destruct the event after the queue has gone out of scope.
259 Event event(false, false);
260
261 int tasks_executed = 0;
262 int tasks_cleaned_up = 0;
263 static const int kTaskCount = 0xffff;
264
265 {
266 static const char kQueueName[] = "PostALot";
267 TaskQueue queue(kQueueName);
268
269 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
270 // So here we post a total of 0xffff+1 messages, which triggers a failure
271 // case inside of the libevent queue implementation.
272
273 queue.PostTask([&event]() { event.Wait(Event::kForever); });
274 for (int i = 0; i < kTaskCount; ++i)
275 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
276 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
277 event.Set(); // Unblock the first task.
278 }
279
280 EXPECT_GE(tasks_cleaned_up, tasks_executed);
281 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700282}
283
284} // namespace rtc