blob: cedf68e2e851359c505e4c34b14b9cd89bdaee63 [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"
Tommi68561562018-02-13 19:47:50 +010024#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/timeutils.h"
tommic06b1332016-05-14 11:31:40 -070026
Tommi68561562018-02-13 19:47:50 +010027using rtc::test::TaskQueueForTest;
28
tommic06b1332016-05-14 11:31:40 -070029namespace rtc {
Tommi68561562018-02-13 19:47:50 +010030
tommi0b942152017-03-10 09:33:53 -080031namespace {
32// Noop on all platforms except Windows, where it turns on high precision
33// multimedia timers which increases the precision of TimeMillis() while in
34// scope.
35class EnableHighResTimers {
36 public:
37#if !defined(WEBRTC_WIN)
38 EnableHighResTimers() {}
39#else
40 EnableHighResTimers() : enabled_(timeBeginPeriod(1) == TIMERR_NOERROR) {}
41 ~EnableHighResTimers() {
42 if (enabled_)
43 timeEndPeriod(1);
44 }
45
46 private:
47 const bool enabled_;
48#endif
49};
tommic06b1332016-05-14 11:31:40 -070050
nisse2c7b7a62017-09-04 05:18:21 -070051void CheckCurrent(Event* signal, TaskQueue* queue) {
tommic06b1332016-05-14 11:31:40 -070052 EXPECT_TRUE(queue->IsCurrent());
53 if (signal)
54 signal->Set();
55}
56
57} // namespace
58
59TEST(TaskQueueTest, Construct) {
60 static const char kQueueName[] = "Construct";
61 TaskQueue queue(kQueueName);
62 EXPECT_FALSE(queue.IsCurrent());
63}
64
65TEST(TaskQueueTest, PostAndCheckCurrent) {
66 static const char kQueueName[] = "PostAndCheckCurrent";
tommi8c80c6e2017-02-23 00:34:52 -080067 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -070068 TaskQueue queue(kQueueName);
69
70 // We're not running a task, so there shouldn't be a current queue.
71 EXPECT_FALSE(queue.IsCurrent());
72 EXPECT_FALSE(TaskQueue::Current());
73
nisse2c7b7a62017-09-04 05:18:21 -070074 queue.PostTask(Bind(&CheckCurrent, &event, &queue));
tommic06b1332016-05-14 11:31:40 -070075 EXPECT_TRUE(event.Wait(1000));
76}
77
78TEST(TaskQueueTest, PostCustomTask) {
79 static const char kQueueName[] = "PostCustomImplementation";
Tommi68561562018-02-13 19:47:50 +010080 TaskQueueForTest queue(kQueueName);
tommic06b1332016-05-14 11:31:40 -070081
82 class CustomTask : public QueuedTask {
83 public:
Tommi68561562018-02-13 19:47:50 +010084 CustomTask() {}
85 bool ran() const { return ran_; }
tommic06b1332016-05-14 11:31:40 -070086
87 private:
88 bool Run() override {
Tommi68561562018-02-13 19:47:50 +010089 ran_ = true;
90 return false; // Never allow the task to be deleted by the queue.
tommic06b1332016-05-14 11:31:40 -070091 }
92
Tommi68561562018-02-13 19:47:50 +010093 bool ran_ = false;
94 } my_task;
tommic06b1332016-05-14 11:31:40 -070095
Tommi68561562018-02-13 19:47:50 +010096 queue.SendTask(&my_task);
97 EXPECT_TRUE(my_task.ran());
tommic06b1332016-05-14 11:31:40 -070098}
99
100TEST(TaskQueueTest, PostLambda) {
Tommi68561562018-02-13 19:47:50 +0100101 TaskQueueForTest queue("PostLambda");
102 bool ran = false;
103 queue.SendTask([&ran]() { ran = true; });
104 EXPECT_TRUE(ran);
tommic06b1332016-05-14 11:31:40 -0700105}
106
tommiede07592017-02-27 07:16:10 -0800107TEST(TaskQueueTest, PostDelayedZero) {
108 static const char kQueueName[] = "PostDelayedZero";
109 Event event(false, false);
110 TaskQueue queue(kQueueName);
111
112 queue.PostDelayedTask([&event]() { event.Set(); }, 0);
113 EXPECT_TRUE(event.Wait(1000));
114}
115
tommic06b1332016-05-14 11:31:40 -0700116TEST(TaskQueueTest, PostFromQueue) {
117 static const char kQueueName[] = "PostFromQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800118 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700119 TaskQueue queue(kQueueName);
120
tommic06b1332016-05-14 11:31:40 -0700121 queue.PostTask(
122 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
123 EXPECT_TRUE(event.Wait(1000));
124}
125
tommic5b435d2016-10-31 02:17:11 -0700126TEST(TaskQueueTest, PostDelayed) {
tommic06b1332016-05-14 11:31:40 -0700127 static const char kQueueName[] = "PostDelayed";
tommi8c80c6e2017-02-23 00:34:52 -0800128 Event event(false, false);
tommi5bdee472017-03-03 05:20:12 -0800129 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
tommic06b1332016-05-14 11:31:40 -0700130
tommic06b1332016-05-14 11:31:40 -0700131 uint32_t start = Time();
nisse2c7b7a62017-09-04 05:18:21 -0700132 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommic06b1332016-05-14 11:31:40 -0700133 EXPECT_TRUE(event.Wait(1000));
134 uint32_t end = Time();
tommic5b435d2016-10-31 02:17:11 -0700135 // These tests are a little relaxed due to how "powerful" our test bots can
tommi67fcad82016-11-16 10:50:24 -0800136 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommic5b435d2016-10-31 02:17:11 -0700137 // which is why we have a little bit of leeway backwards as well.
tommi67fcad82016-11-16 10:50:24 -0800138 EXPECT_GE(end - start, 90u);
139 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommic06b1332016-05-14 11:31:40 -0700140}
141
tommi0b942152017-03-10 09:33:53 -0800142// This task needs to be run manually due to the slowness of some of our bots.
143// TODO(tommi): Can we run this on the perf bots?
144TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) {
145 EnableHighResTimers high_res_scope;
146
147 static const char kQueueName[] = "PostDelayedHighRes";
148 Event event(false, false);
149 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH);
150
151 uint32_t start = Time();
nisse2c7b7a62017-09-04 05:18:21 -0700152 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3);
tommi0b942152017-03-10 09:33:53 -0800153 EXPECT_TRUE(event.Wait(1000));
154 uint32_t end = TimeMillis();
155 // These tests are a little relaxed due to how "powerful" our test bots can
156 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
157 // which is why we have a little bit of leeway backwards as well.
158 EXPECT_GE(end - start, 3u);
159 EXPECT_NEAR(end - start, 3, 3u);
160}
161
tommic06b1332016-05-14 11:31:40 -0700162TEST(TaskQueueTest, PostMultipleDelayed) {
163 static const char kQueueName[] = "PostMultipleDelayed";
164 TaskQueue queue(kQueueName);
165
166 std::vector<std::unique_ptr<Event>> events;
tommif9d91542017-02-17 02:47:11 -0800167 for (int i = 0; i < 100; ++i) {
tommic06b1332016-05-14 11:31:40 -0700168 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
Yves Gerey665174f2018-06-19 15:03:05 +0200169 queue.PostDelayedTask(Bind(&CheckCurrent, events.back().get(), &queue), i);
tommic06b1332016-05-14 11:31:40 -0700170 }
171
172 for (const auto& e : events)
tommif9d91542017-02-17 02:47:11 -0800173 EXPECT_TRUE(e->Wait(1000));
tommic06b1332016-05-14 11:31:40 -0700174}
175
176TEST(TaskQueueTest, PostDelayedAfterDestruct) {
177 static const char kQueueName[] = "PostDelayedAfterDestruct";
Danil Chapovalova36631c2018-09-24 20:27:22 +0200178 Event run(false, false);
179 Event deleted(false, false);
tommic06b1332016-05-14 11:31:40 -0700180 {
181 TaskQueue queue(kQueueName);
Danil Chapovalova36631c2018-09-24 20:27:22 +0200182 queue.PostDelayedTask(
183 rtc::NewClosure([&run] { run.Set(); }, [&deleted] { deleted.Set(); }),
184 100);
tommic06b1332016-05-14 11:31:40 -0700185 }
Danil Chapovalova36631c2018-09-24 20:27:22 +0200186 // Task might outlive the TaskQueue, but still should be deleted.
187 EXPECT_TRUE(deleted.Wait(200));
188 EXPECT_FALSE(run.Wait(0)); // and should not run.
tommic06b1332016-05-14 11:31:40 -0700189}
190
191TEST(TaskQueueTest, PostAndReply) {
192 static const char kPostQueue[] = "PostQueue";
193 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800194 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700195 TaskQueue post_queue(kPostQueue);
196 TaskQueue reply_queue(kReplyQueue);
197
Yves Gerey665174f2018-06-19 15:03:05 +0200198 post_queue.PostTaskAndReply(Bind(&CheckCurrent, nullptr, &post_queue),
199 Bind(&CheckCurrent, &event, &reply_queue),
200 &reply_queue);
tommic06b1332016-05-14 11:31:40 -0700201 EXPECT_TRUE(event.Wait(1000));
202}
203
204TEST(TaskQueueTest, PostAndReuse) {
205 static const char kPostQueue[] = "PostQueue";
206 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800207 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700208 TaskQueue post_queue(kPostQueue);
209 TaskQueue reply_queue(kReplyQueue);
210
211 int call_count = 0;
212
213 class ReusedTask : public QueuedTask {
214 public:
215 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
216 : counter_(counter), reply_queue_(reply_queue), event_(event) {
217 EXPECT_EQ(0, *counter_);
218 }
219
220 private:
221 bool Run() override {
222 if (++(*counter_) == 1) {
223 std::unique_ptr<QueuedTask> myself(this);
224 reply_queue_->PostTask(std::move(myself));
225 // At this point, the object is owned by reply_queue_ and it's
226 // theoratically possible that the object has been deleted (e.g. if
227 // posting wasn't possible). So, don't touch any member variables here.
228
229 // Indicate to the current queue that ownership has been transferred.
230 return false;
231 } else {
232 EXPECT_EQ(2, *counter_);
233 EXPECT_TRUE(reply_queue_->IsCurrent());
234 event_->Set();
235 return true; // Indicate that the object should be deleted.
236 }
237 }
238
239 int* const counter_;
240 TaskQueue* const reply_queue_;
241 Event* const event_;
242 };
243
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200244 std::unique_ptr<ReusedTask> task(
tommic06b1332016-05-14 11:31:40 -0700245 new ReusedTask(&call_count, &reply_queue, &event));
246
247 post_queue.PostTask(std::move(task));
248 EXPECT_TRUE(event.Wait(1000));
249}
250
251TEST(TaskQueueTest, PostAndReplyLambda) {
252 static const char kPostQueue[] = "PostQueue";
253 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800254 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700255 TaskQueue post_queue(kPostQueue);
256 TaskQueue reply_queue(kReplyQueue);
257
tommic06b1332016-05-14 11:31:40 -0700258 bool my_flag = false;
259 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
260 [&event]() { event.Set(); }, &reply_queue);
261 EXPECT_TRUE(event.Wait(1000));
262 EXPECT_TRUE(my_flag);
263}
264
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200265TEST(TaskQueueTest, PostCopyableClosure) {
266 struct CopyableClosure {
267 CopyableClosure(int* num_copies, int* num_moves, Event* event)
268 : num_copies(num_copies), num_moves(num_moves), event(event) {}
269 CopyableClosure(const CopyableClosure& other)
270 : num_copies(other.num_copies),
271 num_moves(other.num_moves),
272 event(other.event) {
273 ++*num_copies;
274 }
275 CopyableClosure(CopyableClosure&& other)
276 : num_copies(other.num_copies),
277 num_moves(other.num_moves),
278 event(other.event) {
279 ++*num_moves;
280 }
281 void operator()() { event->Set(); }
282
283 int* num_copies;
284 int* num_moves;
285 Event* event;
286 };
287
288 int num_copies = 0;
289 int num_moves = 0;
290 Event event(false, false);
291
292 static const char kPostQueue[] = "PostCopyableClosure";
293 TaskQueue post_queue(kPostQueue);
294 {
295 CopyableClosure closure(&num_copies, &num_moves, &event);
296 post_queue.PostTask(closure);
297 // Destroy closure to check with msan and tsan posted task has own copy.
298 }
299
300 EXPECT_TRUE(event.Wait(1000));
301 EXPECT_EQ(num_copies, 1);
302 EXPECT_EQ(num_moves, 0);
303}
304
305TEST(TaskQueueTest, PostMoveOnlyClosure) {
306 struct SomeState {
307 explicit SomeState(Event* event) : event(event) {}
308 ~SomeState() { event->Set(); }
309 Event* event;
310 };
311 struct MoveOnlyClosure {
312 MoveOnlyClosure(int* num_moves, std::unique_ptr<SomeState> state)
313 : num_moves(num_moves), state(std::move(state)) {}
314 MoveOnlyClosure(const MoveOnlyClosure&) = delete;
315 MoveOnlyClosure(MoveOnlyClosure&& other)
316 : num_moves(other.num_moves), state(std::move(other.state)) {
317 ++*num_moves;
318 }
319 void operator()() { state.reset(); }
320
321 int* num_moves;
322 std::unique_ptr<SomeState> state;
323 };
324
325 int num_moves = 0;
326 Event event(false, false);
327 std::unique_ptr<SomeState> state(new SomeState(&event));
328
329 static const char kPostQueue[] = "PostMoveOnlyClosure";
330 TaskQueue post_queue(kPostQueue);
331 post_queue.PostTask(MoveOnlyClosure(&num_moves, std::move(state)));
332
333 EXPECT_TRUE(event.Wait(1000));
334 EXPECT_EQ(num_moves, 1);
335}
336
337TEST(TaskQueueTest, PostMoveOnlyCleanup) {
338 struct SomeState {
339 explicit SomeState(Event* event) : event(event) {}
340 ~SomeState() { event->Set(); }
341 Event* event;
342 };
343 struct MoveOnlyClosure {
344 void operator()() { state.reset(); }
345
346 std::unique_ptr<SomeState> state;
347 };
348
349 Event event_run(false, false);
350 Event event_cleanup(false, false);
351 std::unique_ptr<SomeState> state_run(new SomeState(&event_run));
352 std::unique_ptr<SomeState> state_cleanup(new SomeState(&event_cleanup));
353
354 static const char kPostQueue[] = "PostMoveOnlyCleanup";
355 TaskQueue post_queue(kPostQueue);
356 post_queue.PostTask(NewClosure(MoveOnlyClosure{std::move(state_run)},
357 MoveOnlyClosure{std::move(state_cleanup)}));
358
359 EXPECT_TRUE(event_cleanup.Wait(1000));
360 // Expect run closure to complete before cleanup closure.
361 EXPECT_TRUE(event_run.Wait(0));
362}
363
tommi8c80c6e2017-02-23 00:34:52 -0800364// This test covers a particular bug that we had in the libevent implementation
365// where we could hit a deadlock while trying to post a reply task to a queue
366// that was being deleted. The test isn't guaranteed to hit that case but it's
367// written in a way that makes it likely and by running with --gtest_repeat=1000
368// the bug would occur. Alas, now it should be fixed.
369TEST(TaskQueueTest, PostAndReplyDeadlock) {
370 Event event(false, false);
371 TaskQueue post_queue("PostQueue");
372 TaskQueue reply_queue("ReplyQueue");
373
374 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
375 &reply_queue);
376 EXPECT_TRUE(event.Wait(1000));
377}
378
Danil Chapovalov29038882018-09-07 16:28:20 +0200379// http://bugs.webrtc.org/9728
380#if defined(WEBRTC_WIN)
381#define MAYBE_DeleteTaskQueueAfterPostAndReply \
382 DISABLED_DeleteTaskQueueAfterPostAndReply
383#else
384#define MAYBE_DeleteTaskQueueAfterPostAndReply DeleteTaskQueueAfterPostAndReply
385#endif
386TEST(TaskQueueTest, MAYBE_DeleteTaskQueueAfterPostAndReply) {
387 Event task_deleted(false, false);
388 Event reply_deleted(false, false);
389 auto* task_queue = new TaskQueue("Queue");
390
391 task_queue->PostTaskAndReply(
392 /*task=*/rtc::NewClosure(
393 /*closure=*/[] {},
394 /*cleanup=*/[&task_deleted] { task_deleted.Set(); }),
395 /*reply=*/rtc::NewClosure(
396 /*closure=*/[] {},
397 /*cleanup=*/[&reply_deleted] { reply_deleted.Set(); }));
398
399 delete task_queue;
400
401 EXPECT_TRUE(task_deleted.Wait(1000));
402 EXPECT_TRUE(reply_deleted.Wait(1000));
403}
404
Yves Gerey665174f2018-06-19 15:03:05 +0200405void TestPostTaskAndReply(TaskQueue* work_queue, Event* event) {
tommic06b1332016-05-14 11:31:40 -0700406 ASSERT_FALSE(work_queue->IsCurrent());
Yves Gerey665174f2018-06-19 15:03:05 +0200407 work_queue->PostTaskAndReply(Bind(&CheckCurrent, nullptr, work_queue),
408 NewClosure([event]() { event->Set(); }));
tommic06b1332016-05-14 11:31:40 -0700409}
410
411// Does a PostTaskAndReply from within a task to post and reply to the current
412// queue. All in all there will be 3 tasks posted and run.
413TEST(TaskQueueTest, PostAndReply2) {
414 static const char kQueueName[] = "PostAndReply2";
415 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi8c80c6e2017-02-23 00:34:52 -0800416 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700417 TaskQueue queue(kQueueName);
418 TaskQueue work_queue(kWorkQueueName);
419
Yves Gerey665174f2018-06-19 15:03:05 +0200420 queue.PostTask(Bind(&TestPostTaskAndReply, &work_queue, &event));
tommic06b1332016-05-14 11:31:40 -0700421 EXPECT_TRUE(event.Wait(1000));
422}
423
424// Tests posting more messages than a queue can queue up.
425// In situations like that, tasks will get dropped.
426TEST(TaskQueueTest, PostALot) {
427 // To destruct the event after the queue has gone out of scope.
428 Event event(false, false);
429
430 int tasks_executed = 0;
431 int tasks_cleaned_up = 0;
432 static const int kTaskCount = 0xffff;
433
434 {
435 static const char kQueueName[] = "PostALot";
436 TaskQueue queue(kQueueName);
437
438 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
439 // So here we post a total of 0xffff+1 messages, which triggers a failure
440 // case inside of the libevent queue implementation.
441
442 queue.PostTask([&event]() { event.Wait(Event::kForever); });
443 for (int i = 0; i < kTaskCount; ++i)
444 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
445 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
446 event.Set(); // Unblock the first task.
447 }
448
449 EXPECT_GE(tasks_cleaned_up, tasks_executed);
450 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700451}
452
453} // namespace rtc