Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / test / test_timeouts.cc
blob739245040e8339f0db77aef3d982026ecf0bec89
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.
5 #include "base/test/test_timeouts.h"
7 #include <algorithm>
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"
15 namespace {
17 // ASan/TSan/MSan instrument each memory access. This may slow the execution
18 // down significantly.
19 #if defined(MEMORY_SANITIZER)
20 // For MSan the slowdown depends heavily on the value of msan_track_origins GYP
21 // flag. The multiplier below corresponds to msan_track_origins=1.
22 static const int kTimeoutMultiplier = 6;
23 #elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
24 defined(SYZYASAN)
25 static const int kTimeoutMultiplier = 2;
26 #else
27 static const int kTimeoutMultiplier = 1;
28 #endif
30 const int kAlmostInfiniteTimeoutMs = 100000000;
32 // Sets value to the greatest of:
33 // 1) value's current value multiplied by kTimeoutMultiplier (assuming
34 // InitializeTimeout is called only once per value).
35 // 2) min_value.
36 // 3) the numerical value given by switch_name on the command line multiplied
37 // by kTimeoutMultiplier.
38 void InitializeTimeout(const char* switch_name, int min_value, int* value) {
39 DCHECK(value);
40 if (CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
41 std::string string_value(
42 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name));
43 int timeout;
44 base::StringToInt(string_value, &timeout);
45 *value = std::max(*value, timeout);
47 *value *= kTimeoutMultiplier;
48 *value = std::max(*value, min_value);
51 // Sets value to the greatest of:
52 // 1) value's current value multiplied by kTimeoutMultiplier.
53 // 2) 0
54 // 3) the numerical value given by switch_name on the command line multiplied
55 // by kTimeoutMultiplier.
56 void InitializeTimeout(const char* switch_name, int* value) {
57 InitializeTimeout(switch_name, 0, value);
60 } // namespace
62 // static
63 bool TestTimeouts::initialized_ = false;
65 // The timeout values should increase in the order they appear in this block.
66 // static
67 int TestTimeouts::tiny_timeout_ms_ = 100;
68 int TestTimeouts::action_timeout_ms_ = 10000;
69 #ifndef NDEBUG
70 int TestTimeouts::action_max_timeout_ms_ = 45000;
71 #else
72 int TestTimeouts::action_max_timeout_ms_ = 30000;
73 #endif // NDEBUG
75 int TestTimeouts::test_launcher_timeout_ms_ = 45000;
77 // static
78 void TestTimeouts::Initialize() {
79 if (initialized_) {
80 NOTREACHED();
81 return;
83 initialized_ = true;
85 if (base::debug::BeingDebugged()) {
86 fprintf(stdout,
87 "Detected presence of a debugger, running without test timeouts.\n");
90 // Note that these timeouts MUST be initialized in the correct order as
91 // per the CHECKS below.
92 InitializeTimeout(switches::kTestTinyTimeout, &tiny_timeout_ms_);
93 InitializeTimeout(switches::kUiTestActionTimeout,
94 base::debug::BeingDebugged() ? kAlmostInfiniteTimeoutMs
95 : tiny_timeout_ms_,
96 &action_timeout_ms_);
97 InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_ms_,
98 &action_max_timeout_ms_);
100 // Test launcher timeout is independent from anything above action timeout.
101 InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_ms_,
102 &test_launcher_timeout_ms_);
104 // The timeout values should be increasing in the right order.
105 CHECK(tiny_timeout_ms_ <= action_timeout_ms_);
106 CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
108 CHECK(action_timeout_ms_ <= test_launcher_timeout_ms_);