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
15namespace {
16
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.
22static const int kTimeoutMultiplier = 6;
23#elif defined(ADDRESS_SANITIZER) && defined(OS_WIN)
24// Asan/Win has not been optimized yet, give it a higher
25// timeout multiplier. See http://crbug.com/412471
26static const int kTimeoutMultiplier = 8;
27#elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
28    defined(SYZYASAN)
29static const int kTimeoutMultiplier = 2;
30#else
31static const int kTimeoutMultiplier = 1;
32#endif
33
34const int kAlmostInfiniteTimeoutMs = 100000000;
35
36// Sets value to the greatest of:
37// 1) value's current value multiplied by kTimeoutMultiplier (assuming
38// InitializeTimeout is called only once per value).
39// 2) min_value.
40// 3) the numerical value given by switch_name on the command line multiplied
41// by kTimeoutMultiplier.
42void InitializeTimeout(const char* switch_name, int min_value, int* value) {
43  DCHECK(value);
44  if (CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
45    std::string string_value(
46        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switch_name));
47    int timeout;
48    base::StringToInt(string_value, &timeout);
49    *value = std::max(*value, timeout);
50  }
51  *value *= kTimeoutMultiplier;
52  *value = std::max(*value, min_value);
53}
54
55// Sets value to the greatest of:
56// 1) value's current value multiplied by kTimeoutMultiplier.
57// 2) 0
58// 3) the numerical value given by switch_name on the command line multiplied
59// by kTimeoutMultiplier.
60void InitializeTimeout(const char* switch_name, int* value) {
61  InitializeTimeout(switch_name, 0, value);
62}
63
64}  // namespace
65
66// static
67bool TestTimeouts::initialized_ = false;
68
69// The timeout values should increase in the order they appear in this block.
70// static
71int TestTimeouts::tiny_timeout_ms_ = 100;
72int TestTimeouts::action_timeout_ms_ = 10000;
73#ifndef NDEBUG
74int TestTimeouts::action_max_timeout_ms_ = 45000;
75#else
76int TestTimeouts::action_max_timeout_ms_ = 30000;
77#endif  // NDEBUG
78
79int TestTimeouts::test_launcher_timeout_ms_ = 45000;
80
81// static
82void TestTimeouts::Initialize() {
83  if (initialized_) {
84    NOTREACHED();
85    return;
86  }
87  initialized_ = true;
88
89  if (base::debug::BeingDebugged()) {
90    fprintf(stdout,
91        "Detected presence of a debugger, running without test timeouts.\n");
92  }
93
94  // Note that these timeouts MUST be initialized in the correct order as
95  // per the CHECKS below.
96  InitializeTimeout(switches::kTestTinyTimeout, &tiny_timeout_ms_);
97  InitializeTimeout(switches::kUiTestActionTimeout,
98                    base::debug::BeingDebugged() ? kAlmostInfiniteTimeoutMs
99                                                 : tiny_timeout_ms_,
100                    &action_timeout_ms_);
101  InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_ms_,
102                    &action_max_timeout_ms_);
103
104  // Test launcher timeout is independent from anything above action timeout.
105  InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_ms_,
106                    &test_launcher_timeout_ms_);
107
108  // The timeout values should be increasing in the right order.
109  CHECK(tiny_timeout_ms_ <= action_timeout_ms_);
110  CHECK(action_timeout_ms_ <= action_max_timeout_ms_);
111
112  CHECK(action_timeout_ms_ <= test_launcher_timeout_ms_);
113}
114