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