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