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 
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 
18 #include <stdint.h>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/memory/memory.h"
24 #include "rtc_base/bind.h"
25 #include "rtc_base/event.h"
26 #include "rtc_base/task_queue_for_test.h"
27 #include "rtc_base/time_utils.h"
28 #include "test/gtest.h"
29 
30 namespace rtc {
31 
32 namespace {
33 // Noop on all platforms except Windows, where it turns on high precision
34 // multimedia timers which increases the precision of TimeMillis() while in
35 // scope.
36 class EnableHighResTimers {
37  public:
38 #if !defined(WEBRTC_WIN)
EnableHighResTimers()39   EnableHighResTimers() {}
40 #else
41   EnableHighResTimers() : enabled_(timeBeginPeriod(1) == TIMERR_NOERROR) {}
42   ~EnableHighResTimers() {
43     if (enabled_)
44       timeEndPeriod(1);
45   }
46 
47  private:
48   const bool enabled_;
49 #endif
50 };
51 
CheckCurrent(Event * signal,TaskQueue * queue)52 void CheckCurrent(Event* signal, TaskQueue* queue) {
53   EXPECT_TRUE(queue->IsCurrent());
54   if (signal)
55     signal->Set();
56 }
57 
58 }  // namespace
59 
60 // This task needs to be run manually due to the slowness of some of our bots.
61 // TODO(tommi): Can we run this on the perf bots?
TEST(TaskQueueTest,DISABLED_PostDelayedHighRes)62 TEST(TaskQueueTest, DISABLED_PostDelayedHighRes) {
63   EnableHighResTimers high_res_scope;
64 
65   static const char kQueueName[] = "PostDelayedHighRes";
66   Event event;
67   webrtc::TaskQueueForTest queue(kQueueName, TaskQueue::Priority::HIGH);
68 
69   uint32_t start = Time();
70   queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 3);
71   EXPECT_TRUE(event.Wait(1000));
72   uint32_t end = TimeMillis();
73   // These tests are a little relaxed due to how "powerful" our test bots can
74   // be.  Most recently we've seen windows bots fire the callback after 94-99ms,
75   // which is why we have a little bit of leeway backwards as well.
76   EXPECT_GE(end - start, 3u);
77   EXPECT_NEAR(end - start, 3, 3u);
78 }
79 
80 }  // namespace rtc
81