• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/core/test_utils.h"
6 
7 #include <stdint.h>
8 
9 #include <limits>
10 
11 #include "base/logging.h"
12 #include "base/test/test_timeouts.h"
13 #include "base/threading/platform_thread.h"  // For |Sleep()|.
14 #include "build/build_config.h"
15 
16 namespace mojo {
17 namespace core {
18 namespace test {
19 
DeadlineFromMilliseconds(unsigned milliseconds)20 MojoDeadline DeadlineFromMilliseconds(unsigned milliseconds) {
21   return static_cast<MojoDeadline>(milliseconds) * 1000;
22 }
23 
EpsilonDeadline()24 MojoDeadline EpsilonDeadline() {
25 // Originally, our epsilon timeout was 10 ms, which was mostly fine but flaky on
26 // some Windows bots. I don't recall ever seeing flakes on other bots. At 30 ms
27 // tests seem reliable on Windows bots, but not at 25 ms. We'd like this timeout
28 // to be as small as possible (see the description in the .h file).
29 //
30 // Currently, |tiny_timeout()| is usually 100 ms (possibly scaled under ASAN,
31 // etc.). Based on this, set it to (usually be) 30 ms on Windows and 20 ms
32 // elsewhere.
33 #if defined(OS_WIN) || defined(OS_ANDROID)
34   return (TinyDeadline() * 3) / 10;
35 #else
36   return (TinyDeadline() * 2) / 10;
37 #endif
38 }
39 
TinyDeadline()40 MojoDeadline TinyDeadline() {
41   return static_cast<MojoDeadline>(
42       TestTimeouts::tiny_timeout().InMicroseconds());
43 }
44 
ActionDeadline()45 MojoDeadline ActionDeadline() {
46   return static_cast<MojoDeadline>(
47       TestTimeouts::action_timeout().InMicroseconds());
48 }
49 
Sleep(MojoDeadline deadline)50 void Sleep(MojoDeadline deadline) {
51   CHECK_LE(deadline,
52            static_cast<MojoDeadline>(std::numeric_limits<int64_t>::max()));
53   base::PlatformThread::Sleep(
54       base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline)));
55 }
56 
Stopwatch()57 Stopwatch::Stopwatch() {}
58 
~Stopwatch()59 Stopwatch::~Stopwatch() {}
60 
Start()61 void Stopwatch::Start() {
62   start_time_ = base::TimeTicks::Now();
63 }
64 
Elapsed()65 MojoDeadline Stopwatch::Elapsed() {
66   int64_t result = (base::TimeTicks::Now() - start_time_).InMicroseconds();
67   // |DCHECK_GE|, not |CHECK_GE|, since this may be performance-important.
68   DCHECK_GE(result, 0);
69   return static_cast<MojoDeadline>(result);
70 }
71 
72 }  // namespace test
73 }  // namespace core
74 }  // namespace mojo
75