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 | |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 11 | #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 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <vector> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/bind.h" |
| 22 | #include "rtc_base/event.h" |
| 23 | #include "rtc_base/gunit.h" |
| 24 | #include "rtc_base/task_queue.h" |
| 25 | #include "rtc_base/timeutils.h" |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 26 | |
| 27 | namespace rtc { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | // Noop on all platforms except Windows, where it turns on high precision |
| 30 | // multimedia timers which increases the precision of TimeMillis() while in |
| 31 | // scope. |
| 32 | class EnableHighResTimers { |
| 33 | public: |
| 34 | #if !defined(WEBRTC_WIN) |
| 35 | EnableHighResTimers() {} |
| 36 | #else |
| 37 | EnableHighResTimers() : enabled_(timeBeginPeriod(1) == TIMERR_NOERROR) {} |
| 38 | ~EnableHighResTimers() { |
| 39 | if (enabled_) |
| 40 | timeEndPeriod(1); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | const bool enabled_; |
| 45 | #endif |
| 46 | }; |
| 47 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 48 | |
| 49 | namespace { |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 50 | void CheckCurrent(Event* signal, TaskQueue* queue) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 51 | EXPECT_TRUE(queue->IsCurrent()); |
| 52 | if (signal) |
| 53 | signal->Set(); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | TEST(TaskQueueTest, Construct) { |
| 59 | static const char kQueueName[] = "Construct"; |
| 60 | TaskQueue queue(kQueueName); |
| 61 | EXPECT_FALSE(queue.IsCurrent()); |
| 62 | } |
| 63 | |
| 64 | TEST(TaskQueueTest, PostAndCheckCurrent) { |
| 65 | static const char kQueueName[] = "PostAndCheckCurrent"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 66 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 67 | TaskQueue queue(kQueueName); |
| 68 | |
| 69 | // We're not running a task, so there shouldn't be a current queue. |
| 70 | EXPECT_FALSE(queue.IsCurrent()); |
| 71 | EXPECT_FALSE(TaskQueue::Current()); |
| 72 | |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 73 | queue.PostTask(Bind(&CheckCurrent, &event, &queue)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 74 | EXPECT_TRUE(event.Wait(1000)); |
| 75 | } |
| 76 | |
| 77 | TEST(TaskQueueTest, PostCustomTask) { |
| 78 | static const char kQueueName[] = "PostCustomImplementation"; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 79 | Event event(false, false); |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 80 | TaskQueue queue(kQueueName); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 81 | |
| 82 | class CustomTask : public QueuedTask { |
| 83 | public: |
| 84 | explicit CustomTask(Event* event) : event_(event) {} |
| 85 | |
| 86 | private: |
| 87 | bool Run() override { |
| 88 | event_->Set(); |
| 89 | return false; // Never allows the task to be deleted by the queue. |
| 90 | } |
| 91 | |
| 92 | Event* const event_; |
| 93 | } my_task(&event); |
| 94 | |
| 95 | // Please don't do this in production code! :) |
| 96 | queue.PostTask(std::unique_ptr<QueuedTask>(&my_task)); |
| 97 | EXPECT_TRUE(event.Wait(1000)); |
| 98 | } |
| 99 | |
| 100 | TEST(TaskQueueTest, PostLambda) { |
| 101 | static const char kQueueName[] = "PostLambda"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 102 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 103 | TaskQueue queue(kQueueName); |
| 104 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 105 | queue.PostTask([&event]() { event.Set(); }); |
| 106 | EXPECT_TRUE(event.Wait(1000)); |
| 107 | } |
| 108 | |
tommi | ede0759 | 2017-02-27 07:16:10 -0800 | [diff] [blame] | 109 | TEST(TaskQueueTest, PostDelayedZero) { |
| 110 | static const char kQueueName[] = "PostDelayedZero"; |
| 111 | Event event(false, false); |
| 112 | TaskQueue queue(kQueueName); |
| 113 | |
| 114 | queue.PostDelayedTask([&event]() { event.Set(); }, 0); |
| 115 | EXPECT_TRUE(event.Wait(1000)); |
| 116 | } |
| 117 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 118 | TEST(TaskQueueTest, PostFromQueue) { |
| 119 | static const char kQueueName[] = "PostFromQueue"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 120 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 121 | TaskQueue queue(kQueueName); |
| 122 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 123 | queue.PostTask( |
| 124 | [&event, &queue]() { queue.PostTask([&event]() { event.Set(); }); }); |
| 125 | EXPECT_TRUE(event.Wait(1000)); |
| 126 | } |
| 127 | |
tommi | c5b435d | 2016-10-31 02:17:11 -0700 | [diff] [blame] | 128 | TEST(TaskQueueTest, PostDelayed) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 129 | static const char kQueueName[] = "PostDelayed"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 130 | Event event(false, false); |
tommi | 5bdee47 | 2017-03-03 05:20:12 -0800 | [diff] [blame] | 131 | TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 132 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 133 | uint32_t start = Time(); |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 134 | queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(event.Wait(1000)); |
| 136 | uint32_t end = Time(); |
tommi | c5b435d | 2016-10-31 02:17:11 -0700 | [diff] [blame] | 137 | // 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] | 138 | // 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] | 139 | // which is why we have a little bit of leeway backwards as well. |
tommi | 67fcad8 | 2016-11-16 10:50:24 -0800 | [diff] [blame] | 140 | EXPECT_GE(end - start, 90u); |
| 141 | EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290. |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 142 | } |
| 143 | |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 144 | // This task needs to be run manually due to the slowness of some of our bots. |
| 145 | // TODO(tommi): Can we run this on the perf bots? |
| 146 | TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { |
| 147 | EnableHighResTimers high_res_scope; |
| 148 | |
| 149 | static const char kQueueName[] = "PostDelayedHighRes"; |
| 150 | Event event(false, false); |
| 151 | TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH); |
| 152 | |
| 153 | uint32_t start = Time(); |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 154 | queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 155 | EXPECT_TRUE(event.Wait(1000)); |
| 156 | uint32_t end = TimeMillis(); |
| 157 | // These tests are a little relaxed due to how "powerful" our test bots can |
| 158 | // be. Most recently we've seen windows bots fire the callback after 94-99ms, |
| 159 | // which is why we have a little bit of leeway backwards as well. |
| 160 | EXPECT_GE(end - start, 3u); |
| 161 | EXPECT_NEAR(end - start, 3, 3u); |
| 162 | } |
| 163 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 164 | TEST(TaskQueueTest, PostMultipleDelayed) { |
| 165 | static const char kQueueName[] = "PostMultipleDelayed"; |
| 166 | TaskQueue queue(kQueueName); |
| 167 | |
| 168 | std::vector<std::unique_ptr<Event>> events; |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 169 | for (int i = 0; i < 100; ++i) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 170 | events.push_back(std::unique_ptr<Event>(new Event(false, false))); |
| 171 | queue.PostDelayedTask( |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 172 | Bind(&CheckCurrent, events.back().get(), &queue), i); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | for (const auto& e : events) |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 176 | EXPECT_TRUE(e->Wait(1000)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST(TaskQueueTest, PostDelayedAfterDestruct) { |
| 180 | static const char kQueueName[] = "PostDelayedAfterDestruct"; |
| 181 | Event event(false, false); |
| 182 | { |
| 183 | TaskQueue queue(kQueueName); |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 184 | queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 185 | } |
| 186 | EXPECT_FALSE(event.Wait(200)); // Task should not run. |
| 187 | } |
| 188 | |
| 189 | TEST(TaskQueueTest, PostAndReply) { |
| 190 | static const char kPostQueue[] = "PostQueue"; |
| 191 | static const char kReplyQueue[] = "ReplyQueue"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 192 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 193 | TaskQueue post_queue(kPostQueue); |
| 194 | TaskQueue reply_queue(kReplyQueue); |
| 195 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 196 | post_queue.PostTaskAndReply( |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 197 | Bind(&CheckCurrent, nullptr, &post_queue), |
| 198 | Bind(&CheckCurrent, &event, &reply_queue), &reply_queue); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 199 | EXPECT_TRUE(event.Wait(1000)); |
| 200 | } |
| 201 | |
| 202 | TEST(TaskQueueTest, PostAndReuse) { |
| 203 | static const char kPostQueue[] = "PostQueue"; |
| 204 | static const char kReplyQueue[] = "ReplyQueue"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 205 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 206 | TaskQueue post_queue(kPostQueue); |
| 207 | TaskQueue reply_queue(kReplyQueue); |
| 208 | |
| 209 | int call_count = 0; |
| 210 | |
| 211 | class ReusedTask : public QueuedTask { |
| 212 | public: |
| 213 | ReusedTask(int* counter, TaskQueue* reply_queue, Event* event) |
| 214 | : counter_(counter), reply_queue_(reply_queue), event_(event) { |
| 215 | EXPECT_EQ(0, *counter_); |
| 216 | } |
| 217 | |
| 218 | private: |
| 219 | bool Run() override { |
| 220 | if (++(*counter_) == 1) { |
| 221 | std::unique_ptr<QueuedTask> myself(this); |
| 222 | reply_queue_->PostTask(std::move(myself)); |
| 223 | // At this point, the object is owned by reply_queue_ and it's |
| 224 | // theoratically possible that the object has been deleted (e.g. if |
| 225 | // posting wasn't possible). So, don't touch any member variables here. |
| 226 | |
| 227 | // Indicate to the current queue that ownership has been transferred. |
| 228 | return false; |
| 229 | } else { |
| 230 | EXPECT_EQ(2, *counter_); |
| 231 | EXPECT_TRUE(reply_queue_->IsCurrent()); |
| 232 | event_->Set(); |
| 233 | return true; // Indicate that the object should be deleted. |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | int* const counter_; |
| 238 | TaskQueue* const reply_queue_; |
| 239 | Event* const event_; |
| 240 | }; |
| 241 | |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 242 | std::unique_ptr<ReusedTask> task( |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 243 | new ReusedTask(&call_count, &reply_queue, &event)); |
| 244 | |
| 245 | post_queue.PostTask(std::move(task)); |
| 246 | EXPECT_TRUE(event.Wait(1000)); |
| 247 | } |
| 248 | |
| 249 | TEST(TaskQueueTest, PostAndReplyLambda) { |
| 250 | static const char kPostQueue[] = "PostQueue"; |
| 251 | static const char kReplyQueue[] = "ReplyQueue"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 252 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 253 | TaskQueue post_queue(kPostQueue); |
| 254 | TaskQueue reply_queue(kReplyQueue); |
| 255 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 256 | bool my_flag = false; |
| 257 | post_queue.PostTaskAndReply([&my_flag]() { my_flag = true; }, |
| 258 | [&event]() { event.Set(); }, &reply_queue); |
| 259 | EXPECT_TRUE(event.Wait(1000)); |
| 260 | EXPECT_TRUE(my_flag); |
| 261 | } |
| 262 | |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 263 | TEST(TaskQueueTest, PostCopyableClosure) { |
| 264 | struct CopyableClosure { |
| 265 | CopyableClosure(int* num_copies, int* num_moves, Event* event) |
| 266 | : num_copies(num_copies), num_moves(num_moves), event(event) {} |
| 267 | CopyableClosure(const CopyableClosure& other) |
| 268 | : num_copies(other.num_copies), |
| 269 | num_moves(other.num_moves), |
| 270 | event(other.event) { |
| 271 | ++*num_copies; |
| 272 | } |
| 273 | CopyableClosure(CopyableClosure&& other) |
| 274 | : num_copies(other.num_copies), |
| 275 | num_moves(other.num_moves), |
| 276 | event(other.event) { |
| 277 | ++*num_moves; |
| 278 | } |
| 279 | void operator()() { event->Set(); } |
| 280 | |
| 281 | int* num_copies; |
| 282 | int* num_moves; |
| 283 | Event* event; |
| 284 | }; |
| 285 | |
| 286 | int num_copies = 0; |
| 287 | int num_moves = 0; |
| 288 | Event event(false, false); |
| 289 | |
| 290 | static const char kPostQueue[] = "PostCopyableClosure"; |
| 291 | TaskQueue post_queue(kPostQueue); |
| 292 | { |
| 293 | CopyableClosure closure(&num_copies, &num_moves, &event); |
| 294 | post_queue.PostTask(closure); |
| 295 | // Destroy closure to check with msan and tsan posted task has own copy. |
| 296 | } |
| 297 | |
| 298 | EXPECT_TRUE(event.Wait(1000)); |
| 299 | EXPECT_EQ(num_copies, 1); |
| 300 | EXPECT_EQ(num_moves, 0); |
| 301 | } |
| 302 | |
| 303 | TEST(TaskQueueTest, PostMoveOnlyClosure) { |
| 304 | struct SomeState { |
| 305 | explicit SomeState(Event* event) : event(event) {} |
| 306 | ~SomeState() { event->Set(); } |
| 307 | Event* event; |
| 308 | }; |
| 309 | struct MoveOnlyClosure { |
| 310 | MoveOnlyClosure(int* num_moves, std::unique_ptr<SomeState> state) |
| 311 | : num_moves(num_moves), state(std::move(state)) {} |
| 312 | MoveOnlyClosure(const MoveOnlyClosure&) = delete; |
| 313 | MoveOnlyClosure(MoveOnlyClosure&& other) |
| 314 | : num_moves(other.num_moves), state(std::move(other.state)) { |
| 315 | ++*num_moves; |
| 316 | } |
| 317 | void operator()() { state.reset(); } |
| 318 | |
| 319 | int* num_moves; |
| 320 | std::unique_ptr<SomeState> state; |
| 321 | }; |
| 322 | |
| 323 | int num_moves = 0; |
| 324 | Event event(false, false); |
| 325 | std::unique_ptr<SomeState> state(new SomeState(&event)); |
| 326 | |
| 327 | static const char kPostQueue[] = "PostMoveOnlyClosure"; |
| 328 | TaskQueue post_queue(kPostQueue); |
| 329 | post_queue.PostTask(MoveOnlyClosure(&num_moves, std::move(state))); |
| 330 | |
| 331 | EXPECT_TRUE(event.Wait(1000)); |
| 332 | EXPECT_EQ(num_moves, 1); |
| 333 | } |
| 334 | |
| 335 | TEST(TaskQueueTest, PostMoveOnlyCleanup) { |
| 336 | struct SomeState { |
| 337 | explicit SomeState(Event* event) : event(event) {} |
| 338 | ~SomeState() { event->Set(); } |
| 339 | Event* event; |
| 340 | }; |
| 341 | struct MoveOnlyClosure { |
| 342 | void operator()() { state.reset(); } |
| 343 | |
| 344 | std::unique_ptr<SomeState> state; |
| 345 | }; |
| 346 | |
| 347 | Event event_run(false, false); |
| 348 | Event event_cleanup(false, false); |
| 349 | std::unique_ptr<SomeState> state_run(new SomeState(&event_run)); |
| 350 | std::unique_ptr<SomeState> state_cleanup(new SomeState(&event_cleanup)); |
| 351 | |
| 352 | static const char kPostQueue[] = "PostMoveOnlyCleanup"; |
| 353 | TaskQueue post_queue(kPostQueue); |
| 354 | post_queue.PostTask(NewClosure(MoveOnlyClosure{std::move(state_run)}, |
| 355 | MoveOnlyClosure{std::move(state_cleanup)})); |
| 356 | |
| 357 | EXPECT_TRUE(event_cleanup.Wait(1000)); |
| 358 | // Expect run closure to complete before cleanup closure. |
| 359 | EXPECT_TRUE(event_run.Wait(0)); |
| 360 | } |
| 361 | |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 362 | // This test covers a particular bug that we had in the libevent implementation |
| 363 | // where we could hit a deadlock while trying to post a reply task to a queue |
| 364 | // that was being deleted. The test isn't guaranteed to hit that case but it's |
| 365 | // written in a way that makes it likely and by running with --gtest_repeat=1000 |
| 366 | // the bug would occur. Alas, now it should be fixed. |
| 367 | TEST(TaskQueueTest, PostAndReplyDeadlock) { |
| 368 | Event event(false, false); |
| 369 | TaskQueue post_queue("PostQueue"); |
| 370 | TaskQueue reply_queue("ReplyQueue"); |
| 371 | |
| 372 | post_queue.PostTaskAndReply([&event]() { event.Set(); }, []() {}, |
| 373 | &reply_queue); |
| 374 | EXPECT_TRUE(event.Wait(1000)); |
| 375 | } |
| 376 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 377 | void TestPostTaskAndReply(TaskQueue* work_queue, |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 378 | Event* event) { |
| 379 | ASSERT_FALSE(work_queue->IsCurrent()); |
| 380 | work_queue->PostTaskAndReply( |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 381 | Bind(&CheckCurrent, nullptr, work_queue), |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 382 | NewClosure([event]() { event->Set(); })); |
| 383 | } |
| 384 | |
| 385 | // Does a PostTaskAndReply from within a task to post and reply to the current |
| 386 | // queue. All in all there will be 3 tasks posted and run. |
| 387 | TEST(TaskQueueTest, PostAndReply2) { |
| 388 | static const char kQueueName[] = "PostAndReply2"; |
| 389 | static const char kWorkQueueName[] = "PostAndReply2_Worker"; |
tommi | 8c80c6e | 2017-02-23 00:34:52 -0800 | [diff] [blame] | 390 | Event event(false, false); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 391 | TaskQueue queue(kQueueName); |
| 392 | TaskQueue work_queue(kWorkQueueName); |
| 393 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 394 | queue.PostTask( |
nisse | 2c7b7a6 | 2017-09-04 05:18:21 -0700 | [diff] [blame] | 395 | Bind(&TestPostTaskAndReply, &work_queue, &event)); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 396 | EXPECT_TRUE(event.Wait(1000)); |
| 397 | } |
| 398 | |
| 399 | // Tests posting more messages than a queue can queue up. |
| 400 | // In situations like that, tasks will get dropped. |
| 401 | TEST(TaskQueueTest, PostALot) { |
| 402 | // To destruct the event after the queue has gone out of scope. |
| 403 | Event event(false, false); |
| 404 | |
| 405 | int tasks_executed = 0; |
| 406 | int tasks_cleaned_up = 0; |
| 407 | static const int kTaskCount = 0xffff; |
| 408 | |
| 409 | { |
| 410 | static const char kQueueName[] = "PostALot"; |
| 411 | TaskQueue queue(kQueueName); |
| 412 | |
| 413 | // On linux, the limit of pending bytes in the pipe buffer is 0xffff. |
| 414 | // So here we post a total of 0xffff+1 messages, which triggers a failure |
| 415 | // case inside of the libevent queue implementation. |
| 416 | |
| 417 | queue.PostTask([&event]() { event.Wait(Event::kForever); }); |
| 418 | for (int i = 0; i < kTaskCount; ++i) |
| 419 | queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; }, |
| 420 | [&tasks_cleaned_up]() { ++tasks_cleaned_up; })); |
| 421 | event.Set(); // Unblock the first task. |
| 422 | } |
| 423 | |
| 424 | EXPECT_GE(tasks_cleaned_up, tasks_executed); |
| 425 | EXPECT_EQ(kTaskCount, tasks_cleaned_up); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | } // namespace rtc |