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