blob: bd07a408a6a3f8603d6f938b21d3add4a00184c3 [file] [log] [blame]
tommic06b1332016-05-14 11:31:40 -07001/*
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 09:33:53 -080011#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 16:47:49 +010018#include <stdint.h>
tommic06b1332016-05-14 11:31:40 -070019#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010020#include <utility>
tommic06b1332016-05-14 11:31:40 -070021#include <vector>
22
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/event.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010025#include "rtc_base/task_queue_for_test.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "test/gtest.h"
tommic06b1332016-05-14 11:31:40 -070028
29namespace rtc {
Tommi68561562018-02-13 19:47:50 +010030
tommi0b942152017-03-10 09:33:53 -080031namespace {
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.
35class 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};
tommic06b1332016-05-14 11:31:40 -070050
nisse2c7b7a62017-09-04 05:18:21 -070051void CheckCurrent(Event* signal, TaskQueue* queue) {
tommic06b1332016-05-14 11:31:40 -070052 EXPECT_TRUE(queue->IsCurrent());
53 if (signal)
54 signal->Set();
55}
56
57} // namespace
58
tommi0b942152017-03-10 09:33:53 -080059// This task needs to be run manually due to the slowness of some of our bots.
60// TODO(tommi): Can we run this on the perf bots?
61TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) {
62 EnableHighResTimers high_res_scope;
63
64 static const char kQueueName[] = "PostDelayedHighRes";
Niels Möllerc572ff32018-11-07 08:43:50 +010065 Event event;
Danil Chapovalov07122bc2019-03-26 14:37:01 +010066 webrtc::TaskQueueForTest queue(kQueueName, TaskQueue::Priority::HIGH);
tommi0b942152017-03-10 09:33:53 -080067
68 uint32_t start = Time();
Markus Handell9a665402022-06-10 14:00:22 +000069 queue.PostDelayedTask(
70 webrtc::ToQueuedTask([&event, &queue] { CheckCurrent(&event, &queue); }),
71 3);
tommi0b942152017-03-10 09:33:53 -080072 EXPECT_TRUE(event.Wait(1000));
73 uint32_t end = TimeMillis();
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
tommic06b1332016-05-14 11:31:40 -070081} // namespace rtc