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