Home | History | Annotate | Download | only in system
      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/edk/system/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 edk {
     18 namespace test {
     19 
     20 MojoDeadline DeadlineFromMilliseconds(unsigned milliseconds) {
     21   return static_cast<MojoDeadline>(milliseconds) * 1000;
     22 }
     23 
     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 
     40 MojoDeadline TinyDeadline() {
     41   return static_cast<MojoDeadline>(
     42       TestTimeouts::tiny_timeout().InMicroseconds());
     43 }
     44 
     45 MojoDeadline ActionDeadline() {
     46   return static_cast<MojoDeadline>(
     47       TestTimeouts::action_timeout().InMicroseconds());
     48 }
     49 
     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 
     57 Stopwatch::Stopwatch() {
     58 }
     59 
     60 Stopwatch::~Stopwatch() {
     61 }
     62 
     63 void Stopwatch::Start() {
     64   start_time_ = base::TimeTicks::Now();
     65 }
     66 
     67 MojoDeadline Stopwatch::Elapsed() {
     68   int64_t result = (base::TimeTicks::Now() - start_time_).InMicroseconds();
     69   // |DCHECK_GE|, not |CHECK_GE|, since this may be performance-important.
     70   DCHECK_GE(result, 0);
     71   return static_cast<MojoDeadline>(result);
     72 }
     73 
     74 }  // namespace test
     75 }  // namespace edk
     76 }  // namespace mojo
     77