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