blob: 512c4eefe106047453312413a85defc2858a7cb1 [file] [log] [blame]
tommie9bea572016-05-14 18:31:401/*
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
tommia2b3db12017-03-10 17:33:5311#if defined(WEBRTC_WIN)
12// clang-format off
13#include <windows.h> // Must come first.
14#include <mmsystem.h>
15// clang-format on
16#endif
17
tommie9bea572016-05-14 18:31:4018#include <memory>
19#include <vector>
20
kjellander19796962017-06-30 17:45:2121#include "webrtc/rtc_base/bind.h"
22#include "webrtc/rtc_base/event.h"
23#include "webrtc/rtc_base/gunit.h"
24#include "webrtc/rtc_base/task_queue.h"
25#include "webrtc/rtc_base/timeutils.h"
tommie9bea572016-05-14 18:31:4026
27namespace rtc {
tommia2b3db12017-03-10 17:33:5328namespace {
29// Noop on all platforms except Windows, where it turns on high precision
30// multimedia timers which increases the precision of TimeMillis() while in
31// scope.
32class EnableHighResTimers {
33 public:
34#if !defined(WEBRTC_WIN)
35 EnableHighResTimers() {}
36#else
37 EnableHighResTimers() : enabled_(timeBeginPeriod(1) == TIMERR_NOERROR) {}
38 ~EnableHighResTimers() {
39 if (enabled_)
40 timeEndPeriod(1);
41 }
42
43 private:
44 const bool enabled_;
45#endif
46};
47}
tommie9bea572016-05-14 18:31:4048
49namespace {
nisse48dee582017-09-04 12:18:2150void CheckCurrent(Event* signal, TaskQueue* queue) {
tommie9bea572016-05-14 18:31:4051 EXPECT_TRUE(queue->IsCurrent());
52 if (signal)
53 signal->Set();
54}
55
56} // namespace
57
58TEST(TaskQueueTest, Construct) {
59 static const char kQueueName[] = "Construct";
60 TaskQueue queue(kQueueName);
61 EXPECT_FALSE(queue.IsCurrent());
62}
63
64TEST(TaskQueueTest, PostAndCheckCurrent) {
65 static const char kQueueName[] = "PostAndCheckCurrent";
tommi1ef4d022017-02-23 08:34:5266 Event event(false, false);
tommie9bea572016-05-14 18:31:4067 TaskQueue queue(kQueueName);
68
69 // We're not running a task, so there shouldn't be a current queue.
70 EXPECT_FALSE(queue.IsCurrent());
71 EXPECT_FALSE(TaskQueue::Current());
72
nisse48dee582017-09-04 12:18:2173 queue.PostTask(Bind(&CheckCurrent, &event, &queue));
tommie9bea572016-05-14 18:31:4074 EXPECT_TRUE(event.Wait(1000));
75}
76
77TEST(TaskQueueTest, PostCustomTask) {
78 static const char kQueueName[] = "PostCustomImplementation";
tommie9bea572016-05-14 18:31:4079 Event event(false, false);
tommi1ef4d022017-02-23 08:34:5280 TaskQueue queue(kQueueName);
tommie9bea572016-05-14 18:31:4081
82 class CustomTask : public QueuedTask {
83 public:
84 explicit CustomTask(Event* event) : event_(event) {}
85
86 private:
87 bool Run() override {
88 event_->Set();
89 return false; // Never allows the task to be deleted by the queue.
90 }
91
92 Event* const event_;
93 } my_task(&event);
94
95 // Please don't do this in production code! :)
96 queue.PostTask(std::unique_ptr<QueuedTask>(&my_task));
97 EXPECT_TRUE(event.Wait(1000));
98}
99
100TEST(TaskQueueTest, PostLambda) {
101 static const char kQueueName[] = "PostLambda";
tommi1ef4d022017-02-23 08:34:52102 Event event(false, false);
tommie9bea572016-05-14 18:31:40103 TaskQueue queue(kQueueName);
104
tommie9bea572016-05-14 18:31:40105 queue.PostTask([&event]() { event.Set(); });
106 EXPECT_TRUE(event.Wait(1000));
107}
108
tommi14e74e82017-02-27 15:16:10109TEST(TaskQueueTest, PostDelayedZero) {
110 static const char kQueueName[] = "PostDelayedZero";
111 Event event(false, false);
112 TaskQueue queue(kQueueName);
113
114 queue.PostDelayedTask([&event]() { event.Set(); }, 0);
115 EXPECT_TRUE(event.Wait(1000));
116}
117
tommie9bea572016-05-14 18:31:40118TEST(TaskQueueTest, PostFromQueue) {
119 static const char kQueueName[] = "PostFromQueue";
tommi1ef4d022017-02-23 08:34:52120 Event event(false, false);
tommie9bea572016-05-14 18:31:40121 TaskQueue queue(kQueueName);
122
tommie9bea572016-05-14 18:31:40123 queue.PostTask(
124 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
125 EXPECT_TRUE(event.Wait(1000));
126}
127
tommi8cbcd352016-10-31 09:17:11128TEST(TaskQueueTest, PostDelayed) {
tommie9bea572016-05-14 18:31:40129 static const char kQueueName[] = "PostDelayed";
tommi1ef4d022017-02-23 08:34:52130 Event event(false, false);
tommi4bec8ef2017-03-03 13:20:12131 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
tommie9bea572016-05-14 18:31:40132
tommie9bea572016-05-14 18:31:40133 uint32_t start = Time();
nisse48dee582017-09-04 12:18:21134 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommie9bea572016-05-14 18:31:40135 EXPECT_TRUE(event.Wait(1000));
136 uint32_t end = Time();
tommi8cbcd352016-10-31 09:17:11137 // These tests are a little relaxed due to how "powerful" our test bots can
tommia8bd6b62016-11-16 18:50:24138 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommi8cbcd352016-10-31 09:17:11139 // which is why we have a little bit of leeway backwards as well.
tommia8bd6b62016-11-16 18:50:24140 EXPECT_GE(end - start, 90u);
141 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommie9bea572016-05-14 18:31:40142}
143
tommia2b3db12017-03-10 17:33:53144// This task needs to be run manually due to the slowness of some of our bots.
145// TODO(tommi): Can we run this on the perf bots?
146TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) {
147 EnableHighResTimers high_res_scope;
148
149 static const char kQueueName[] = "PostDelayedHighRes";
150 Event event(false, false);
151 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
152
153 uint32_t start = Time();
nisse48dee582017-09-04 12:18:21154 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3);
tommia2b3db12017-03-10 17:33:53155 EXPECT_TRUE(event.Wait(1000));
156 uint32_t end = TimeMillis();
157 // These tests are a little relaxed due to how "powerful" our test bots can
158 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
159 // which is why we have a little bit of leeway backwards as well.
160 EXPECT_GE(end - start, 3u);
161 EXPECT_NEAR(end - start, 3, 3u);
162}
163
tommie9bea572016-05-14 18:31:40164TEST(TaskQueueTest, PostMultipleDelayed) {
165 static const char kQueueName[] = "PostMultipleDelayed";
166 TaskQueue queue(kQueueName);
167
168 std::vector<std::unique_ptr<Event>> events;
tommicf3ae612017-02-17 10:47:11169 for (int i = 0; i < 100; ++i) {
tommie9bea572016-05-14 18:31:40170 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
171 queue.PostDelayedTask(
nisse48dee582017-09-04 12:18:21172 Bind(&CheckCurrent, events.back().get(), &queue), i);
tommie9bea572016-05-14 18:31:40173 }
174
175 for (const auto& e : events)
tommicf3ae612017-02-17 10:47:11176 EXPECT_TRUE(e->Wait(1000));
tommie9bea572016-05-14 18:31:40177}
178
179TEST(TaskQueueTest, PostDelayedAfterDestruct) {
180 static const char kQueueName[] = "PostDelayedAfterDestruct";
181 Event event(false, false);
182 {
183 TaskQueue queue(kQueueName);
nisse48dee582017-09-04 12:18:21184 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommie9bea572016-05-14 18:31:40185 }
186 EXPECT_FALSE(event.Wait(200)); // Task should not run.
187}
188
189TEST(TaskQueueTest, PostAndReply) {
190 static const char kPostQueue[] = "PostQueue";
191 static const char kReplyQueue[] = "ReplyQueue";
tommi1ef4d022017-02-23 08:34:52192 Event event(false, false);
tommie9bea572016-05-14 18:31:40193 TaskQueue post_queue(kPostQueue);
194 TaskQueue reply_queue(kReplyQueue);
195
tommie9bea572016-05-14 18:31:40196 post_queue.PostTaskAndReply(
nisse48dee582017-09-04 12:18:21197 Bind(&CheckCurrent, nullptr, &post_queue),
198 Bind(&CheckCurrent, &event, &reply_queue), &reply_queue);
tommie9bea572016-05-14 18:31:40199 EXPECT_TRUE(event.Wait(1000));
200}
201
202TEST(TaskQueueTest, PostAndReuse) {
203 static const char kPostQueue[] = "PostQueue";
204 static const char kReplyQueue[] = "ReplyQueue";
tommi1ef4d022017-02-23 08:34:52205 Event event(false, false);
tommie9bea572016-05-14 18:31:40206 TaskQueue post_queue(kPostQueue);
207 TaskQueue reply_queue(kReplyQueue);
208
209 int call_count = 0;
210
211 class ReusedTask : public QueuedTask {
212 public:
213 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
214 : counter_(counter), reply_queue_(reply_queue), event_(event) {
215 EXPECT_EQ(0, *counter_);
216 }
217
218 private:
219 bool Run() override {
220 if (++(*counter_) == 1) {
221 std::unique_ptr<QueuedTask> myself(this);
222 reply_queue_->PostTask(std::move(myself));
223 // At this point, the object is owned by reply_queue_ and it's
224 // theoratically possible that the object has been deleted (e.g. if
225 // posting wasn't possible). So, don't touch any member variables here.
226
227 // Indicate to the current queue that ownership has been transferred.
228 return false;
229 } else {
230 EXPECT_EQ(2, *counter_);
231 EXPECT_TRUE(reply_queue_->IsCurrent());
232 event_->Set();
233 return true; // Indicate that the object should be deleted.
234 }
235 }
236
237 int* const counter_;
238 TaskQueue* const reply_queue_;
239 Event* const event_;
240 };
241
tommie9bea572016-05-14 18:31:40242 std::unique_ptr<QueuedTask> task(
243 new ReusedTask(&call_count, &reply_queue, &event));
244
245 post_queue.PostTask(std::move(task));
246 EXPECT_TRUE(event.Wait(1000));
247}
248
249TEST(TaskQueueTest, PostAndReplyLambda) {
250 static const char kPostQueue[] = "PostQueue";
251 static const char kReplyQueue[] = "ReplyQueue";
tommi1ef4d022017-02-23 08:34:52252 Event event(false, false);
tommie9bea572016-05-14 18:31:40253 TaskQueue post_queue(kPostQueue);
254 TaskQueue reply_queue(kReplyQueue);
255
tommie9bea572016-05-14 18:31:40256 bool my_flag = false;
257 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
258 [&event]() { event.Set(); }, &reply_queue);
259 EXPECT_TRUE(event.Wait(1000));
260 EXPECT_TRUE(my_flag);
261}
262
tommi1ef4d022017-02-23 08:34:52263// This test covers a particular bug that we had in the libevent implementation
264// where we could hit a deadlock while trying to post a reply task to a queue
265// that was being deleted. The test isn't guaranteed to hit that case but it's
266// written in a way that makes it likely and by running with --gtest_repeat=1000
267// the bug would occur. Alas, now it should be fixed.
268TEST(TaskQueueTest, PostAndReplyDeadlock) {
269 Event event(false, false);
270 TaskQueue post_queue("PostQueue");
271 TaskQueue reply_queue("ReplyQueue");
272
273 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
274 &reply_queue);
275 EXPECT_TRUE(event.Wait(1000));
276}
277
tommie9bea572016-05-14 18:31:40278void TestPostTaskAndReply(TaskQueue* work_queue,
tommie9bea572016-05-14 18:31:40279 Event* event) {
280 ASSERT_FALSE(work_queue->IsCurrent());
281 work_queue->PostTaskAndReply(
nisse48dee582017-09-04 12:18:21282 Bind(&CheckCurrent, nullptr, work_queue),
tommie9bea572016-05-14 18:31:40283 NewClosure([event]() { event->Set(); }));
284}
285
286// Does a PostTaskAndReply from within a task to post and reply to the current
287// queue. All in all there will be 3 tasks posted and run.
288TEST(TaskQueueTest, PostAndReply2) {
289 static const char kQueueName[] = "PostAndReply2";
290 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi1ef4d022017-02-23 08:34:52291 Event event(false, false);
tommie9bea572016-05-14 18:31:40292 TaskQueue queue(kQueueName);
293 TaskQueue work_queue(kWorkQueueName);
294
tommie9bea572016-05-14 18:31:40295 queue.PostTask(
nisse48dee582017-09-04 12:18:21296 Bind(&TestPostTaskAndReply, &work_queue, &event));
tommie9bea572016-05-14 18:31:40297 EXPECT_TRUE(event.Wait(1000));
298}
299
300// Tests posting more messages than a queue can queue up.
301// In situations like that, tasks will get dropped.
302TEST(TaskQueueTest, PostALot) {
303 // To destruct the event after the queue has gone out of scope.
304 Event event(false, false);
305
306 int tasks_executed = 0;
307 int tasks_cleaned_up = 0;
308 static const int kTaskCount = 0xffff;
309
310 {
311 static const char kQueueName[] = "PostALot";
312 TaskQueue queue(kQueueName);
313
314 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
315 // So here we post a total of 0xffff+1 messages, which triggers a failure
316 // case inside of the libevent queue implementation.
317
318 queue.PostTask([&event]() { event.Wait(Event::kForever); });
319 for (int i = 0; i < kTaskCount; ++i)
320 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
321 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
322 event.Set(); // Unblock the first task.
323 }
324
325 EXPECT_GE(tasks_cleaned_up, tasks_executed);
326 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommie9bea572016-05-14 18:31:40327}
328
329} // namespace rtc