test_timeouts.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "base/test/test_timeouts.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include "base/check_op.h"
  8. #include "base/clang_profiling_buildflags.h"
  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. #include "build/chromeos_buildflags.h"
  16. namespace {
  17. // Sets value to the greatest of:
  18. // 1) value's current value multiplied by kTimeoutMultiplier (assuming
  19. // InitializeTimeout is called only once per value).
  20. // 2) min_value.
  21. // 3) the numerical value given by switch_name on the command line multiplied
  22. // by kTimeoutMultiplier.
  23. void InitializeTimeout(const char* switch_name,
  24. base::TimeDelta min_value,
  25. base::TimeDelta* value) {
  26. DCHECK(value);
  27. base::TimeDelta command_line_timeout;
  28. if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
  29. std::string string_value(base::CommandLine::ForCurrentProcess()->
  30. GetSwitchValueASCII(switch_name));
  31. int command_line_timeout_ms = 0;
  32. if (!base::StringToInt(string_value, &command_line_timeout_ms)) {
  33. LOG(FATAL) << "Timeout value \"" << string_value << "\" was parsed as "
  34. << command_line_timeout_ms;
  35. }
  36. command_line_timeout = base::Milliseconds(command_line_timeout_ms);
  37. }
  38. #if defined(MEMORY_SANITIZER)
  39. // ASan/TSan/MSan instrument each memory access. This may slow the execution
  40. // down significantly.
  41. // For MSan the slowdown depends heavily on the value of msan_track_origins
  42. // build flag. The multiplier below corresponds to msan_track_origins = 1.
  43. #if BUILDFLAG(IS_CHROMEOS_ASH)
  44. // A handful of tests on ChromeOS time out when using the 6x limit used
  45. // elsewhere, so it's bumped to 10x.
  46. constexpr int kTimeoutMultiplier = 10;
  47. #else
  48. constexpr int kTimeoutMultiplier = 6;
  49. #endif
  50. #elif defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_WIN)
  51. // ASan/Win has not been optimized yet, give it a higher
  52. // timeout multiplier. See http://crbug.com/412471
  53. constexpr int kTimeoutMultiplier = 3;
  54. #elif defined(ADDRESS_SANITIZER) && BUILDFLAG(IS_CHROMEOS_ASH)
  55. // A number of tests on ChromeOS run very close to the 2x limit, so ChromeOS
  56. // gets 3x.
  57. constexpr int kTimeoutMultiplier = 3;
  58. #elif defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER)
  59. constexpr int kTimeoutMultiplier = 2;
  60. #elif BUILDFLAG(CLANG_PROFILING)
  61. // On coverage build, tests run 3x slower.
  62. constexpr int kTimeoutMultiplier = 3;
  63. #elif !defined(NDEBUG) && BUILDFLAG(IS_CHROMEOS_ASH)
  64. // TODO(crbug.com/1295825): reduce the multiplier back to 2x.
  65. // A number of tests on ChromeOS run very close to the base limit, so ChromeOS
  66. // gets 4x.
  67. constexpr int kTimeoutMultiplier = 4;
  68. #elif !defined(NDEBUG) && BUILDFLAG(IS_MAC)
  69. // A lot of browser_tests on Mac debug time out.
  70. constexpr int kTimeoutMultiplier = 2;
  71. #else
  72. constexpr int kTimeoutMultiplier = 1;
  73. #endif
  74. *value = std::max(std::max(*value, command_line_timeout) * kTimeoutMultiplier,
  75. min_value);
  76. }
  77. } // namespace
  78. // static
  79. bool TestTimeouts::initialized_ = false;
  80. // The timeout values should increase in the order they appear in this block.
  81. // static
  82. base::TimeDelta TestTimeouts::tiny_timeout_ = base::Milliseconds(100);
  83. base::TimeDelta TestTimeouts::action_timeout_ = base::Seconds(10);
  84. base::TimeDelta TestTimeouts::action_max_timeout_ = base::Seconds(30);
  85. base::TimeDelta TestTimeouts::test_launcher_timeout_ = base::Seconds(45);
  86. // static
  87. void TestTimeouts::Initialize() {
  88. DCHECK(!initialized_);
  89. initialized_ = true;
  90. const bool being_debugged = base::debug::BeingDebugged();
  91. if (being_debugged) {
  92. fprintf(stdout,
  93. "Detected presence of a debugger, running without test timeouts.\n");
  94. }
  95. // Note that these timeouts MUST be initialized in the correct order as
  96. // per the CHECKS below.
  97. InitializeTimeout(switches::kTestTinyTimeout, base::TimeDelta(),
  98. &tiny_timeout_);
  99. // All timeouts other than the "tiny" one should be set to very large values
  100. // when in a debugger or when run interactively, so that tests will not get
  101. // auto-terminated. By setting the UI test action timeout to at least this
  102. // value, we guarantee the subsequent timeouts will be this large also.
  103. // Setting the "tiny" timeout to a large value as well would make some tests
  104. // hang (because it's used as a task-posting delay). In particular this
  105. // causes problems for some iOS device tests, which are always run inside a
  106. // debugger (thus BeingDebugged() is true even on the bots).
  107. base::TimeDelta min_ui_test_action_timeout = tiny_timeout_;
  108. if (being_debugged || base::CommandLine::ForCurrentProcess()->HasSwitch(
  109. switches::kTestLauncherInteractive)) {
  110. min_ui_test_action_timeout = base::Days(1);
  111. }
  112. InitializeTimeout(switches::kUiTestActionTimeout, min_ui_test_action_timeout,
  113. &action_timeout_);
  114. InitializeTimeout(switches::kUiTestActionMaxTimeout, action_timeout_,
  115. &action_max_timeout_);
  116. // Test launcher timeout is independent from anything above action timeout.
  117. InitializeTimeout(switches::kTestLauncherTimeout, action_timeout_,
  118. &test_launcher_timeout_);
  119. // The timeout values should be increasing in the right order.
  120. CHECK_LE(tiny_timeout_, action_timeout_);
  121. CHECK_LE(action_timeout_, action_max_timeout_);
  122. CHECK_LE(action_timeout_, test_launcher_timeout_);
  123. }