time_override.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 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_TIME_TIME_OVERRIDE_H_
  5. #define BASE_TIME_TIME_OVERRIDE_H_
  6. #include <atomic>
  7. #include "base/base_export.h"
  8. #include "base/time/time.h"
  9. #include "build/build_config.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace base {
  12. using TimeNowFunction = decltype(&Time::Now);
  13. using TimeTicksNowFunction = decltype(&TimeTicks::Now);
  14. using ThreadTicksNowFunction = decltype(&ThreadTicks::Now);
  15. // Time overrides should be used with extreme caution. Discuss with //base/time
  16. // OWNERS before adding a new one.
  17. namespace subtle {
  18. // Override the return value of Time::Now and Time::NowFromSystemTime /
  19. // TimeTicks::Now / ThreadTicks::Now to emulate time, e.g. for tests or to
  20. // modify progression of time. It is recommended that the override be set while
  21. // single-threaded and before the first call to Now() to avoid threading issues
  22. // and inconsistencies in returned values. Overriding time while other threads
  23. // are running is very subtle and should be reserved for developer only use
  24. // cases (e.g. virtual time in devtools) where any flakiness caused by a racy
  25. // time update isn't surprising. Instantiating a ScopedTimeClockOverrides while
  26. // other threads are running might break their expectation that TimeTicks and
  27. // ThreadTicks increase monotonically. Nested overrides are not allowed.
  28. class BASE_EXPORT ScopedTimeClockOverrides {
  29. public:
  30. // Pass |nullptr| for any override if it shouldn't be overriden.
  31. ScopedTimeClockOverrides(TimeNowFunction time_override,
  32. TimeTicksNowFunction time_ticks_override,
  33. ThreadTicksNowFunction thread_ticks_override);
  34. ScopedTimeClockOverrides(const ScopedTimeClockOverrides&) = delete;
  35. ScopedTimeClockOverrides& operator=(const ScopedTimeClockOverrides&) = delete;
  36. // Restores the platform default Now() functions.
  37. ~ScopedTimeClockOverrides();
  38. static bool overrides_active() { return overrides_active_; }
  39. private:
  40. static bool overrides_active_;
  41. };
  42. // These methods return the platform default Time::Now / TimeTicks::Now /
  43. // ThreadTicks::Now values even while an override is in place. These methods
  44. // should only be used in places where emulated time should be disregarded. For
  45. // example, they can be used to implement test timeouts for tests that may
  46. // override time.
  47. BASE_EXPORT Time TimeNowIgnoringOverride();
  48. BASE_EXPORT Time TimeNowFromSystemTimeIgnoringOverride();
  49. BASE_EXPORT TimeTicks TimeTicksNowIgnoringOverride();
  50. BASE_EXPORT ThreadTicks ThreadTicksNowIgnoringOverride();
  51. #if BUILDFLAG(IS_POSIX)
  52. // Equivalent to TimeTicksNowIgnoringOverride(), but is allowed to fail and
  53. // return absl::nullopt. This may safely be used in a signal handler.
  54. BASE_EXPORT absl::optional<TimeTicks> MaybeTimeTicksNowIgnoringOverride();
  55. #endif
  56. } // namespace subtle
  57. namespace internal {
  58. // These function pointers are used by platform-independent implementations of
  59. // the Now() methods and ScopedTimeClockOverrides. They are set to point to the
  60. // respective NowIgnoringOverride functions by default, but can also be set by
  61. // platform-specific code to select a default implementation at runtime, thereby
  62. // avoiding the indirection via the NowIgnoringOverride functions. Note that the
  63. // pointers can be overridden and later reset to the NowIgnoringOverride
  64. // functions by ScopedTimeClockOverrides.
  65. extern std::atomic<TimeNowFunction> g_time_now_function;
  66. extern std::atomic<TimeNowFunction> g_time_now_from_system_time_function;
  67. extern std::atomic<TimeTicksNowFunction> g_time_ticks_now_function;
  68. extern std::atomic<ThreadTicksNowFunction> g_thread_ticks_now_function;
  69. } // namespace internal
  70. } // namespace base
  71. #endif // BASE_TIME_TIME_OVERRIDE_H_