test_simple_task_runner.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_SIMPLE_TASK_RUNNER_H_
  5. #define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
  6. #include "base/callback.h"
  7. #include "base/compiler_specific.h"
  8. #include "base/containers/circular_deque.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/test/test_pending_task.h"
  12. #include "base/threading/platform_thread.h"
  13. namespace base {
  14. class TimeDelta;
  15. // ATTENTION: Prefer using base::test::TaskEnvironment and a task runner
  16. // obtained from base/task/thread_pool.h over this class. This class isn't as
  17. // "simple" as it seems specifically because it runs tasks in a surprising order
  18. // (delays aren't respected and nesting doesn't behave as usual). Should you
  19. // prefer to flush all tasks regardless of delays,
  20. // TaskEnvironment::TimeSource::MOCK_TIME and
  21. // TaskEnvironment::FastForwardUntilNoTasksRemain() have you covered.
  22. //
  23. // TestSimpleTaskRunner is a simple TaskRunner implementation that can
  24. // be used for testing. It implements SingleThreadTaskRunner as that
  25. // interface implements SequencedTaskRunner, which in turn implements
  26. // TaskRunner, so TestSimpleTaskRunner can be passed in to a function
  27. // that accepts any *TaskRunner object.
  28. //
  29. // TestSimpleTaskRunner has the following properties which make it simple:
  30. //
  31. // - Tasks are simply stored in a queue in FIFO order, ignoring delay
  32. // and nestability.
  33. // - Tasks aren't guaranteed to be destroyed immediately after
  34. // they're run.
  35. //
  36. // However, TestSimpleTaskRunner allows for reentrancy, in that it
  37. // handles the running of tasks that in turn call back into itself
  38. // (e.g., to post more tasks).
  39. //
  40. // Note that, like any TaskRunner, TestSimpleTaskRunner is
  41. // ref-counted.
  42. class TestSimpleTaskRunner : public SingleThreadTaskRunner {
  43. public:
  44. TestSimpleTaskRunner();
  45. TestSimpleTaskRunner(const TestSimpleTaskRunner&) = delete;
  46. TestSimpleTaskRunner& operator=(const TestSimpleTaskRunner&) = delete;
  47. // SingleThreadTaskRunner implementation.
  48. bool PostDelayedTask(const Location& from_here,
  49. OnceClosure task,
  50. TimeDelta delay) override;
  51. bool PostNonNestableDelayedTask(const Location& from_here,
  52. OnceClosure task,
  53. TimeDelta delay) override;
  54. bool RunsTasksInCurrentSequence() const override;
  55. base::circular_deque<TestPendingTask> TakePendingTasks();
  56. size_t NumPendingTasks() const;
  57. bool HasPendingTask() const;
  58. base::TimeDelta NextPendingTaskDelay() const;
  59. base::TimeDelta FinalPendingTaskDelay() const;
  60. // Clears the queue of pending tasks without running them.
  61. void ClearPendingTasks();
  62. // Runs each current pending task in order and clears the queue. Tasks posted
  63. // by the tasks that run within this call do not run within this call. Can
  64. // only be called on the thread that created this TestSimpleTaskRunner.
  65. void RunPendingTasks();
  66. // Runs pending tasks until the queue is empty. Can only be called on the
  67. // thread that created this TestSimpleTaskRunner.
  68. void RunUntilIdle();
  69. protected:
  70. ~TestSimpleTaskRunner() override;
  71. private:
  72. // Thread on which this was instantiated.
  73. const PlatformThreadRef thread_ref_ = PlatformThread::CurrentRef();
  74. // Synchronizes access to |pending_tasks_|.
  75. mutable Lock lock_;
  76. base::circular_deque<TestPendingTask> pending_tasks_;
  77. };
  78. } // namespace base
  79. #endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_