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