blob: 579dc3cced5a29dff08715f3c7d437d2879287ef [file] [log] [blame]
tommic06b1332016-05-14 18:31:401/*
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
tommi0b942152017-03-10 17:33:5311#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 Gerey3e707812018-11-28 15:47:4918#include <stdint.h>
Danil Chapovalovc05a1be2022-07-19 11:07:1219
tommic06b1332016-05-14 18:31:4020#include <memory>
Yves Gerey3e707812018-11-28 15:47:4921#include <utility>
tommic06b1332016-05-14 18:31:4022#include <vector>
23
Yves Gerey3e707812018-11-28 15:47:4924#include "absl/memory/memory.h"
Danil Chapovalovc05a1be2022-07-19 11:07:1225#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "rtc_base/event.h"
Danil Chapovalov07122bc2019-03-26 13:37:0127#include "rtc_base/task_queue_for_test.h"
Steve Anton10542f22019-01-11 17:11:0028#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 15:47:4929#include "test/gtest.h"
tommic06b1332016-05-14 18:31:4030
31namespace rtc {
Tommi68561562018-02-13 18:47:5032
tommi0b942152017-03-10 17:33:5333namespace {
Danil Chapovalovc05a1be2022-07-19 11:07:1234using ::webrtc::TimeDelta;
tommi0b942152017-03-10 17:33:5335// 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.
38class 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};
tommic06b1332016-05-14 18:31:4053
nisse2c7b7a62017-09-04 12:18:2154void CheckCurrent(Event* signal, TaskQueue* queue) {
tommic06b1332016-05-14 18:31:4055 EXPECT_TRUE(queue->IsCurrent());
56 if (signal)
57 signal->Set();
58}
59
60} // namespace
61
tommi0b942152017-03-10 17:33:5362// This task needs to be run manually due to the slowness of some of our bots.
63// TODO(tommi): Can we run this on the perf bots?
64TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) {
65 EnableHighResTimers high_res_scope;
66
67 static const char kQueueName[] = "PostDelayedHighRes";
Niels Möllerc572ff32018-11-07 07:43:5068 Event event;
Danil Chapovalov07122bc2019-03-26 13:37:0169 webrtc::TaskQueueForTest queue(kQueueName, TaskQueue::Priority::HIGH);
tommi0b942152017-03-10 17:33:5370
71 uint32_t start = Time();
Danil Chapovalovc05a1be2022-07-19 11:07:1272 queue.PostDelayedTask([&event, &queue] { CheckCurrent(&event, &queue); },
73 TimeDelta::Millis(3));
Markus Handell2cfc1af2022-08-19 08:16:4874 EXPECT_TRUE(event.Wait(webrtc::TimeDelta::Seconds(1)));
tommi0b942152017-03-10 17:33:5375 uint32_t end = TimeMillis();
76 // These tests are a little relaxed due to how "powerful" our test bots can
77 // be. Most recently we've seen windows bots fire the callback after 94-99ms,
78 // which is why we have a little bit of leeway backwards as well.
79 EXPECT_GE(end - start, 3u);
80 EXPECT_NEAR(end - start, 3, 3u);
81}
82
tommic06b1332016-05-14 18:31:4083} // namespace rtc