test_timeouts.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2012 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. #ifndef BASE_TEST_TEST_TIMEOUTS_H_
  5. #define BASE_TEST_TEST_TIMEOUTS_H_
  6. #include "butil/basictypes.h"
  7. #include "butil/logging.h"
  8. #include "butil/time/time.h"
  9. // Returns common timeouts to use in tests. Makes it possible to adjust
  10. // the timeouts for different environments (like Valgrind).
  11. class TestTimeouts {
  12. public:
  13. // Initializes the timeouts. Non thread-safe. Should be called exactly once
  14. // by the test suite.
  15. static void Initialize();
  16. // Timeout for actions that are expected to finish "almost instantly".
  17. static butil::TimeDelta tiny_timeout() {
  18. DCHECK(initialized_);
  19. return butil::TimeDelta::FromMilliseconds(tiny_timeout_ms_);
  20. }
  21. // Timeout to wait for something to happen. If you are not sure
  22. // which timeout to use, this is the one you want.
  23. static butil::TimeDelta action_timeout() {
  24. DCHECK(initialized_);
  25. return butil::TimeDelta::FromMilliseconds(action_timeout_ms_);
  26. }
  27. // Timeout longer than the above, but still suitable to use
  28. // multiple times in a single test. Use if the timeout above
  29. // is not sufficient.
  30. static butil::TimeDelta action_max_timeout() {
  31. DCHECK(initialized_);
  32. return butil::TimeDelta::FromMilliseconds(action_max_timeout_ms_);
  33. }
  34. // Timeout for a large test that may take a few minutes to run.
  35. static butil::TimeDelta large_test_timeout() {
  36. DCHECK(initialized_);
  37. return butil::TimeDelta::FromMilliseconds(large_test_timeout_ms_);
  38. }
  39. // Timeout for a single test launched used built-in test launcher.
  40. // Do not use outside of the test launcher.
  41. static butil::TimeDelta test_launcher_timeout() {
  42. DCHECK(initialized_);
  43. return butil::TimeDelta::FromMilliseconds(test_launcher_timeout_ms_);
  44. }
  45. private:
  46. static bool initialized_;
  47. static int tiny_timeout_ms_;
  48. static int action_timeout_ms_;
  49. static int action_max_timeout_ms_;
  50. static int large_test_timeout_ms_;
  51. static int test_launcher_timeout_ms_;
  52. DISALLOW_IMPLICIT_CONSTRUCTORS(TestTimeouts);
  53. };
  54. #endif // BASE_TEST_TEST_TIMEOUTS_H_