blob: cdf0d59b046837365b7480e8ee58b7ddffcdae87 [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";
178 Event event(false, false);
179 {
180 TaskQueue queue(kQueueName);
nisse2c7b7a62017-09-04 05:18:21 -0700181 queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
tommic06b1332016-05-14 11:31:40 -0700182 }
183 EXPECT_FALSE(event.Wait(200)); // Task should not run.
184}
185
186TEST(TaskQueueTest, PostAndReply) {
187 static const char kPostQueue[] = "PostQueue";
188 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800189 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700190 TaskQueue post_queue(kPostQueue);
191 TaskQueue reply_queue(kReplyQueue);
192
Yves Gerey665174f2018-06-19 15:03:05 +0200193 post_queue.PostTaskAndReply(Bind(&CheckCurrent, nullptr, &post_queue),
194 Bind(&CheckCurrent, &event, &reply_queue),
195 &reply_queue);
tommic06b1332016-05-14 11:31:40 -0700196 EXPECT_TRUE(event.Wait(1000));
197}
198
199TEST(TaskQueueTest, PostAndReuse) {
200 static const char kPostQueue[] = "PostQueue";
201 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800202 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700203 TaskQueue post_queue(kPostQueue);
204 TaskQueue reply_queue(kReplyQueue);
205
206 int call_count = 0;
207
208 class ReusedTask : public QueuedTask {
209 public:
210 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
211 : counter_(counter), reply_queue_(reply_queue), event_(event) {
212 EXPECT_EQ(0, *counter_);
213 }
214
215 private:
216 bool Run() override {
217 if (++(*counter_) == 1) {
218 std::unique_ptr<QueuedTask> myself(this);
219 reply_queue_->PostTask(std::move(myself));
220 // At this point, the object is owned by reply_queue_ and it's
221 // theoratically possible that the object has been deleted (e.g. if
222 // posting wasn't possible). So, don't touch any member variables here.
223
224 // Indicate to the current queue that ownership has been transferred.
225 return false;
226 } else {
227 EXPECT_EQ(2, *counter_);
228 EXPECT_TRUE(reply_queue_->IsCurrent());
229 event_->Set();
230 return true; // Indicate that the object should be deleted.
231 }
232 }
233
234 int* const counter_;
235 TaskQueue* const reply_queue_;
236 Event* const event_;
237 };
238
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200239 std::unique_ptr<ReusedTask> task(
tommic06b1332016-05-14 11:31:40 -0700240 new ReusedTask(&call_count, &reply_queue, &event));
241
242 post_queue.PostTask(std::move(task));
243 EXPECT_TRUE(event.Wait(1000));
244}
245
246TEST(TaskQueueTest, PostAndReplyLambda) {
247 static const char kPostQueue[] = "PostQueue";
248 static const char kReplyQueue[] = "ReplyQueue";
tommi8c80c6e2017-02-23 00:34:52 -0800249 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700250 TaskQueue post_queue(kPostQueue);
251 TaskQueue reply_queue(kReplyQueue);
252
tommic06b1332016-05-14 11:31:40 -0700253 bool my_flag = false;
254 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
255 [&event]() { event.Set(); }, &reply_queue);
256 EXPECT_TRUE(event.Wait(1000));
257 EXPECT_TRUE(my_flag);
258}
259
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200260TEST(TaskQueueTest, PostCopyableClosure) {
261 struct CopyableClosure {
262 CopyableClosure(int* num_copies, int* num_moves, Event* event)
263 : num_copies(num_copies), num_moves(num_moves), event(event) {}
264 CopyableClosure(const CopyableClosure& other)
265 : num_copies(other.num_copies),
266 num_moves(other.num_moves),
267 event(other.event) {
268 ++*num_copies;
269 }
270 CopyableClosure(CopyableClosure&& other)
271 : num_copies(other.num_copies),
272 num_moves(other.num_moves),
273 event(other.event) {
274 ++*num_moves;
275 }
276 void operator()() { event->Set(); }
277
278 int* num_copies;
279 int* num_moves;
280 Event* event;
281 };
282
283 int num_copies = 0;
284 int num_moves = 0;
285 Event event(false, false);
286
287 static const char kPostQueue[] = "PostCopyableClosure";
288 TaskQueue post_queue(kPostQueue);
289 {
290 CopyableClosure closure(&num_copies, &num_moves, &event);
291 post_queue.PostTask(closure);
292 // Destroy closure to check with msan and tsan posted task has own copy.
293 }
294
295 EXPECT_TRUE(event.Wait(1000));
296 EXPECT_EQ(num_copies, 1);
297 EXPECT_EQ(num_moves, 0);
298}
299
300TEST(TaskQueueTest, PostMoveOnlyClosure) {
301 struct SomeState {
302 explicit SomeState(Event* event) : event(event) {}
303 ~SomeState() { event->Set(); }
304 Event* event;
305 };
306 struct MoveOnlyClosure {
307 MoveOnlyClosure(int* num_moves, std::unique_ptr<SomeState> state)
308 : num_moves(num_moves), state(std::move(state)) {}
309 MoveOnlyClosure(const MoveOnlyClosure&) = delete;
310 MoveOnlyClosure(MoveOnlyClosure&& other)
311 : num_moves(other.num_moves), state(std::move(other.state)) {
312 ++*num_moves;
313 }
314 void operator()() { state.reset(); }
315
316 int* num_moves;
317 std::unique_ptr<SomeState> state;
318 };
319
320 int num_moves = 0;
321 Event event(false, false);
322 std::unique_ptr<SomeState> state(new SomeState(&event));
323
324 static const char kPostQueue[] = "PostMoveOnlyClosure";
325 TaskQueue post_queue(kPostQueue);
326 post_queue.PostTask(MoveOnlyClosure(&num_moves, std::move(state)));
327
328 EXPECT_TRUE(event.Wait(1000));
329 EXPECT_EQ(num_moves, 1);
330}
331
332TEST(TaskQueueTest, PostMoveOnlyCleanup) {
333 struct SomeState {
334 explicit SomeState(Event* event) : event(event) {}
335 ~SomeState() { event->Set(); }
336 Event* event;
337 };
338 struct MoveOnlyClosure {
339 void operator()() { state.reset(); }
340
341 std::unique_ptr<SomeState> state;
342 };
343
344 Event event_run(false, false);
345 Event event_cleanup(false, false);
346 std::unique_ptr<SomeState> state_run(new SomeState(&event_run));
347 std::unique_ptr<SomeState> state_cleanup(new SomeState(&event_cleanup));
348
349 static const char kPostQueue[] = "PostMoveOnlyCleanup";
350 TaskQueue post_queue(kPostQueue);
351 post_queue.PostTask(NewClosure(MoveOnlyClosure{std::move(state_run)},
352 MoveOnlyClosure{std::move(state_cleanup)}));
353
354 EXPECT_TRUE(event_cleanup.Wait(1000));
355 // Expect run closure to complete before cleanup closure.
356 EXPECT_TRUE(event_run.Wait(0));
357}
358
tommi8c80c6e2017-02-23 00:34:52 -0800359// This test covers a particular bug that we had in the libevent implementation
360// where we could hit a deadlock while trying to post a reply task to a queue
361// that was being deleted. The test isn't guaranteed to hit that case but it's
362// written in a way that makes it likely and by running with --gtest_repeat=1000
363// the bug would occur. Alas, now it should be fixed.
364TEST(TaskQueueTest, PostAndReplyDeadlock) {
365 Event event(false, false);
366 TaskQueue post_queue("PostQueue");
367 TaskQueue reply_queue("ReplyQueue");
368
369 post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {},
370 &reply_queue);
371 EXPECT_TRUE(event.Wait(1000));
372}
373
Yves Gerey665174f2018-06-19 15:03:05 +0200374void TestPostTaskAndReply(TaskQueue* work_queue, Event* event) {
tommic06b1332016-05-14 11:31:40 -0700375 ASSERT_FALSE(work_queue->IsCurrent());
Yves Gerey665174f2018-06-19 15:03:05 +0200376 work_queue->PostTaskAndReply(Bind(&CheckCurrent, nullptr, work_queue),
377 NewClosure([event]() { event->Set(); }));
tommic06b1332016-05-14 11:31:40 -0700378}
379
380// Does a PostTaskAndReply from within a task to post and reply to the current
381// queue. All in all there will be 3 tasks posted and run.
382TEST(TaskQueueTest, PostAndReply2) {
383 static const char kQueueName[] = "PostAndReply2";
384 static const char kWorkQueueName[] = "PostAndReply2_Worker";
tommi8c80c6e2017-02-23 00:34:52 -0800385 Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700386 TaskQueue queue(kQueueName);
387 TaskQueue work_queue(kWorkQueueName);
388
Yves Gerey665174f2018-06-19 15:03:05 +0200389 queue.PostTask(Bind(&TestPostTaskAndReply, &work_queue, &event));
tommic06b1332016-05-14 11:31:40 -0700390 EXPECT_TRUE(event.Wait(1000));
391}
392
393// Tests posting more messages than a queue can queue up.
394// In situations like that, tasks will get dropped.
395TEST(TaskQueueTest, PostALot) {
396 // To destruct the event after the queue has gone out of scope.
397 Event event(false, false);
398
399 int tasks_executed = 0;
400 int tasks_cleaned_up = 0;
401 static const int kTaskCount = 0xffff;
402
403 {
404 static const char kQueueName[] = "PostALot";
405 TaskQueue queue(kQueueName);
406
407 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
408 // So here we post a total of 0xffff+1 messages, which triggers a failure
409 // case inside of the libevent queue implementation.
410
411 queue.PostTask([&event]() { event.Wait(Event::kForever); });
412 for (int i = 0; i < kTaskCount; ++i)
413 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
414 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
415 event.Set(); // Unblock the first task.
416 }
417
418 EXPECT_GE(tasks_cleaned_up, tasks_executed);
419 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700420}
421
422} // namespace rtc