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