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