Reland "Don't refcount tracking id -> slot id mapping."
[chromium-blink-merge.git] / base / test / test_mock_time_task_runner.h
blob2f59892b6d58d5abe943e5e5dbc5d0aee3471e3a
1 // Copyright 2015 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.
5 #ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
8 #include <queue>
9 #include <vector>
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/test/test_pending_task.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/tick_clock.h"
18 #include "base/time/time.h"
20 namespace base {
22 // Runs pending tasks in the order of the tasks' post time + delay, and keeps
23 // track of a mock (virtual) tick clock time that can be fast-forwarded.
25 // TestMockTimeTaskRunner has the following properties:
27 // - Methods RunsTasksOnCurrentThread() and Post[Delayed]Task() can be called
28 // from any thread, but the rest of the methods must be called on the same
29 // thread the TaskRunner was created on.
30 // - It allows for reentrancy, in that it handles the running of tasks that in
31 // turn call back into it (e.g., to post more tasks).
32 // - Tasks are stored in a priority queue, and executed in the increasing
33 // order of post time + delay.
34 // - Non-nestable tasks are not supported.
35 // - Tasks aren't guaranteed to be destroyed immediately after they're run.
37 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in
38 // that it supports running delayed tasks in the correct temporal order.
39 class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner {
40 public:
41 TestMockTimeTaskRunner();
43 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining
44 // delay less than or equal to |delta| to be executed.
45 void FastForwardBy(base::TimeDelta delta);
47 // Fast-forwards virtual time just until all tasks are executed.
48 void FastForwardUntilNoTasksRemain();
50 // Executes all tasks that have no remaining delay. Tasks with a remaining
51 // delay greater than zero will remain enqueued, and no virtual time will
52 // elapse.
53 void RunUntilIdle();
55 // Returns the current virtual time.
56 TimeTicks GetCurrentMockTime() const;
58 // Returns a TickClock that uses the mock time of |this| as its time source.
59 // The returned TickClock will hold a reference to |this|.
60 scoped_ptr<TickClock> GetMockTickClock() const;
62 bool HasPendingTask() const;
63 size_t GetPendingTaskCount() const;
64 TimeDelta NextPendingTaskDelay() const;
66 // SingleThreadTaskRunner:
67 bool RunsTasksOnCurrentThread() const override;
68 bool PostDelayedTask(const tracked_objects::Location& from_here,
69 const base::Closure& task,
70 TimeDelta delay) override;
71 bool PostNonNestableDelayedTask(
72 const tracked_objects::Location& from_here,
73 const base::Closure& task,
74 TimeDelta delay) override;
76 protected:
77 ~TestMockTimeTaskRunner() override;
79 // Called before the next task to run is selected, so that subclasses have a
80 // last chance to make sure all tasks are posted.
81 virtual void OnBeforeSelectingTask();
83 // Called after the current mock time has been incremented so that subclasses
84 // can react to the passing of time.
85 virtual void OnAfterTimePassed();
87 // Called after each task is run so that subclasses may perform additional
88 // activities, e.g., pump additional task runners.
89 virtual void OnAfterTaskRun();
91 private:
92 // Predicate that defines a strict weak temporal ordering of tasks.
93 class TemporalOrder {
94 public:
95 bool operator()(const TestPendingTask& first_task,
96 const TestPendingTask& second_task) const;
99 typedef std::priority_queue<TestPendingTask,
100 std::vector<TestPendingTask>,
101 TemporalOrder> TaskPriorityQueue;
103 // Returns the |next_task| to run if there is any with a running time that is
104 // at most |reference| + |max_delta|. This additional complexity is required
105 // so that |max_delta| == TimeDelta::Max() can be supported.
106 bool DequeueNextTask(const base::TimeTicks& reference,
107 const base::TimeDelta& max_delta,
108 TestPendingTask* next_task);
110 base::ThreadChecker thread_checker_;
111 base::TimeTicks now_;
113 // Temporally ordered heap of pending tasks. Must only be accessed while the
114 // |tasks_lock_| is held.
115 TaskPriorityQueue tasks_;
116 base::Lock tasks_lock_;
118 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
121 } // namespace base
123 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_