scoped_run_loop_timeout.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 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_SCOPED_RUN_LOOP_TIMEOUT_H_
  5. #define BASE_TEST_SCOPED_RUN_LOOP_TIMEOUT_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/run_loop.h"
  10. #include "base/time/time.h"
  11. namespace content {
  12. FORWARD_DECLARE_TEST(ContentBrowserTest, RunTimeoutInstalled);
  13. }
  14. namespace base {
  15. namespace test {
  16. FORWARD_DECLARE_TEST(TaskEnvironmentTest, SetsDefaultRunTimeout);
  17. // Configures all RunLoop::Run() calls on the current thread to run the
  18. // supplied |on_timeout| callback if they run for longer than |timeout|.
  19. //
  20. // Specifying Run() timeouts per-thread avoids the need to cope with Run()s
  21. // executing concurrently with ScopedRunLoopTimeout initialization or
  22. // teardown, and allows "default" timeouts to be specified by suites, rather
  23. // than explicitly configuring them for every RunLoop, in each test.
  24. //
  25. // This is used by test classes including TaskEnvironment and TestSuite to
  26. // set a default Run() timeout on the main thread of all tests which use them.
  27. //
  28. // Tests which have steps which need to Run() for longer than their suite's
  29. // default (if any) allows can override the active timeout by creating a nested
  30. // ScopedRunLoopTimeout on their stack, e.g:
  31. //
  32. // ScopedRunLoopTimeout default_timeout(kDefaultRunTimeout);
  33. // ... do other test stuff ...
  34. // RunLoop().Run(); // Run for up to kDefaultRunTimeout.
  35. // ...
  36. // {
  37. // ScopedRunLoopTimeout specific_timeout(kTestSpecificTimeout);
  38. // RunLoop().Run(); // Run for up to kTestSpecificTimeout.
  39. // }
  40. // ...
  41. // RunLoop().Run(); // Run for up to kDefaultRunTimeout.
  42. //
  43. // The currently-active timeout can also be temporarily disabled:
  44. // ScopedDisableRunLoopTimeout disable_timeout;
  45. //
  46. // By default LOG(FATAL) will be invoked on Run() timeout. Test binaries
  47. // can opt-in to using ADD_FAILURE() instead by calling
  48. // SetAddGTestFailureOnTimeout() during process initialization.
  49. //
  50. // TaskEnvironment applies a default Run() timeout.
  51. class ScopedRunLoopTimeout {
  52. public:
  53. ScopedRunLoopTimeout(const Location& from_here, TimeDelta timeout);
  54. ~ScopedRunLoopTimeout();
  55. // Invokes |on_timeout_log| if |timeout| expires, and appends it to the
  56. // logged error message.
  57. ScopedRunLoopTimeout(const Location& from_here,
  58. TimeDelta timeout,
  59. RepeatingCallback<std::string()> on_timeout_log);
  60. ScopedRunLoopTimeout(const ScopedRunLoopTimeout&) = delete;
  61. ScopedRunLoopTimeout& operator=(const ScopedRunLoopTimeout&) = delete;
  62. // Returns true if there is a Run() timeout configured on the current thread.
  63. static bool ExistsForCurrentThread();
  64. static void SetAddGTestFailureOnTimeout();
  65. protected:
  66. FRIEND_TEST_ALL_PREFIXES(ScopedRunLoopRunTimeoutTest, TimesOut);
  67. FRIEND_TEST_ALL_PREFIXES(ScopedRunLoopRunTimeoutTest, RunTasksUntilTimeout);
  68. FRIEND_TEST_ALL_PREFIXES(TaskEnvironmentTest, SetsDefaultRunTimeout);
  69. FRIEND_TEST_ALL_PREFIXES(content::ContentBrowserTest, RunTimeoutInstalled);
  70. // Exposes the RunLoopTimeout to the friend tests (see above).
  71. static const RunLoop::RunLoopTimeout* GetTimeoutForCurrentThread();
  72. const RunLoop::RunLoopTimeout* const nested_timeout_;
  73. RunLoop::RunLoopTimeout run_timeout_;
  74. };
  75. class ScopedDisableRunLoopTimeout {
  76. public:
  77. ScopedDisableRunLoopTimeout();
  78. ~ScopedDisableRunLoopTimeout();
  79. ScopedDisableRunLoopTimeout(const ScopedDisableRunLoopTimeout&) = delete;
  80. ScopedDisableRunLoopTimeout& operator=(const ScopedDisableRunLoopTimeout&) =
  81. delete;
  82. private:
  83. const RunLoop::RunLoopTimeout* const nested_timeout_;
  84. };
  85. } // namespace test
  86. } // namespace base
  87. #endif // BASE_TEST_SCOPED_RUN_LOOP_TIMEOUT_H_