blob: d70a5fb090d28f8cdb3b9195e35f29a7fdfd90be [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
tommi0b942152017-03-10 09:33:53 -080011#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
tommic06b1332016-05-14 11:31:40 -070018#include <memory>
19#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/bind.h"
22#include "rtc_base/event.h"
23#include "rtc_base/gunit.h"
24#include "rtc_base/task_queue.h"
25#include "rtc_base/timeutils.h"
tommic06b1332016-05-14 11:31:40 -070026
27namespace rtc {
tommi0b942152017-03-10 09:33:53 -080028namespace {
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}
tommic06b1332016-05-14 11:31:40 -070048
49namespace {
nisse2c7b7a62017-09-04 05:18:21 -070050void CheckCurrent(Event* signal, TaskQueue* queue) {
tommic06b1332016-05-14 11:31:40 -070051 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";
tommi8c80c6e2017-02-23 00:34:52 -080066 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070067 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
nisse2c7b7a62017-09-04 05:18:21 -070073 queue.PostTask(Bind(&CheckCurrent, &event, &queue));
tommic06b1332016-05-14 11:31:40 -070074 EXPECT_TRUE(event.Wait(1000));
75}
76
77TEST(TaskQueueTest, PostCustomTask) {
78 static const char kQueueName[] = "PostCustomImplementation";
tommic06b1332016-05-14 11:31:40 -070079 Event event(false, false);
tommi8c80c6e2017-02-23 00:34:52 -080080 TaskQueue queue(kQueueName);
tommic06b1332016-05-14 11:31:40 -070081
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";
tommi8c80c6e2017-02-23 00:34:52 -0800102 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700103 TaskQueue queue(kQueueName);
104
tommic06b1332016-05-14 11:31:40 -0700105 queue.PostTask([&event]() { event.Set(); });
106 EXPECT_TRUE(event.Wait(1000));
107}
108
tommiede07592017-02-27 07:16:10 -0800109TEST(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
tommic06b1332016-05-14 11:31:40 -0700118TEST(TaskQueueTest, PostFromQueue) {
119 static const char kQueueName[] = "PostFromQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800120 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700121 TaskQueue queue(kQueueName);
122
tommic06b1332016-05-14 11:31:40 -0700123 queue.PostTask(
124 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
125 EXPECT_TRUE(event.Wait(1000));
126}
127
tommic5b435d2016-10-31 02:17:11 -0700128TEST(TaskQueueTest, PostDelayed) {
tommic06b1332016-05-14 11:31:40 -0700129 static const char kQueueName[] = "PostDelayed";
tommi8c80c6e2017-02-23 00:34:52 -0800130 Event event(false, false);
tommi5bdee472017-03-03 05:20:12 -0800131 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
tommic06b1332016-05-14 11:31:40 -0700132
tommic06b1332016-05-14 11:31:40 -0700133 uint32_t start = Time();
nisse2c7b7a62017-09-04 05:18:21 -0700134 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommic06b1332016-05-14 11:31:40 -0700135 EXPECT_TRUE(event.Wait(1000));
136 uint32_t end = Time();
tommic5b435d2016-10-31 02:17:11 -0700137 // These tests are a little relaxed due to how "powerful" our test bots can
tommi67fcad82016-11-16 10:50:24 -0800138 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommic5b435d2016-10-31 02:17:11 -0700139 // which is why we have a little bit of leeway backwards as well.
tommi67fcad82016-11-16 10:50:24 -0800140 EXPECT_GE(end - start, 90u);
141 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommic06b1332016-05-14 11:31:40 -0700142}
143
tommi0b942152017-03-10 09:33:53 -0800144// 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();
nisse2c7b7a62017-09-04 05:18:21 -0700154 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3);
tommi0b942152017-03-10 09:33:53 -0800155 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
tommic06b1332016-05-14 11:31:40 -0700164TEST(TaskQueueTest, PostMultipleDelayed) {
165 static const char kQueueName[] = "PostMultipleDelayed";
166 TaskQueue queue(kQueueName);
167
168 std::vector<std::unique_ptr<Event>> events;
tommif9d91542017-02-17 02:47:11 -0800169 for (int i = 0; i < 100; ++i) {
tommic06b1332016-05-14 11:31:40 -0700170 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
171 queue.PostDelayedTask(
nisse2c7b7a62017-09-04 05:18:21 -0700172 Bind(&CheckCurrent, events.back().get(), &queue), i);
tommic06b1332016-05-14 11:31:40 -0700173 }
174
175 for (const auto& e : events)
tommif9d91542017-02-17 02:47:11 -0800176 EXPECT_TRUE(e->Wait(1000));
tommic06b1332016-05-14 11:31:40 -0700177}
178
179TEST(TaskQueueTest, PostDelayedAfterDestruct) {
180 static const char kQueueName[] = "PostDelayedAfterDestruct";
181 Event event(false, false);
182 {
183 TaskQueue queue(kQueueName);
nisse2c7b7a62017-09-04 05:18:21 -0700184 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommic06b1332016-05-14 11:31:40 -0700185 }
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";
tommi8c80c6e2017-02-23 00:34:52 -0800192 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700193 TaskQueue post_queue(kPostQueue);
194 TaskQueue reply_queue(kReplyQueue);
195
tommic06b1332016-05-14 11:31:40 -0700196 post_queue.PostTaskAndReply(
nisse2c7b7a62017-09-04 05:18:21 -0700197 Bind(&CheckCurrent, nullptr, &post_queue),
198 Bind(&CheckCurrent, &event, &reply_queue), &reply_queue);
tommic06b1332016-05-14 11:31:40 -0700199 EXPECT_TRUE(event.Wait(1000));
200}
201
202TEST(TaskQueueTest, PostAndReuse) {
203 static const char kPostQueue[] = "PostQueue";
204 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800205 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700206 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
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200242 std::unique_ptr<ReusedTask> task(
tommic06b1332016-05-14 11:31:40 -0700243 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";
tommi8c80c6e2017-02-23 00:34:52 -0800252 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700253 TaskQueue post_queue(kPostQueue);
254 TaskQueue reply_queue(kReplyQueue);
255
tommic06b1332016-05-14 11:31:40 -0700256 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
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200263TEST(TaskQueueTest, PostCopyableClosure) {
264 struct CopyableClosure {
265 CopyableClosure(int* num_copies, int* num_moves, Event* event)
266 : num_copies(num_copies), num_moves(num_moves), event(event) {}
267 CopyableClosure(const CopyableClosure& other)
268 : num_copies(other.num_copies),
269 num_moves(other.num_moves),
270 event(other.event) {
271 ++*num_copies;
272 }
273 CopyableClosure(CopyableClosure&& other)
274 : num_copies(other.num_copies),
275 num_moves(other.num_moves),
276 event(other.event) {
277 ++*num_moves;
278 }
279 void operator()() { event->Set(); }
280
281 int* num_copies;
282 int* num_moves;
283 Event* event;
284 };
285
286 int num_copies = 0;
287 int num_moves = 0;
288 Event event(false, false);
289
290 static const char kPostQueue[] = "PostCopyableClosure";
291 TaskQueue post_queue(kPostQueue);
292 {
293 CopyableClosure closure(&num_copies, &num_moves, &event);
294 post_queue.PostTask(closure);
295 // Destroy closure to check with msan and tsan posted task has own copy.
296 }
297
298 EXPECT_TRUE(event.Wait(1000));
299 EXPECT_EQ(num_copies, 1);
300 EXPECT_EQ(num_moves, 0);
301}
302
303TEST(TaskQueueTest, PostMoveOnlyClosure) {
304 struct SomeState {
305 explicit SomeState(Event* event) : event(event) {}
306 ~SomeState() { event->Set(); }
307 Event* event;
308 };
309 struct MoveOnlyClosure {
310 MoveOnlyClosure(int* num_moves, std::unique_ptr<SomeState> state)
311 : num_moves(num_moves), state(std::move(state)) {}
312 MoveOnlyClosure(const MoveOnlyClosure&) = delete;
313 MoveOnlyClosure(MoveOnlyClosure&& other)
314 : num_moves(other.num_moves), state(std::move(other.state)) {
315 ++*num_moves;
316 }
317 void operator()() { state.reset(); }
318
319 int* num_moves;
320 std::unique_ptr<SomeState> state;
321 };
322
323 int num_moves = 0;
324 Event event(false, false);
325 std::unique_ptr<SomeState> state(new SomeState(&event));
326
327 static const char kPostQueue[] = "PostMoveOnlyClosure";
328 TaskQueue post_queue(kPostQueue);
329 post_queue.PostTask(MoveOnlyClosure(&num_moves, std::move(state)));
330
331 EXPECT_TRUE(event.Wait(1000));
332 EXPECT_EQ(num_moves, 1);
333}
334
335TEST(TaskQueueTest, PostMoveOnlyCleanup) {
336 struct SomeState {
337 explicit SomeState(Event* event) : event(event) {}
338 ~SomeState() { event->Set(); }
339 Event* event;
340 };
341 struct MoveOnlyClosure {
342 void operator()() { state.reset(); }
343
344 std::unique_ptr<SomeState> state;
345 };
346
347 Event event_run(false, false);
348 Event event_cleanup(false, false);
349 std::unique_ptr<SomeState> state_run(new SomeState(&event_run));
350 std::unique_ptr<SomeState> state_cleanup(new SomeState(&event_cleanup));
351
352 static const char kPostQueue[] = "PostMoveOnlyCleanup";
353 TaskQueue post_queue(kPostQueue);
354 post_queue.PostTask(NewClosure(MoveOnlyClosure{std::move(state_run)},
355 MoveOnlyClosure{std::move(state_cleanup)}));
356
357 EXPECT_TRUE(event_cleanup.Wait(1000));
358 // Expect run closure to complete before cleanup closure.
359 EXPECT_TRUE(event_run.Wait(0));
360}
361
tommi8c80c6e2017-02-23 00:34:52 -0800362// This test covers a particular bug that we had in the libevent implementation
363// where we could hit a deadlock while trying to post a reply task to a queue
364// that was being deleted. The test isn't guaranteed to hit that case but it's
365// written in a way that makes it likely and by running with --gtest_repeat=1000
366// the bug would occur. Alas, now it should be fixed.
367TEST(TaskQueueTest, PostAndReplyDeadlock) {
368 Event event(false, false);
369 TaskQueue post_queue("PostQueue");
370 TaskQueue reply_queue("ReplyQueue");
371
372 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
373 &reply_queue);
374 EXPECT_TRUE(event.Wait(1000));
375}
376
tommic06b1332016-05-14 11:31:40 -0700377void TestPostTaskAndReply(TaskQueue* work_queue,
tommic06b1332016-05-14 11:31:40 -0700378 Event* event) {
379 ASSERT_FALSE(work_queue->IsCurrent());
380 work_queue->PostTaskAndReply(
nisse2c7b7a62017-09-04 05:18:21 -0700381 Bind(&CheckCurrent, nullptr, work_queue),
tommic06b1332016-05-14 11:31:40 -0700382 NewClosure([event]() { event->Set(); }));
383}
384
385// Does a PostTaskAndReply from within a task to post and reply to the current
386// queue. All in all there will be 3 tasks posted and run.
387TEST(TaskQueueTest, PostAndReply2) {
388 static const char kQueueName[] = "PostAndReply2";
389 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi8c80c6e2017-02-23 00:34:52 -0800390 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700391 TaskQueue queue(kQueueName);
392 TaskQueue work_queue(kWorkQueueName);
393
tommic06b1332016-05-14 11:31:40 -0700394 queue.PostTask(
nisse2c7b7a62017-09-04 05:18:21 -0700395 Bind(&TestPostTaskAndReply, &work_queue, &event));
tommic06b1332016-05-14 11:31:40 -0700396 EXPECT_TRUE(event.Wait(1000));
397}
398
399// Tests posting more messages than a queue can queue up.
400// In situations like that, tasks will get dropped.
401TEST(TaskQueueTest, PostALot) {
402 // To destruct the event after the queue has gone out of scope.
403 Event event(false, false);
404
405 int tasks_executed = 0;
406 int tasks_cleaned_up = 0;
407 static const int kTaskCount = 0xffff;
408
409 {
410 static const char kQueueName[] = "PostALot";
411 TaskQueue queue(kQueueName);
412
413 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
414 // So here we post a total of 0xffff+1 messages, which triggers a failure
415 // case inside of the libevent queue implementation.
416
417 queue.PostTask([&event]() { event.Wait(Event::kForever); });
418 for (int i = 0; i < kTaskCount; ++i)
419 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
420 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
421 event.Set(); // Unblock the first task.
422 }
423
424 EXPECT_GE(tasks_cleaned_up, tasks_executed);
425 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700426}
427
428} // namespace rtc