blob: 08123d073cef50fe0d936122fa5d6e4b1237733d [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
11#include <memory>
12#include <vector>
13
14#include "webrtc/base/bind.h"
15#include "webrtc/base/event.h"
16#include "webrtc/base/gunit.h"
17#include "webrtc/base/task_queue.h"
18#include "webrtc/base/timeutils.h"
19
20namespace rtc {
21
22namespace {
23void CheckCurrent(const char* expected_queue, Event* signal, TaskQueue* queue) {
24 EXPECT_TRUE(TaskQueue::IsCurrent(expected_queue));
25 EXPECT_TRUE(queue->IsCurrent());
26 if (signal)
27 signal->Set();
28}
29
30} // namespace
31
32TEST(TaskQueueTest, Construct) {
33 static const char kQueueName[] = "Construct";
34 TaskQueue queue(kQueueName);
35 EXPECT_FALSE(queue.IsCurrent());
36}
37
38TEST(TaskQueueTest, PostAndCheckCurrent) {
39 static const char kQueueName[] = "PostAndCheckCurrent";
40 TaskQueue queue(kQueueName);
41
42 // We're not running a task, so there shouldn't be a current queue.
43 EXPECT_FALSE(queue.IsCurrent());
44 EXPECT_FALSE(TaskQueue::Current());
45
46 Event event(false, false);
47 queue.PostTask(Bind(&CheckCurrent, kQueueName, &event, &queue));
48 EXPECT_TRUE(event.Wait(1000));
49}
50
51TEST(TaskQueueTest, PostCustomTask) {
52 static const char kQueueName[] = "PostCustomImplementation";
53 TaskQueue queue(kQueueName);
54
55 Event event(false, false);
56
57 class CustomTask : public QueuedTask {
58 public:
59 explicit CustomTask(Event* event) : event_(event) {}
60
61 private:
62 bool Run() override {
63 event_->Set();
64 return false; // Never allows the task to be deleted by the queue.
65 }
66
67 Event* const event_;
68 } my_task(&event);
69
70 // Please don't do this in production code! :)
71 queue.PostTask(std::unique_ptr<QueuedTask>(&my_task));
72 EXPECT_TRUE(event.Wait(1000));
73}
74
75TEST(TaskQueueTest, PostLambda) {
76 static const char kQueueName[] = "PostLambda";
77 TaskQueue queue(kQueueName);
78
79 Event event(false, false);
80 queue.PostTask([&event]() { event.Set(); });
81 EXPECT_TRUE(event.Wait(1000));
82}
83
84TEST(TaskQueueTest, PostFromQueue) {
85 static const char kQueueName[] = "PostFromQueue";
86 TaskQueue queue(kQueueName);
87
88 Event event(false, false);
89 queue.PostTask(
90 [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); });
91 EXPECT_TRUE(event.Wait(1000));
92}
93
tommic5b435d2016-10-31 02:17:11 -070094TEST(TaskQueueTest, PostDelayed) {
tommic06b1332016-05-14 11:31:40 -070095 static const char kQueueName[] = "PostDelayed";
96 TaskQueue queue(kQueueName);
97
98 Event event(false, false);
99 uint32_t start = Time();
100 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
101 EXPECT_TRUE(event.Wait(1000));
102 uint32_t end = Time();
tommic5b435d2016-10-31 02:17:11 -0700103 // These tests are a little relaxed due to how "powerful" our test bots can
tommi67fcad82016-11-16 10:50:24 -0800104 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
tommic5b435d2016-10-31 02:17:11 -0700105 // which is why we have a little bit of leeway backwards as well.
tommi67fcad82016-11-16 10:50:24 -0800106 EXPECT_GE(end - start, 90u);
107 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290.
tommic06b1332016-05-14 11:31:40 -0700108}
109
110TEST(TaskQueueTest, PostMultipleDelayed) {
111 static const char kQueueName[] = "PostMultipleDelayed";
112 TaskQueue queue(kQueueName);
113
114 std::vector<std::unique_ptr<Event>> events;
115 for (int i = 0; i < 10; ++i) {
116 events.push_back(std::unique_ptr<Event>(new Event(false, false)));
117 queue.PostDelayedTask(
118 Bind(&CheckCurrent, kQueueName, events.back().get(), &queue), 10);
119 }
120
121 for (const auto& e : events)
122 EXPECT_TRUE(e->Wait(100));
123}
124
125TEST(TaskQueueTest, PostDelayedAfterDestruct) {
126 static const char kQueueName[] = "PostDelayedAfterDestruct";
127 Event event(false, false);
128 {
129 TaskQueue queue(kQueueName);
130 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100);
131 }
132 EXPECT_FALSE(event.Wait(200)); // Task should not run.
133}
134
135TEST(TaskQueueTest, PostAndReply) {
136 static const char kPostQueue[] = "PostQueue";
137 static const char kReplyQueue[] = "ReplyQueue";
138 TaskQueue post_queue(kPostQueue);
139 TaskQueue reply_queue(kReplyQueue);
140
141 Event event(false, false);
142 post_queue.PostTaskAndReply(
143 Bind(&CheckCurrent, kPostQueue, nullptr, &post_queue),
144 Bind(&CheckCurrent, kReplyQueue, &event, &reply_queue), &reply_queue);
145 EXPECT_TRUE(event.Wait(1000));
146}
147
148TEST(TaskQueueTest, PostAndReuse) {
149 static const char kPostQueue[] = "PostQueue";
150 static const char kReplyQueue[] = "ReplyQueue";
151 TaskQueue post_queue(kPostQueue);
152 TaskQueue reply_queue(kReplyQueue);
153
154 int call_count = 0;
155
156 class ReusedTask : public QueuedTask {
157 public:
158 ReusedTask(int* counter, TaskQueue* reply_queue, Event* event)
159 : counter_(counter), reply_queue_(reply_queue), event_(event) {
160 EXPECT_EQ(0, *counter_);
161 }
162
163 private:
164 bool Run() override {
165 if (++(*counter_) == 1) {
166 std::unique_ptr<QueuedTask> myself(this);
167 reply_queue_->PostTask(std::move(myself));
168 // At this point, the object is owned by reply_queue_ and it's
169 // theoratically possible that the object has been deleted (e.g. if
170 // posting wasn't possible). So, don't touch any member variables here.
171
172 // Indicate to the current queue that ownership has been transferred.
173 return false;
174 } else {
175 EXPECT_EQ(2, *counter_);
176 EXPECT_TRUE(reply_queue_->IsCurrent());
177 event_->Set();
178 return true; // Indicate that the object should be deleted.
179 }
180 }
181
182 int* const counter_;
183 TaskQueue* const reply_queue_;
184 Event* const event_;
185 };
186
187 Event event(false, false);
188 std::unique_ptr<QueuedTask> task(
189 new ReusedTask(&call_count, &reply_queue, &event));
190
191 post_queue.PostTask(std::move(task));
192 EXPECT_TRUE(event.Wait(1000));
193}
194
195TEST(TaskQueueTest, PostAndReplyLambda) {
196 static const char kPostQueue[] = "PostQueue";
197 static const char kReplyQueue[] = "ReplyQueue";
198 TaskQueue post_queue(kPostQueue);
199 TaskQueue reply_queue(kReplyQueue);
200
201 Event event(false, false);
202 bool my_flag = false;
203 post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; },
204 [&event]() { event.Set(); }, &reply_queue);
205 EXPECT_TRUE(event.Wait(1000));
206 EXPECT_TRUE(my_flag);
207}
208
209void TestPostTaskAndReply(TaskQueue* work_queue,
210 const char* work_queue_name,
211 Event* event) {
212 ASSERT_FALSE(work_queue->IsCurrent());
213 work_queue->PostTaskAndReply(
214 Bind(&CheckCurrent, work_queue_name, nullptr, work_queue),
215 NewClosure([event]() { event->Set(); }));
216}
217
218// Does a PostTaskAndReply from within a task to post and reply to the current
219// queue. All in all there will be 3 tasks posted and run.
220TEST(TaskQueueTest, PostAndReply2) {
221 static const char kQueueName[] = "PostAndReply2";
222 static const char kWorkQueueName[] = "PostAndReply2_Worker";
223 TaskQueue queue(kQueueName);
224 TaskQueue work_queue(kWorkQueueName);
225
226 Event event(false, false);
227 queue.PostTask(
228 Bind(&TestPostTaskAndReply, &work_queue, kWorkQueueName, &event));
229 EXPECT_TRUE(event.Wait(1000));
230}
231
232// Tests posting more messages than a queue can queue up.
233// In situations like that, tasks will get dropped.
234TEST(TaskQueueTest, PostALot) {
235 // To destruct the event after the queue has gone out of scope.
236 Event event(false, false);
237
238 int tasks_executed = 0;
239 int tasks_cleaned_up = 0;
240 static const int kTaskCount = 0xffff;
241
242 {
243 static const char kQueueName[] = "PostALot";
244 TaskQueue queue(kQueueName);
245
246 // On linux, the limit of pending bytes in the pipe buffer is 0xffff.
247 // So here we post a total of 0xffff+1 messages, which triggers a failure
248 // case inside of the libevent queue implementation.
249
250 queue.PostTask([&event]() { event.Wait(Event::kForever); });
251 for (int i = 0; i < kTaskCount; ++i)
252 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; },
253 [&tasks_cleaned_up]() { ++tasks_cleaned_up; }));
254 event.Set(); // Unblock the first task.
255 }
256
257 EXPECT_GE(tasks_cleaned_up, tasks_executed);
258 EXPECT_EQ(kTaskCount, tasks_cleaned_up);
tommic06b1332016-05-14 11:31:40 -0700259}
260
261} // namespace rtc