scoped_run_loop_timeout.h 3.9 KB

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