blob: 4389d35f59258f36122d54b41b18fc715572ee20 [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
83TEST(TaskQueueTest, PostFromQueue) {
84 static const char kQueueName[] = "PostFromQueue";
tommi8c80c6e2017-02-23 00:34:52 -080085 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070086 TaskQueue queue(kQueueName);
87
tommic06b1332016-05-14 11:31:40 -070088 queue.PostTask(
89 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
90 EXPECT_TRUE(event.Wait(1000));
91}
92
tommic5b435d2016-10-31 02:17:11 -070093TEST(TaskQueueTest, PostDelayed) {
tommic06b1332016-05-14 11:31:40 -070094 static const char kQueueName[] = "PostDelayed";
tommi8c80c6e2017-02-23 00:34:52 -080095 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070096 TaskQueue queue(kQueueName);
97
tommic06b1332016-05-14 11:31:40 -070098 uint32_t start = Time();
99 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
100 EXPECT_TRUE(event.Wait(1000));
101 uint32_t end = Time();
tommic5b435d2016-10-31 02:17:11 -0700102 // These tests are a little relaxed due to how "powerful" our test bots can
tommi67fcad82016-11-16 10:50:24 -0800103 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommic5b435d2016-10-31 02:17:11 -0700104 // which is why we have a little bit of leeway backwards as well.
tommi67fcad82016-11-16 10:50:24 -0800105 EXPECT_GE(end - start, 90u);
106 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommic06b1332016-05-14 11:31:40 -0700107}
108
109TEST(TaskQueueTest, PostMultipleDelayed) {
110 static const char kQueueName[] = "PostMultipleDelayed";
111 TaskQueue queue(kQueueName);
112
113 std::vector<std::unique_ptr<Event>> events;
tommif9d91542017-02-17 02:47:11 -0800114 for (int i = 0; i < 100; ++i) {
tommic06b1332016-05-14 11:31:40 -0700115 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
116 queue.PostDelayedTask(
117 Bind(&CheckCurrent, kQueueName, events.back().get(), &queue), 10);
118 }
119
120 for (const auto& e : events)
tommif9d91542017-02-17 02:47:11 -0800121 EXPECT_TRUE(e->Wait(1000));
tommic06b1332016-05-14 11:31:40 -0700122}
123
124TEST(TaskQueueTest, PostDelayedAfterDestruct) {
125 static const char kQueueName[] = "PostDelayedAfterDestruct";
126 Event event(false, false);
127 {
128 TaskQueue queue(kQueueName);
129 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
130 }
131 EXPECT_FALSE(event.Wait(200)); // Task should not run.
132}
133
134TEST(TaskQueueTest, PostAndReply) {
135 static const char kPostQueue[] = "PostQueue";
136 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800137 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700138 TaskQueue post_queue(kPostQueue);
139 TaskQueue reply_queue(kReplyQueue);
140
tommic06b1332016-05-14 11:31:40 -0700141 post_queue.PostTaskAndReply(
142 Bind(&CheckCurrent, kPostQueue, nullptr, &post_queue),
143 Bind(&CheckCurrent, kReplyQueue, &event, &reply_queue), &reply_queue);
144 EXPECT_TRUE(event.Wait(1000));
145}
146
147TEST(TaskQueueTest, PostAndReuse) {
148 static const char kPostQueue[] = "PostQueue";
149 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800150 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700151 TaskQueue post_queue(kPostQueue);
152 TaskQueue reply_queue(kReplyQueue);
153
154 int call_count = 0;
155
156 class ReusedTask : public QueuedTask {
157 public:
158 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
159 : counter_(counter), reply_queue_(reply_queue), event_(event) {
160 EXPECT_EQ(0, *counter_);
161 }
162
163 private:
164 bool Run() override {
165 if (++(*counter_) == 1) {
166 std::unique_ptr<QueuedTask> myself(this);
167 reply_queue_->PostTask(std::move(myself));
168 // At this point, the object is owned by reply_queue_ and it's
169 // theoratically possible that the object has been deleted (e.g. if
170 // posting wasn't possible). So, don't touch any member variables here.
171
172 // Indicate to the current queue that ownership has been transferred.
173 return false;
174 } else {
175 EXPECT_EQ(2, *counter_);
176 EXPECT_TRUE(reply_queue_->IsCurrent());
177 event_->Set();
178 return true; // Indicate that the object should be deleted.
179 }
180 }
181
182 int* const counter_;
183 TaskQueue* const reply_queue_;
184 Event* const event_;
185 };
186
tommic06b1332016-05-14 11:31:40 -0700187 std::unique_ptr<QueuedTask> task(
188 new ReusedTask(&call_count, &reply_queue, &event));
189
190 post_queue.PostTask(std::move(task));
191 EXPECT_TRUE(event.Wait(1000));
192}
193
194TEST(TaskQueueTest, PostAndReplyLambda) {
195 static const char kPostQueue[] = "PostQueue";
196 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800197 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700198 TaskQueue post_queue(kPostQueue);
199 TaskQueue reply_queue(kReplyQueue);
200
tommic06b1332016-05-14 11:31:40 -0700201 bool my_flag = false;
202 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
203 [&event]() { event.Set(); }, &reply_queue);
204 EXPECT_TRUE(event.Wait(1000));
205 EXPECT_TRUE(my_flag);
206}
207
tommi8c80c6e2017-02-23 00:34:52 -0800208// This test covers a particular bug that we had in the libevent implementation
209// where we could hit a deadlock while trying to post a reply task to a queue
210// that was being deleted. The test isn't guaranteed to hit that case but it's
211// written in a way that makes it likely and by running with --gtest_repeat=1000
212// the bug would occur. Alas, now it should be fixed.
213TEST(TaskQueueTest, PostAndReplyDeadlock) {
214 Event event(false, false);
215 TaskQueue post_queue("PostQueue");
216 TaskQueue reply_queue("ReplyQueue");
217
218 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
219 &reply_queue);
220 EXPECT_TRUE(event.Wait(1000));
221}
222
tommic06b1332016-05-14 11:31:40 -0700223void TestPostTaskAndReply(TaskQueue* work_queue,
224 const char* work_queue_name,
225 Event* event) {
226 ASSERT_FALSE(work_queue->IsCurrent());
227 work_queue->PostTaskAndReply(
228 Bind(&CheckCurrent, work_queue_name, nullptr, work_queue),
229 NewClosure([event]() { event->Set(); }));
230}
231
232// Does a PostTaskAndReply from within a task to post and reply to the current
233// queue. All in all there will be 3 tasks posted and run.
234TEST(TaskQueueTest, PostAndReply2) {
235 static const char kQueueName[] = "PostAndReply2";
236 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi8c80c6e2017-02-23 00:34:52 -0800237 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700238 TaskQueue queue(kQueueName);
239 TaskQueue work_queue(kWorkQueueName);
240
tommic06b1332016-05-14 11:31:40 -0700241 queue.PostTask(
242 Bind(&TestPostTaskAndReply, &work_queue, kWorkQueueName, &event));
243 EXPECT_TRUE(event.Wait(1000));
244}
245
246// Tests posting more messages than a queue can queue up.
247// In situations like that, tasks will get dropped.
248TEST(TaskQueueTest, PostALot) {
249 // To destruct the event after the queue has gone out of scope.
250 Event event(false, false);
251
252 int tasks_executed = 0;
253 int tasks_cleaned_up = 0;
254 static const int kTaskCount = 0xffff;
255
256 {
257 static const char kQueueName[] = "PostALot";
258 TaskQueue queue(kQueueName);
259
260 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
261 // So here we post a total of 0xffff+1 messages, which triggers a failure
262 // case inside of the libevent queue implementation.
263
264 queue.PostTask([&event]() { event.Wait(Event::kForever); });
265 for (int i = 0; i < kTaskCount; ++i)
266 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
267 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
268 event.Set(); // Unblock the first task.
269 }
270
271 EXPECT_GE(tasks_cleaned_up, tasks_executed);
272 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700273}
274
275} // namespace rtc