tommi | c06b133 | 2016-05-14 18:31:40 | [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 17:33:53 | [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 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 18 | #include <stdint.h> |
Danil Chapovalov | c05a1be | 2022-07-19 11:07:12 | [diff] [blame] | 19 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 20 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include <utility> |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 24 | #include "absl/memory/memory.h" |
Danil Chapovalov | c05a1be | 2022-07-19 11:07:12 | [diff] [blame] | 25 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "rtc_base/event.h" |
Danil Chapovalov | 07122bc | 2019-03-26 13:37:01 | [diff] [blame] | 27 | #include "rtc_base/task_queue_for_test.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/time_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 29 | #include "test/gtest.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 30 | |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 31 | namespace webrtc { |
Tommi | 6856156 | 2018-02-13 18:47:50 | [diff] [blame] | 32 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 33 | namespace { |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 34 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 35 | // Noop on all platforms except Windows, where it turns on high precision |
| 36 | // multimedia timers which increases the precision of TimeMillis() while in |
| 37 | // scope. |
| 38 | class EnableHighResTimers { |
| 39 | public: |
| 40 | #if !defined(WEBRTC_WIN) |
| 41 | EnableHighResTimers() {} |
| 42 | #else |
| 43 | EnableHighResTimers() : enabled_(timeBeginPeriod(1) == TIMERR_NOERROR) {} |
| 44 | ~EnableHighResTimers() { |
| 45 | if (enabled_) |
| 46 | timeEndPeriod(1); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | const bool enabled_; |
| 51 | #endif |
| 52 | }; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 53 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 54 | } // namespace |
| 55 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 56 | // This task needs to be run manually due to the slowness of some of our bots. |
| 57 | // TODO(tommi): Can we run this on the perf bots? |
| 58 | TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) { |
| 59 | EnableHighResTimers high_res_scope; |
| 60 | |
| 61 | static const char kQueueName[] = "PostDelayedHighRes"; |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 62 | rtc::Event event; |
| 63 | TaskQueueForTest queue(kQueueName, TaskQueueFactory::Priority::HIGH); |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 64 | |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 65 | uint32_t start = rtc::TimeMillis(); |
| 66 | queue.PostDelayedTask( |
| 67 | [&event, &queue] { |
| 68 | EXPECT_TRUE(queue.IsCurrent()); |
| 69 | event.Set(); |
| 70 | }, |
| 71 | TimeDelta::Millis(3)); |
| 72 | EXPECT_TRUE(event.Wait(TimeDelta::Seconds(1))); |
| 73 | uint32_t end = rtc::TimeMillis(); |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 74 | // These tests are a little relaxed due to how "powerful" our test bots can |
| 75 | // be. Most recently we've seen windows bots fire the callback after 94-99ms, |
| 76 | // which is why we have a little bit of leeway backwards as well. |
| 77 | EXPECT_GE(end - start, 3u); |
| 78 | EXPECT_NEAR(end - start, 3, 3u); |
| 79 | } |
| 80 | |
Danil Chapovalov | 0206a97 | 2024-01-19 13:08:53 | [diff] [blame] | 81 | } // namespace webrtc |