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