| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 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 |
| 11 #include <memory> | 18 #include <memory> |
| 12 #include <vector> | 19 #include <vector> |
| 13 | 20 |
| 14 #include "webrtc/base/bind.h" | 21 #include "webrtc/base/bind.h" |
| 15 #include "webrtc/base/event.h" | 22 #include "webrtc/base/event.h" |
| 16 #include "webrtc/base/gunit.h" | 23 #include "webrtc/base/gunit.h" |
| 17 #include "webrtc/base/task_queue.h" | 24 #include "webrtc/base/task_queue.h" |
| 18 #include "webrtc/base/timeutils.h" | 25 #include "webrtc/base/timeutils.h" |
| 19 | 26 |
| 20 namespace rtc { | 27 namespace rtc { |
| 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 } |
| 21 | 48 |
| 22 namespace { | 49 namespace { |
| 23 void CheckCurrent(const char* expected_queue, Event* signal, TaskQueue* queue) { | 50 void CheckCurrent(const char* expected_queue, Event* signal, TaskQueue* queue) { |
| 24 EXPECT_TRUE(TaskQueue::IsCurrent(expected_queue)); | 51 EXPECT_TRUE(TaskQueue::IsCurrent(expected_queue)); |
| 25 EXPECT_TRUE(queue->IsCurrent()); | 52 EXPECT_TRUE(queue->IsCurrent()); |
| 26 if (signal) | 53 if (signal) |
| 27 signal->Set(); | 54 signal->Set(); |
| 28 } | 55 } |
| 29 | 56 |
| 30 } // namespace | 57 } // namespace |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100); | 135 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 100); |
| 109 EXPECT_TRUE(event.Wait(1000)); | 136 EXPECT_TRUE(event.Wait(1000)); |
| 110 uint32_t end = Time(); | 137 uint32_t end = Time(); |
| 111 // These tests are a little relaxed due to how "powerful" our test bots can | 138 // These tests are a little relaxed due to how "powerful" our test bots can |
| 112 // be. Most recently we've seen windows bots fire the callback after 94-99ms, | 139 // be. Most recently we've seen windows bots fire the callback after 94-99ms, |
| 113 // which is why we have a little bit of leeway backwards as well. | 140 // which is why we have a little bit of leeway backwards as well. |
| 114 EXPECT_GE(end - start, 90u); | 141 EXPECT_GE(end - start, 90u); |
| 115 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290. | 142 EXPECT_NEAR(end - start, 190u, 100u); // Accept 90-290. |
| 116 } | 143 } |
| 117 | 144 |
| 145 // This task needs to be run manually due to the slowness of some of our bots. |
| 146 // TODO(tommi): Can we run this on the perf bots? |
| 147 TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { |
| 148 EnableHighResTimers high_res_scope; |
| 149 |
| 150 static const char kQueueName[] = "PostDelayedHighRes"; |
| 151 Event event(false, false); |
| 152 TaskQueue queue(kQueueName, TaskQueue::Priority::HIGH); |
| 153 |
| 154 uint32_t start = Time(); |
| 155 queue.PostDelayedTask(Bind(&CheckCurrent, kQueueName, &event, &queue), 3); |
| 156 EXPECT_TRUE(event.Wait(1000)); |
| 157 uint32_t end = TimeMillis(); |
| 158 // These tests are a little relaxed due to how "powerful" our test bots can |
| 159 // be. Most recently we've seen windows bots fire the callback after 94-99ms, |
| 160 // which is why we have a little bit of leeway backwards as well. |
| 161 EXPECT_GE(end - start, 3u); |
| 162 EXPECT_NEAR(end - start, 3, 3u); |
| 163 } |
| 164 |
| 118 TEST(TaskQueueTest, PostMultipleDelayed) { | 165 TEST(TaskQueueTest, PostMultipleDelayed) { |
| 119 static const char kQueueName[] = "PostMultipleDelayed"; | 166 static const char kQueueName[] = "PostMultipleDelayed"; |
| 120 TaskQueue queue(kQueueName); | 167 TaskQueue queue(kQueueName); |
| 121 | 168 |
| 122 std::vector<std::unique_ptr<Event>> events; | 169 std::vector<std::unique_ptr<Event>> events; |
| 123 for (int i = 0; i < 100; ++i) { | 170 for (int i = 0; i < 100; ++i) { |
| 124 events.push_back(std::unique_ptr<Event>(new Event(false, false))); | 171 events.push_back(std::unique_ptr<Event>(new Event(false, false))); |
| 125 queue.PostDelayedTask( | 172 queue.PostDelayedTask( |
| 126 Bind(&CheckCurrent, kQueueName, events.back().get(), &queue), 10); | 173 Bind(&CheckCurrent, kQueueName, events.back().get(), &queue), i); |
| 127 } | 174 } |
| 128 | 175 |
| 129 for (const auto& e : events) | 176 for (const auto& e : events) |
| 130 EXPECT_TRUE(e->Wait(1000)); | 177 EXPECT_TRUE(e->Wait(1000)); |
| 131 } | 178 } |
| 132 | 179 |
| 133 TEST(TaskQueueTest, PostDelayedAfterDestruct) { | 180 TEST(TaskQueueTest, PostDelayedAfterDestruct) { |
| 134 static const char kQueueName[] = "PostDelayedAfterDestruct"; | 181 static const char kQueueName[] = "PostDelayedAfterDestruct"; |
| 135 Event event(false, false); | 182 Event event(false, false); |
| 136 { | 183 { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; }, | 322 queue.PostTask(NewClosure([&tasks_executed]() { ++tasks_executed; }, |
| 276 [&tasks_cleaned_up]() { ++tasks_cleaned_up; })); | 323 [&tasks_cleaned_up]() { ++tasks_cleaned_up; })); |
| 277 event.Set(); // Unblock the first task. | 324 event.Set(); // Unblock the first task. |
| 278 } | 325 } |
| 279 | 326 |
| 280 EXPECT_GE(tasks_cleaned_up, tasks_executed); | 327 EXPECT_GE(tasks_cleaned_up, tasks_executed); |
| 281 EXPECT_EQ(kTaskCount, tasks_cleaned_up); | 328 EXPECT_EQ(kTaskCount, tasks_cleaned_up); |
| 282 } | 329 } |
| 283 | 330 |
| 284 } // namespace rtc | 331 } // namespace rtc |
| OLD | NEW |