task_environment.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // Copyright 2017 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_TASK_ENVIRONMENT_H_
  5. #define BASE_TEST_TASK_ENVIRONMENT_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "base/macros.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/observer_list.h"
  11. #include "base/single_thread_task_runner.h"
  12. #include "base/task/lazy_thread_pool_task_runner.h"
  13. #include "base/task/sequence_manager/sequence_manager.h"
  14. #include "base/test/scoped_run_loop_timeout.h"
  15. #include "base/threading/thread_checker.h"
  16. #include "base/time/time.h"
  17. #include "base/traits_bag.h"
  18. #include "build/build_config.h"
  19. namespace base {
  20. class Clock;
  21. class FileDescriptorWatcher;
  22. class SimpleTaskExecutor;
  23. class TickClock;
  24. namespace subtle {
  25. class ScopedTimeClockOverrides;
  26. }
  27. namespace test {
  28. // This header exposes SingleThreadTaskEnvironment and TaskEnvironment.
  29. //
  30. // SingleThreadTaskEnvironment enables the following APIs within its scope:
  31. // - (Thread|Sequenced)TaskRunnerHandle on the main thread
  32. // - RunLoop on the main thread
  33. //
  34. // TaskEnvironment additionally enables:
  35. // - posting to base::ThreadPool through base/task/thread_pool.h.
  36. //
  37. // Hint: For content::BrowserThreads, use content::BrowserTaskEnvironment.
  38. //
  39. // Tests should prefer SingleThreadTaskEnvironment over TaskEnvironment when the
  40. // former is sufficient.
  41. //
  42. // Tasks posted to the (Thread|Sequenced)TaskRunnerHandle run synchronously when
  43. // RunLoop::Run(UntilIdle) or TaskEnvironment::RunUntilIdle is called on the
  44. // main thread.
  45. //
  46. // The TaskEnvironment requires TestTimeouts::Initialize() to be called in order
  47. // to run posted tasks, so that it can watch for problematic long-running tasks.
  48. //
  49. // The TimeSource trait can be used to request that delayed tasks be under the
  50. // manual control of RunLoop::Run() and TaskEnvironment::FastForward*() methods.
  51. //
  52. // If a TaskEnvironment's ThreadPoolExecutionMode is QUEUED, ThreadPool tasks
  53. // run when RunUntilIdle() or ~TaskEnvironment is called. If
  54. // ThreadPoolExecutionMode is ASYNC, they run as they are posted.
  55. //
  56. // All TaskEnvironment methods must be called from the main thread.
  57. //
  58. // Usage:
  59. //
  60. // class MyTestFixture : public testing::Test {
  61. // public:
  62. // (...)
  63. //
  64. // // protected rather than private visibility will allow controlling the
  65. // // task environment (e.g. RunUntilIdle(), FastForwardBy(), etc.). from the
  66. // // test body.
  67. // protected:
  68. // // Must generally be the first member to be initialized first and
  69. // // destroyed last (some members that require single-threaded
  70. // // initialization and tear down may need to come before -- e.g.
  71. // // base::test::ScopedFeatureList). Extra traits, like TimeSource, are
  72. // // best provided inline when declaring the TaskEnvironment, as
  73. // // such:
  74. // base::test::TaskEnvironment task_environment_{
  75. // base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  76. //
  77. // // Other members go here (or further below in private section.)
  78. // };
  79. class TaskEnvironment {
  80. protected:
  81. // This enables a two-phase initialization for sub classes such as
  82. // content::BrowserTaskEnvironment which need to provide the default task
  83. // queue because they instantiate a scheduler on the same thread. Subclasses
  84. // using this trait must invoke DeferredInitFromSubclass() before running the
  85. // task environment.
  86. struct SubclassCreatesDefaultTaskRunner {};
  87. public:
  88. enum class TimeSource {
  89. // Delayed tasks and Time/TimeTicks::Now() use the real-time system clock.
  90. SYSTEM_TIME,
  91. // Delayed tasks use a mock clock which only advances when reaching "idle"
  92. // during a RunLoop::Run() call on the main thread or a FastForward*() call
  93. // to this TaskEnvironment. "idle" is defined as the main thread and thread
  94. // pool being out of ready tasks. In that situation : time advances to the
  95. // soonest delay between main thread and thread pool delayed tasks,
  96. // according to the semantics of the current Run*() or FastForward*() call.
  97. //
  98. // This also mocks Time/TimeTicks::Now() with the same mock clock.
  99. //
  100. // Warning some platform APIs are still real-time, e.g.:
  101. // * PlatformThread::Sleep
  102. // * WaitableEvent::TimedWait
  103. // * ConditionVariable::TimedWait
  104. // * Delayed tasks on unmanaged base::Thread's and other custom task
  105. // runners.
  106. MOCK_TIME,
  107. DEFAULT = SYSTEM_TIME
  108. };
  109. // This type will determine what types of messages will get pumped by the main
  110. // thread.
  111. // Note: If your test needs to use a custom MessagePump you should
  112. // consider using a SingleThreadTaskExecutor instead.
  113. enum class MainThreadType {
  114. // The main thread doesn't pump system messages.
  115. DEFAULT,
  116. // The main thread pumps UI messages.
  117. UI,
  118. // The main thread pumps asynchronous IO messages and supports the
  119. // FileDescriptorWatcher API on POSIX.
  120. IO,
  121. };
  122. // Note that this is irrelevant (and ignored) under
  123. // ThreadingMode::MAIN_THREAD_ONLY
  124. enum class ThreadPoolExecutionMode {
  125. // Thread pool tasks are queued and only executed when RunUntilIdle(),
  126. // FastForwardBy(), or FastForwardUntilNoTasksRemain() are explicitly
  127. // called. Note: RunLoop::Run() does *not* unblock the ThreadPool in this
  128. // mode (it strictly runs only the main thread).
  129. QUEUED,
  130. // Thread pool tasks run as they are posted. RunUntilIdle() can still be
  131. // used to block until done.
  132. // Note that regardless of this trait, delayed tasks are always "queued"
  133. // under TimeSource::MOCK_TIME mode.
  134. ASYNC,
  135. DEFAULT = ASYNC
  136. };
  137. enum class ThreadingMode {
  138. // ThreadPool will be initialized, thus adding support for multi-threaded
  139. // tests.
  140. MULTIPLE_THREADS,
  141. // No thread pool will be initialized. Useful for tests that want to run
  142. // single threaded. Prefer using SingleThreadTaskEnvironment over this
  143. // trait.
  144. MAIN_THREAD_ONLY,
  145. DEFAULT = MULTIPLE_THREADS
  146. };
  147. // On Windows, sets the COM environment for the ThreadPoolInstance. Ignored
  148. // on other platforms.
  149. enum class ThreadPoolCOMEnvironment {
  150. // Do not initialize COM for the pool's workers.
  151. NONE,
  152. // Place the pool's workers in a COM MTA.
  153. COM_MTA,
  154. // Enable the MTA by default in unit tests to match the browser process's
  155. // ThreadPoolInstance configuration.
  156. //
  157. // This has the adverse side-effect of enabling the MTA in non-browser unit
  158. // tests as well but the downside there is not as bad as not having it in
  159. // browser unit tests. It just means some COM asserts may pass in unit
  160. // tests where they wouldn't in integration tests or prod. That's okay
  161. // because unit tests are already generally very loose on allowing I/O,
  162. // waits, etc. Such misuse will still be caught in later phases (and COM
  163. // usage should already be pretty much inexistent in sandboxed processes).
  164. DEFAULT = COM_MTA,
  165. };
  166. // List of traits that are valid inputs for the constructor below.
  167. struct ValidTraits {
  168. ValidTraits(TimeSource);
  169. ValidTraits(MainThreadType);
  170. ValidTraits(ThreadPoolExecutionMode);
  171. ValidTraits(SubclassCreatesDefaultTaskRunner);
  172. ValidTraits(ThreadingMode);
  173. ValidTraits(ThreadPoolCOMEnvironment);
  174. };
  175. // Constructor accepts zero or more traits which customize the testing
  176. // environment.
  177. template <typename... TaskEnvironmentTraits,
  178. class CheckArgumentsAreValid = std::enable_if_t<
  179. trait_helpers::AreValidTraits<ValidTraits,
  180. TaskEnvironmentTraits...>::value>>
  181. NOINLINE explicit TaskEnvironment(TaskEnvironmentTraits... traits)
  182. : TaskEnvironment(
  183. trait_helpers::GetEnum<TimeSource, TimeSource::DEFAULT>(traits...),
  184. trait_helpers::GetEnum<MainThreadType, MainThreadType::DEFAULT>(
  185. traits...),
  186. trait_helpers::GetEnum<ThreadPoolExecutionMode,
  187. ThreadPoolExecutionMode::DEFAULT>(traits...),
  188. trait_helpers::GetEnum<ThreadingMode, ThreadingMode::DEFAULT>(
  189. traits...),
  190. trait_helpers::GetEnum<ThreadPoolCOMEnvironment,
  191. ThreadPoolCOMEnvironment::DEFAULT>(
  192. traits...),
  193. trait_helpers::HasTrait<SubclassCreatesDefaultTaskRunner,
  194. TaskEnvironmentTraits...>(),
  195. trait_helpers::NotATraitTag()) {}
  196. // Waits until no undelayed ThreadPool tasks remain. Then, unregisters the
  197. // ThreadPoolInstance and the (Thread|Sequenced)TaskRunnerHandle.
  198. virtual ~TaskEnvironment();
  199. // Returns a TaskRunner that schedules tasks on the main thread.
  200. scoped_refptr<base::SingleThreadTaskRunner> GetMainThreadTaskRunner();
  201. // Returns whether the main thread's TaskRunner has pending tasks. This will
  202. // always return true if called right after RunUntilIdle.
  203. bool MainThreadIsIdle() const;
  204. // Runs tasks until both the (Thread|Sequenced)TaskRunnerHandle and the
  205. // ThreadPool's non-delayed queues are empty.
  206. // While RunUntilIdle() is quite practical and sometimes even necessary -- for
  207. // example, to flush all tasks bound to Unretained() state before destroying
  208. // test members -- it should be used with caution per the following warnings:
  209. //
  210. // WARNING #1: This may run long (flakily timeout) and even never return! Do
  211. // not use this when repeating tasks such as animated web pages
  212. // are present.
  213. // WARNING #2: This may return too early! For example, if used to run until an
  214. // incoming event has occurred but that event depends on a task in
  215. // a different queue -- e.g. a standalone base::Thread or a system
  216. // event.
  217. //
  218. // As such, prefer RunLoop::Run() with an explicit RunLoop::QuitClosure() when
  219. // possible.
  220. void RunUntilIdle();
  221. // Only valid for instances using TimeSource::MOCK_TIME. Fast-forwards
  222. // virtual time by |delta|, causing all tasks on the main thread and thread
  223. // pool with a remaining delay less than or equal to |delta| to be executed in
  224. // their natural order before this returns. |delta| must be non-negative. Upon
  225. // returning from this method, NowTicks() will be >= the initial |NowTicks() +
  226. // delta|. It is guaranteed to be == iff tasks executed in this
  227. // FastForwardBy() didn't result in nested calls to time-advancing-methods.
  228. void FastForwardBy(TimeDelta delta);
  229. // Only valid for instances using TimeSource::MOCK_TIME.
  230. // Short for FastForwardBy(TimeDelta::Max()).
  231. //
  232. // WARNING: This has the same caveat as RunUntilIdle() and is even more likely
  233. // to spin forever (any RepeatingTimer will cause this).
  234. void FastForwardUntilNoTasksRemain();
  235. // Only valid for instances using TimeSource::MOCK_TIME. Advances virtual time
  236. // by |delta|. Unlike FastForwardBy, this does not run tasks. Prefer
  237. // FastForwardBy() when possible but this can be useful when testing blocked
  238. // pending tasks where being idle (required to fast-forward) is not possible.
  239. //
  240. // Delayed tasks that are ripe as a result of this will be scheduled.
  241. // RunUntilIdle() can be used after this call to ensure those tasks have run.
  242. // Note: AdvanceClock(delta) + RunUntilIdle() is slightly different from
  243. // FastForwardBy(delta) in that time passes instantly before running any task
  244. // (whereas FastForwardBy() will advance the clock in the smallest increments
  245. // possible at a time). Hence FastForwardBy() is more realistic but
  246. // AdvanceClock() can be useful when testing edge case scenarios that
  247. // specifically handle more time than expected to have passed.
  248. void AdvanceClock(TimeDelta delta);
  249. // Only valid for instances using TimeSource::MOCK_TIME. Returns a
  250. // TickClock whose time is updated by FastForward(By|UntilNoTasksRemain).
  251. const TickClock* GetMockTickClock() const;
  252. std::unique_ptr<TickClock> DeprecatedGetMockTickClock();
  253. // Only valid for instances using TimeSource::MOCK_TIME. Returns a
  254. // Clock whose time is updated by FastForward(By|UntilNoTasksRemain). The
  255. // initial value is implementation defined and should be queried by tests that
  256. // depend on it.
  257. // TickClock should be used instead of Clock to measure elapsed time in a
  258. // process. See time.h.
  259. const Clock* GetMockClock() const;
  260. // Only valid for instances using TimeSource::MOCK_TIME. Returns the current
  261. // virtual tick time (based on a realistic Now(), sampled when this
  262. // TaskEnvironment was created, and manually advanced from that point on).
  263. // This is always equivalent to base::TimeTicks::Now() under
  264. // TimeSource::MOCK_TIME.
  265. base::TimeTicks NowTicks() const;
  266. // Only valid for instances using TimeSource::MOCK_TIME. Returns the
  267. // number of pending tasks (delayed and non-delayed) of the main thread's
  268. // TaskRunner. When debugging, you can use DescribePendingMainThreadTasks() to
  269. // see what those are.
  270. size_t GetPendingMainThreadTaskCount() const;
  271. // Only valid for instances using TimeSource::MOCK_TIME.
  272. // Returns the delay until the next pending task of the main thread's
  273. // TaskRunner if there is one, otherwise it returns TimeDelta::Max().
  274. TimeDelta NextMainThreadPendingTaskDelay() const;
  275. // Only valid for instances using TimeSource::MOCK_TIME.
  276. // Returns true iff the next task is delayed. Returns false if the next task
  277. // is immediate or if there is no next task.
  278. bool NextTaskIsDelayed() const;
  279. // For debugging purposes: Dumps information about pending tasks on the main
  280. // thread.
  281. void DescribePendingMainThreadTasks() const;
  282. class DestructionObserver : public CheckedObserver {
  283. public:
  284. DestructionObserver() = default;
  285. ~DestructionObserver() override = default;
  286. DestructionObserver(const DestructionObserver&) = delete;
  287. DestructionObserver& operator=(const DestructionObserver&) = delete;
  288. virtual void WillDestroyCurrentTaskEnvironment() = 0;
  289. };
  290. // Adds/removes a DestructionObserver to any TaskEnvironment. Observers are
  291. // notified when any TaskEnvironment goes out of scope (other than with a move
  292. // operation). Must be called on the main thread.
  293. static void AddDestructionObserver(DestructionObserver* observer);
  294. static void RemoveDestructionObserver(DestructionObserver* observer);
  295. // The number of foreground workers in the ThreadPool managed by a
  296. // TaskEnvironment instance. This can be used to determine the maximum
  297. // parallelism in tests that require each parallel task it spawns to be
  298. // running at once. Having multiple threads prevents deadlocks should some
  299. // blocking APIs not use ScopedBlockingCall. It also allows enough concurrency
  300. // to allow TSAN to spot data races.
  301. static constexpr int kNumForegroundThreadPoolThreads = 4;
  302. protected:
  303. explicit TaskEnvironment(TaskEnvironment&& other);
  304. constexpr MainThreadType main_thread_type() const {
  305. return main_thread_type_;
  306. }
  307. constexpr ThreadPoolExecutionMode thread_pool_execution_mode() const {
  308. return thread_pool_execution_mode_;
  309. }
  310. // Returns the TimeDomain driving this TaskEnvironment.
  311. sequence_manager::TimeDomain* GetTimeDomain() const;
  312. sequence_manager::SequenceManager* sequence_manager() const;
  313. void DeferredInitFromSubclass(
  314. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  315. // Derived classes may need to control when the sequence manager goes away.
  316. void NotifyDestructionObserversAndReleaseSequenceManager();
  317. private:
  318. class TestTaskTracker;
  319. class MockTimeDomain;
  320. void InitializeThreadPool();
  321. void DestroyThreadPool();
  322. void CompleteInitialization();
  323. // The template constructor has to be in the header but it delegates to this
  324. // constructor to initialize all other members out-of-line.
  325. TaskEnvironment(TimeSource time_source,
  326. MainThreadType main_thread_type,
  327. ThreadPoolExecutionMode thread_pool_execution_mode,
  328. ThreadingMode threading_mode,
  329. ThreadPoolCOMEnvironment thread_pool_com_environment,
  330. bool subclass_creates_default_taskrunner,
  331. trait_helpers::NotATraitTag tag);
  332. const MainThreadType main_thread_type_;
  333. const ThreadPoolExecutionMode thread_pool_execution_mode_;
  334. const ThreadingMode threading_mode_;
  335. const ThreadPoolCOMEnvironment thread_pool_com_environment_;
  336. const bool subclass_creates_default_taskrunner_;
  337. std::unique_ptr<sequence_manager::SequenceManager> sequence_manager_;
  338. // Manages the clock under TimeSource::MOCK_TIME modes. Null in
  339. // TimeSource::SYSTEM_TIME mode.
  340. std::unique_ptr<MockTimeDomain> mock_time_domain_;
  341. // Overrides Time/TimeTicks::Now() under TimeSource::MOCK_TIME_AND_NOW mode.
  342. // Null in other modes.
  343. std::unique_ptr<subtle::ScopedTimeClockOverrides> time_overrides_;
  344. scoped_refptr<sequence_manager::TaskQueue> task_queue_;
  345. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  346. // Only set for instances using TimeSource::MOCK_TIME.
  347. std::unique_ptr<Clock> mock_clock_;
  348. #if defined(OS_POSIX) || defined(OS_FUCHSIA)
  349. // Enables the FileDescriptorWatcher API iff running a MainThreadType::IO.
  350. std::unique_ptr<FileDescriptorWatcher> file_descriptor_watcher_;
  351. #endif
  352. // Owned by the ThreadPoolInstance.
  353. TestTaskTracker* task_tracker_ = nullptr;
  354. // Ensures destruction of lazy TaskRunners when this is destroyed.
  355. std::unique_ptr<internal::ScopedLazyTaskRunnerListForTesting>
  356. scoped_lazy_task_runner_list_for_testing_;
  357. // Sets RunLoop::Run() to LOG(FATAL) if not Quit() in a timely manner.
  358. std::unique_ptr<ScopedRunLoopTimeout> run_loop_timeout_;
  359. std::unique_ptr<bool> owns_instance_ = std::make_unique<bool>(true);
  360. // To support base::CurrentThread().
  361. std::unique_ptr<SimpleTaskExecutor> simple_task_executor_;
  362. // Used to verify thread-affinity of operations that must occur on the main
  363. // thread. This is the case for anything that modifies or drives the
  364. // |sequence_manager_|.
  365. THREAD_CHECKER(main_thread_checker_);
  366. DISALLOW_COPY_AND_ASSIGN(TaskEnvironment);
  367. };
  368. // SingleThreadTaskEnvironment takes the same traits as TaskEnvironment and is
  369. // used the exact same way. It's a short-form for
  370. // TaskEnvironment{TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY, ...};
  371. class SingleThreadTaskEnvironment : public TaskEnvironment {
  372. public:
  373. template <class... ArgTypes>
  374. SingleThreadTaskEnvironment(ArgTypes... args)
  375. : TaskEnvironment(ThreadingMode::MAIN_THREAD_ONLY, args...) {}
  376. };
  377. } // namespace test
  378. } // namespace base
  379. #endif // BASE_TEST_TASK_ENVIRONMENT_H_