hnguyen@chromium.org | 12b9804 | 2011-04-12 07:14:29 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 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 | |
scottmg@chromium.org | fd621b9 | 2013-10-16 05:29:03 +0900 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 9 | #include "base/command_line.h" |
jochen@chromium.org | d914478 | 2013-07-25 04:34:04 +0900 | [diff] [blame] | 10 | #include "base/debug/debugger.h" |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 11 | #include "base/logging.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 13 | #include "base/test/test_switches.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
earthdok@chromium.org | a43b6c2 | 2014-06-16 23:24:28 +0900 | [diff] [blame] | 17 | // ASan/TSan/MSan instrument each memory access. This may slow the execution |
glider@chromium.org | ab53e93 | 2013-04-19 21:56:51 +0900 | [diff] [blame] | 18 | // down significantly. |
earthdok@chromium.org | 7915acf | 2014-06-17 22:53:57 +0900 | [diff] [blame] | 19 | #if defined(MEMORY_SANITIZER) |
earthdok@chromium.org | 1fb53c2 | 2014-07-28 20:45:16 +0900 | [diff] [blame] | 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; |
earthdok@chromium.org | 7915acf | 2014-06-17 22:53:57 +0900 | [diff] [blame] | 23 | #elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 24 | defined(SYZYASAN) |
glotov@google.com | d532a0c | 2012-06-04 22:15:28 +0900 | [diff] [blame] | 25 | static const int kTimeoutMultiplier = 2; |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 26 | #else |
| 27 | static const int kTimeoutMultiplier = 1; |
| 28 | #endif |
| 29 | |
jochen@chromium.org | d914478 | 2013-07-25 04:34:04 +0900 | [diff] [blame] | 30 | const int kAlmostInfiniteTimeoutMs = 100000000; |
| 31 | |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 32 | // Sets value to the greatest of: |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 33 | // 1) value's current value multiplied by kTimeoutMultiplier (assuming |
| 34 | // InitializeTimeout is called only once per value). |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 35 | // 2) min_value. |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 36 | // 3) the numerical value given by switch_name on the command line multiplied |
| 37 | // by kTimeoutMultiplier. |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 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); |
| 46 | } |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 47 | *value *= kTimeoutMultiplier; |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 48 | *value = std::max(*value, min_value); |
| 49 | } |
| 50 | |
| 51 | // Sets value to the greatest of: |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 52 | // 1) value's current value multiplied by kTimeoutMultiplier. |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 53 | // 2) 0 |
glider@chromium.org | 0a28370 | 2011-11-15 17:43:28 +0900 | [diff] [blame] | 54 | // 3) the numerical value given by switch_name on the command line multiplied |
| 55 | // by kTimeoutMultiplier. |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 56 | void InitializeTimeout(const char* switch_name, int* value) { |
| 57 | InitializeTimeout(switch_name, 0, value); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | // static |
| 63 | bool TestTimeouts::initialized_ = false; |
| 64 | |
| 65 | // The timeout values should increase in the order they appear in this block. |
| 66 | // static |
annacc@chromium.org | 34ff140 | 2010-12-17 03:49:52 +0900 | [diff] [blame] | 67 | int TestTimeouts::tiny_timeout_ms_ = 100; |
phajdan.jr@chromium.org | 98d596a | 2011-09-13 01:51:30 +0900 | [diff] [blame] | 68 | int TestTimeouts::action_timeout_ms_ = 10000; |
phajdan.jr@chromium.org | f5f2f2b | 2013-06-13 01:40:16 +0900 | [diff] [blame] | 69 | #ifndef NDEBUG |
| 70 | int TestTimeouts::action_max_timeout_ms_ = 45000; |
| 71 | #else |
phajdan.jr@chromium.org | 6ef5d50 | 2012-11-29 03:07:01 +0900 | [diff] [blame] | 72 | int TestTimeouts::action_max_timeout_ms_ = 30000; |
phajdan.jr@chromium.org | f5f2f2b | 2013-06-13 01:40:16 +0900 | [diff] [blame] | 73 | #endif // NDEBUG |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 74 | |
phajdan.jr@chromium.org | 7fc78a6 | 2013-11-23 11:01:04 +0900 | [diff] [blame] | 75 | int TestTimeouts::test_launcher_timeout_ms_ = 45000; |
phajdan.jr@chromium.org | 9e7b5d3 | 2013-09-25 02:00:14 +0900 | [diff] [blame] | 76 | |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 77 | // static |
| 78 | void TestTimeouts::Initialize() { |
| 79 | if (initialized_) { |
| 80 | NOTREACHED(); |
| 81 | return; |
| 82 | } |
| 83 | initialized_ = true; |
| 84 | |
jochen@chromium.org | d914478 | 2013-07-25 04:34:04 +0900 | [diff] [blame] | 85 | if (base::debug::BeingDebugged()) { |
| 86 | fprintf(stdout, |
| 87 | "Detected presence of a debugger, running without test timeouts.\n"); |
| 88 | } |
| 89 | |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 90 | // Note that these timeouts MUST be initialized in the correct order as |
| 91 | // per the CHECKS below. |
annacc@chromium.org | 34ff140 | 2010-12-17 03:49:52 +0900 | [diff] [blame] | 92 | InitializeTimeout(switches::kTestTinyTimeout, &tiny_timeout_ms_); |
jochen@chromium.org | d914478 | 2013-07-25 04:34:04 +0900 | [diff] [blame] | 93 | InitializeTimeout(switches::kUiTestActionTimeout, |
| 94 | base::debug::BeingDebugged() ? kAlmostInfiniteTimeoutMs |
| 95 | : tiny_timeout_ms_, |
annacc@chromium.org | 34ff140 | 2010-12-17 03:49:52 +0900 | [diff] [blame] | 96 | &action_timeout_ms_); |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 97 | InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_ms_, |
| 98 | &action_max_timeout_ms_); |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 99 | |
phajdan.jr@chromium.org | 9e7b5d3 | 2013-09-25 02:00:14 +0900 | [diff] [blame] | 100 | // Test launcher timeout is independent from anything above action timeout. |
| 101 | InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_ms_, |
| 102 | &test_launcher_timeout_ms_); |
| 103 | |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 104 | // The timeout values should be increasing in the right order. |
annacc@chromium.org | 34ff140 | 2010-12-17 03:49:52 +0900 | [diff] [blame] | 105 | CHECK(tiny_timeout_ms_ <= action_timeout_ms_); |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 106 | CHECK(action_timeout_ms_ <= action_max_timeout_ms_); |
phajdan.jr@chromium.org | 9e7b5d3 | 2013-09-25 02:00:14 +0900 | [diff] [blame] | 107 | |
| 108 | CHECK(action_timeout_ms_ <= test_launcher_timeout_ms_); |
sergeyu@chromium.org | 0f6abbc | 2010-10-06 04:03:05 +0900 | [diff] [blame] | 109 | } |