Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: base/test/test_mock_time_task_runner.h

Issue 899863002: Add support in TestMockTimeTaskRunner for vending out mock Time and mock Clocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ash_unittests missing header. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ 5 #ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ 6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
7 7
8 #include <queue> 8 #include <queue>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "base/test/test_pending_task.h" 15 #include "base/test/test_pending_task.h"
16 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
17 #include "base/time/tick_clock.h"
18 #include "base/time/time.h" 17 #include "base/time/time.h"
19 18
20 namespace base { 19 namespace base {
21 20
21 class Clock;
22 class TickClock;
23
22 // Runs pending tasks in the order of the tasks' post time + delay, and keeps 24 // 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 // track of a mock (virtual) tick clock time that can be fast-forwarded.
24 // 26 //
25 // TestMockTimeTaskRunner has the following properties: 27 // TestMockTimeTaskRunner has the following properties:
26 // 28 //
27 // - Methods RunsTasksOnCurrentThread() and Post[Delayed]Task() can be called 29 // - 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 30 // from any thread, but the rest of the methods must be called on the same
29 // thread the TaskRunner was created on. 31 // thread the TaskRunner was created on.
30 // - It allows for reentrancy, in that it handles the running of tasks that in 32 // - 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). 33 // turn call back into it (e.g., to post more tasks).
32 // - Tasks are stored in a priority queue, and executed in the increasing 34 // - Tasks are stored in a priority queue, and executed in the increasing
33 // order of post time + delay. 35 // order of post time + delay.
34 // - Non-nestable tasks are not supported. 36 // - Non-nestable tasks are not supported.
35 // - Tasks aren't guaranteed to be destroyed immediately after they're run. 37 // - Tasks aren't guaranteed to be destroyed immediately after they're run.
36 // 38 //
37 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in 39 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in
38 // that it supports running delayed tasks in the correct temporal order. 40 // that it supports running delayed tasks in the correct temporal order.
39 class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { 41 class TestMockTimeTaskRunner : public SingleThreadTaskRunner {
40 public: 42 public:
43 // Constructs an instance whose virtual time will start at the Unix epoch, and
44 // whose time ticks will still start at zero.
41 TestMockTimeTaskRunner(); 45 TestMockTimeTaskRunner();
42 46
47 // Constructs an instance whose virtual time will start at |initial_time|.
48 // Note that its virtual time ticks will still start at zero.
49 TestMockTimeTaskRunner(Time initial_time);
Lei Zhang 2015/02/04 20:27:37 Nobody uses this ctor. If you do keep it, add the
engedy 2015/02/05 10:22:57 Removed.
50
43 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining 51 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining
44 // delay less than or equal to |delta| to be executed. 52 // delay less than or equal to |delta| to be executed.
45 void FastForwardBy(base::TimeDelta delta); 53 void FastForwardBy(TimeDelta delta);
46 54
47 // Fast-forwards virtual time just until all tasks are executed. 55 // Fast-forwards virtual time just until all tasks are executed.
48 void FastForwardUntilNoTasksRemain(); 56 void FastForwardUntilNoTasksRemain();
49 57
50 // Executes all tasks that have no remaining delay. Tasks with a remaining 58 // 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 59 // delay greater than zero will remain enqueued, and no virtual time will
52 // elapse. 60 // elapse.
53 void RunUntilIdle(); 61 void RunUntilIdle();
54 62
55 // Returns the current virtual time. 63 // Returns the current virtual time (initially starting at the Unix epoch or
56 TimeTicks GetCurrentMockTime() const; 64 // the |initial_time| if one was specified in the constructor).
65 Time Now() const;
57 66
58 // Returns a TickClock that uses the mock time of |this| as its time source. 67 // Returns the current virtual tick time (initially starting at 0).
59 // The returned TickClock will hold a reference to |this|. 68 TimeTicks NowTicks() const;
69
70 // Returns a Clock that uses the virtual time of |this| as its time source.
71 // The returned Clock will hold a reference to |this|.
72 scoped_ptr<Clock> GetMockClock() const;
73
74 // Returns a TickClock that uses the virtual time ticks of |this| as its tick
75 // source. The returned TickClock will hold a reference to |this|.
60 scoped_ptr<TickClock> GetMockTickClock() const; 76 scoped_ptr<TickClock> GetMockTickClock() const;
61 77
62 bool HasPendingTask() const; 78 bool HasPendingTask() const;
63 size_t GetPendingTaskCount() const; 79 size_t GetPendingTaskCount() const;
64 TimeDelta NextPendingTaskDelay() const; 80 TimeDelta NextPendingTaskDelay() const;
65 81
66 // SingleThreadTaskRunner: 82 // SingleThreadTaskRunner:
67 bool RunsTasksOnCurrentThread() const override; 83 bool RunsTasksOnCurrentThread() const override;
68 bool PostDelayedTask(const tracked_objects::Location& from_here, 84 bool PostDelayedTask(const tracked_objects::Location& from_here,
69 const base::Closure& task, 85 const Closure& task,
70 TimeDelta delay) override; 86 TimeDelta delay) override;
71 bool PostNonNestableDelayedTask( 87 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
72 const tracked_objects::Location& from_here, 88 const Closure& task,
73 const base::Closure& task, 89 TimeDelta delay) override;
74 TimeDelta delay) override;
75 90
76 protected: 91 protected:
77 ~TestMockTimeTaskRunner() override; 92 ~TestMockTimeTaskRunner() override;
78 93
79 // Called before the next task to run is selected, so that subclasses have a 94 // Called before the next task to run is selected, so that subclasses have a
80 // last chance to make sure all tasks are posted. 95 // last chance to make sure all tasks are posted.
81 virtual void OnBeforeSelectingTask(); 96 virtual void OnBeforeSelectingTask();
82 97
83 // Called after the current mock time has been incremented so that subclasses 98 // Called after the current mock time has been incremented so that subclasses
84 // can react to the passing of time. 99 // can react to the passing of time.
85 virtual void OnAfterTimePassed(); 100 virtual void OnAfterTimePassed();
86 101
87 // Called after each task is run so that subclasses may perform additional 102 // Called after each task is run so that subclasses may perform additional
88 // activities, e.g., pump additional task runners. 103 // activities, e.g., pump additional task runners.
89 virtual void OnAfterTaskRun(); 104 virtual void OnAfterTaskRun();
90 105
91 private: 106 private:
92 // Predicate that defines a strict weak temporal ordering of tasks. 107 // Predicate that defines a strict weak temporal ordering of tasks.
93 class TemporalOrder { 108 class TemporalOrder {
94 public: 109 public:
95 bool operator()(const TestPendingTask& first_task, 110 bool operator()(const TestPendingTask& first_task,
96 const TestPendingTask& second_task) const; 111 const TestPendingTask& second_task) const;
97 }; 112 };
98 113
99 typedef std::priority_queue<TestPendingTask, 114 typedef std::priority_queue<TestPendingTask,
100 std::vector<TestPendingTask>, 115 std::vector<TestPendingTask>,
101 TemporalOrder> TaskPriorityQueue; 116 TemporalOrder> TaskPriorityQueue;
102 117
118 // Forwards |now_ticks_| until it equals |later_ticks|, and forwards |now_| by
119 // the same amount. Calls OnAfterTimePassed() if |now_ticks_| < |later_ticks|.
120 void ForwardClocksUntilTickTime(TimeTicks later_ticks);
121
103 // Returns the |next_task| to run if there is any with a running time that is 122 // 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 123 // at most |reference| + |max_delta|. This additional complexity is required
105 // so that |max_delta| == TimeDelta::Max() can be supported. 124 // so that |max_delta| == TimeDelta::Max() can be supported.
106 bool DequeueNextTask(const base::TimeTicks& reference, 125 bool DequeueNextTask(const TimeTicks& reference,
107 const base::TimeDelta& max_delta, 126 const TimeDelta& max_delta,
108 TestPendingTask* next_task); 127 TestPendingTask* next_task);
109 128
110 base::ThreadChecker thread_checker_; 129 ThreadChecker thread_checker_;
111 base::TimeTicks now_; 130 Time now_;
131 TimeTicks now_ticks_;
112 132
113 // Temporally ordered heap of pending tasks. Must only be accessed while the 133 // Temporally ordered heap of pending tasks. Must only be accessed while the
114 // |tasks_lock_| is held. 134 // |tasks_lock_| is held.
115 TaskPriorityQueue tasks_; 135 TaskPriorityQueue tasks_;
116 base::Lock tasks_lock_; 136 Lock tasks_lock_;
117 137
118 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); 138 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
119 }; 139 };
120 140
121 } // namespace base 141 } // namespace base
122 142
123 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ 143 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698