1 // Copyright 2011 The Chromium Authors
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 #include <string>
9
10 #include "base/cfi_buildflags.h"
11 #include "base/check_op.h"
12 #include "base/clang_profiling_buildflags.h"
13 #include "base/command_line.h"
14 #include "base/debug/debugger.h"
15 #include "base/logging.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/test/test_switches.h"
18 #include "build/build_config.h"
19 #include "build/chromeos_buildflags.h"
20
21 namespace {
22
23 // Sets value to the greatest of:
24 // 1) value's current value multiplied by kTimeoutMultiplier (assuming
25 // InitializeTimeout is called only once per value).
26 // 2) min_value.
27 // 3) the numerical value given by switch_name on the command line multiplied
28 // by kTimeoutMultiplier.
InitializeTimeout(const char * switch_name,base::TimeDelta min_value,base::TimeDelta * value)29 void InitializeTimeout(const char* switch_name,
30 base::TimeDelta min_value,
31 base::TimeDelta* value) {
32 DCHECK(value);
33 base::TimeDelta command_line_timeout;
34 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
35 std::string string_value(base::CommandLine::ForCurrentProcess()->
36 GetSwitchValueASCII(switch_name));
37 int command_line_timeout_ms = 0;
38 if (!base::StringToInt(string_value, &command_line_timeout_ms)) {
39 LOG(FATAL) << "Timeout value \"" << string_value << "\" was parsed as "
40 << command_line_timeout_ms;
41 }
42 command_line_timeout = base::Milliseconds(command_line_timeout_ms);
43 }
44
45 #if defined(MEMORY_SANITIZER)
46 // ASan/TSan/MSan instrument each memory access. This may slow the execution
47 // down significantly.
48 // For MSan the slowdown depends heavily on the value of msan_track_origins
49 // build flag. The multiplier below corresponds to msan_track_origins = 1.
50 #if BUILDFLAG(IS_CHROMEOS_ASH)
51 // A handful of tests on ChromeOS time out when using the 6x limit used
52 // elsewhere, so it's bumped to 10x.
53 constexpr int kTimeoutMultiplier = 10;
54 #else
55 constexpr int kTimeoutMultiplier = 6;
56 #endif
57 #elif BUILDFLAG(CFI_DIAG)
58 constexpr int kTimeoutMultiplier = 3;
59 #elif defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_WIN)
60 // ASan/Win has not been optimized yet, give it a higher
61 // timeout multiplier. See http://crbug.com/412471
62 constexpr int kTimeoutMultiplier = 3;
63 #elif defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_CHROMEOS_ASH)
64 // A number of tests on ChromeOS run very close to the 2x limit, so ChromeOS
65 // gets 3x.
66 constexpr int kTimeoutMultiplier = 3;
67 #elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER)
68 constexpr int kTimeoutMultiplier = 2;
69 #elif BUILDFLAG(CLANG_PROFILING)
70 // On coverage build, tests run 3x slower.
71 constexpr int kTimeoutMultiplier = 3;
72 #elif !defined(NDEBUG) && BUILDFLAG(IS_CHROMEOS_ASH)
73 // TODO(crbug.com/1058022): reduce the multiplier back to 2x.
74 // A number of tests on ChromeOS run very close to the base limit, so ChromeOS
75 // gets 3x.
76 constexpr int kTimeoutMultiplier = 3;
77 #elif !defined(NDEBUG) && BUILDFLAG(IS_MAC)
78 // A lot of browser_tests on Mac debug time out.
79 constexpr int kTimeoutMultiplier = 2;
80 #else
81 constexpr int kTimeoutMultiplier = 1;
82 #endif
83
84 *value = std::max(std::max(*value, command_line_timeout) * kTimeoutMultiplier,
85 min_value);
86 }
87
88 } // namespace
89
90 // static
91 bool TestTimeouts::initialized_ = false;
92
93 // The timeout values should increase in the order they appear in this block.
94 // static
95 base::TimeDelta TestTimeouts::tiny_timeout_ = base::Milliseconds(100);
96 base::TimeDelta TestTimeouts::action_timeout_ = base::Seconds(10);
97 base::TimeDelta TestTimeouts::action_max_timeout_ = base::Seconds(30);
98 base::TimeDelta TestTimeouts::test_launcher_timeout_ = base::Seconds(45);
99
100 // static
Initialize()101 void TestTimeouts::Initialize() {
102 DCHECK(!initialized_);
103 initialized_ = true;
104
105 const bool being_debugged = base::debug::BeingDebugged();
106 if (being_debugged) {
107 fprintf(stdout,
108 "Detected presence of a debugger, running without test timeouts.\n");
109 }
110
111 // Note that these timeouts MUST be initialized in the correct order as
112 // per the CHECKS below.
113
114 InitializeTimeout(switches::kTestTinyTimeout, base::TimeDelta(),
115 &tiny_timeout_);
116
117 // All timeouts other than the "tiny" one should be set to very large values
118 // when in a debugger or when run interactively, so that tests will not get
119 // auto-terminated. By setting the UI test action timeout to at least this
120 // value, we guarantee the subsequent timeouts will be this large also.
121 // Setting the "tiny" timeout to a large value as well would make some tests
122 // hang (because it's used as a task-posting delay). In particular this
123 // causes problems for some iOS device tests, which are always run inside a
124 // debugger (thus BeingDebugged() is true even on the bots).
125 base::TimeDelta min_ui_test_action_timeout = tiny_timeout_;
126 if (being_debugged || base::CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kTestLauncherInteractive)) {
128 min_ui_test_action_timeout = base::Days(1);
129 }
130
131 InitializeTimeout(switches::kUiTestActionTimeout, min_ui_test_action_timeout,
132 &action_timeout_);
133 InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_,
134 &action_max_timeout_);
135
136 // Test launcher timeout is independent from anything above action timeout.
137 InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_,
138 &test_launcher_timeout_);
139
140 // The timeout values should be increasing in the right order.
141 CHECK_LE(tiny_timeout_, action_timeout_);
142 CHECK_LE(action_timeout_, action_max_timeout_);
143 CHECK_LE(action_timeout_, test_launcher_timeout_);
144 }
145