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 base::StringToInt(string_value, &timeout);
50 *value = std::max(*value, timeout);
51 }
52 *value *= kTimeoutMultiplier;
53 *value = std::max(*value, min_value);
54 }
55
56 // Sets value to the greatest of:
57 // 1) value's current value multiplied by kTimeoutMultiplier.
58 // 2) 0
59 // 3) the numerical value given by switch_name on the command line multiplied
60 // by kTimeoutMultiplier.
InitializeTimeout(const char * switch_name,int * value)61 void InitializeTimeout(const char* switch_name, int* value) {
62 InitializeTimeout(switch_name, 0, value);
63 }
64
65 } // namespace
66
67 // static
68 bool TestTimeouts::initialized_ = false;
69
70 // The timeout values should increase in the order they appear in this block.
71 // static
72 int TestTimeouts::tiny_timeout_ms_ = 100;
73 int TestTimeouts::action_timeout_ms_ = 10000;
74 #ifndef NDEBUG
75 int TestTimeouts::action_max_timeout_ms_ = 45000;
76 #else
77 int TestTimeouts::action_max_timeout_ms_ = 30000;
78 #endif // NDEBUG
79
80 int TestTimeouts::test_launcher_timeout_ms_ = 45000;
81
82 // static
Initialize()83 void TestTimeouts::Initialize() {
84 if (initialized_) {
85 NOTREACHED();
86 return;
87 }
88 initialized_ = true;
89
90 if (base::debug::BeingDebugged()) {
91 fprintf(stdout,
92 "Detected presence of a debugger, running without test timeouts.\n");
93 }
94
95 // Note that these timeouts MUST be initialized in the correct order as
96 // per the CHECKS below.
97 InitializeTimeout(switches::kTestTinyTimeout, &tiny_timeout_ms_);
98 InitializeTimeout(switches::kUiTestActionTimeout,
99 base::debug::BeingDebugged() ? kAlmostInfiniteTimeoutMs
100 : tiny_timeout_ms_,
101 &action_timeout_ms_);
102 InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_ms_,
103 &action_max_timeout_ms_);
104
105 // Test launcher timeout is independent from anything above action timeout.
106 InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_ms_,
107 &test_launcher_timeout_ms_);
108
109 // The timeout values should be increasing in the right order.
110 CHECK(tiny_timeout_ms_ <= action_timeout_ms_);
111 CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
112
113 CHECK(action_timeout_ms_ <= test_launcher_timeout_ms_);
114 }
115