• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 "base/test/test_timeouts.h"
6 
7 #include <algorithm>
8 
9 #include "base/command_line.h"
10 #include "base/debug/debugger.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/test/test_switches.h"
14 #include "build/build_config.h"
15 
16 namespace {
17 
18 // ASan/TSan/MSan instrument each memory access. This may slow the execution
19 // down significantly.
20 #if defined(MEMORY_SANITIZER)
21 // For MSan the slowdown depends heavily on the value of msan_track_origins GYP
22 // flag. The multiplier below corresponds to msan_track_origins=1.
23 static const int kTimeoutMultiplier = 6;
24 #elif defined(ADDRESS_SANITIZER) && defined(OS_WIN)
25 // Asan/Win has not been optimized yet, give it a higher
26 // timeout multiplier. See http://crbug.com/412471
27 static const int kTimeoutMultiplier = 3;
28 #elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
29     defined(SYZYASAN)
30 static const int kTimeoutMultiplier = 2;
31 #else
32 static const int kTimeoutMultiplier = 1;
33 #endif
34 
35 const int kAlmostInfiniteTimeoutMs = 100000000;
36 
37 // Sets value to the greatest of:
38 // 1) value's current value multiplied by kTimeoutMultiplier (assuming
39 // InitializeTimeout is called only once per value).
40 // 2) min_value.
41 // 3) the numerical value given by switch_name on the command line multiplied
42 // by kTimeoutMultiplier.
InitializeTimeout(const char * switch_name,int min_value,int * value)43 void InitializeTimeout(const char* switch_name, int min_value, int* value) {
44   DCHECK(value);
45   if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
46     std::string string_value(base::CommandLine::ForCurrentProcess()->
47          GetSwitchValueASCII(switch_name));
48     int timeout;
49     if (string_value == TestTimeouts::kNoTimeoutSwitchValue)
50       timeout = kAlmostInfiniteTimeoutMs;
51     else
52       base::StringToInt(string_value, &timeout);
53     *value = std::max(*value, timeout);
54   }
55   *value *= kTimeoutMultiplier;
56   *value = std::max(*value, min_value);
57 }
58 
59 // Sets value to the greatest of:
60 // 1) value's current value multiplied by kTimeoutMultiplier.
61 // 2) 0
62 // 3) the numerical value given by switch_name on the command line multiplied
63 // by kTimeoutMultiplier.
InitializeTimeout(const char * switch_name,int * value)64 void InitializeTimeout(const char* switch_name, int* value) {
65   InitializeTimeout(switch_name, 0, value);
66 }
67 
68 }  // namespace
69 
70 // static
71 constexpr const char TestTimeouts::kNoTimeoutSwitchValue[];
72 
73 // static
74 bool TestTimeouts::initialized_ = false;
75 
76 // The timeout values should increase in the order they appear in this block.
77 // static
78 int TestTimeouts::tiny_timeout_ms_ = 100;
79 int TestTimeouts::action_timeout_ms_ = 10000;
80 #ifndef NDEBUG
81 int TestTimeouts::action_max_timeout_ms_ = 45000;
82 #else
83 int TestTimeouts::action_max_timeout_ms_ = 30000;
84 #endif  // NDEBUG
85 
86 int TestTimeouts::test_launcher_timeout_ms_ = 45000;
87 
88 // static
Initialize()89 void TestTimeouts::Initialize() {
90   if (initialized_) {
91     NOTREACHED();
92     return;
93   }
94   initialized_ = true;
95 
96   if (base::debug::BeingDebugged()) {
97     fprintf(stdout,
98         "Detected presence of a debugger, running without test timeouts.\n");
99   }
100 
101   // Note that these timeouts MUST be initialized in the correct order as
102   // per the CHECKS below.
103   InitializeTimeout(switches::kTestTinyTimeout, &tiny_timeout_ms_);
104   InitializeTimeout(switches::kUiTestActionTimeout,
105                     base::debug::BeingDebugged() ? kAlmostInfiniteTimeoutMs
106                                                  : tiny_timeout_ms_,
107                     &action_timeout_ms_);
108   InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_ms_,
109                     &action_max_timeout_ms_);
110 
111   // Test launcher timeout is independent from anything above action timeout.
112   InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_ms_,
113                     &test_launcher_timeout_ms_);
114 
115   // The timeout values should be increasing in the right order.
116   CHECK(tiny_timeout_ms_ <= action_timeout_ms_);
117   CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
118 
119   CHECK(action_timeout_ms_ <= test_launcher_timeout_ms_);
120 }
121