| OLD | NEW |
| 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 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 // Called after the current mock time has been incremented so that subclasses | 106 // Called after the current mock time has been incremented so that subclasses |
| 107 // can react to the passing of time. | 107 // can react to the passing of time. |
| 108 virtual void OnAfterTimePassed(); | 108 virtual void OnAfterTimePassed(); |
| 109 | 109 |
| 110 // Called after each task is run so that subclasses may perform additional | 110 // Called after each task is run so that subclasses may perform additional |
| 111 // activities, e.g., pump additional task runners. | 111 // activities, e.g., pump additional task runners. |
| 112 virtual void OnAfterTaskRun(); | 112 virtual void OnAfterTaskRun(); |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 struct TestOrderedPendingTask; |
| 116 |
| 115 // Predicate that defines a strict weak temporal ordering of tasks. | 117 // Predicate that defines a strict weak temporal ordering of tasks. |
| 116 class TemporalOrder { | 118 class TemporalOrder { |
| 117 public: | 119 public: |
| 118 bool operator()(const TestPendingTask& first_task, | 120 bool operator()(const TestOrderedPendingTask& first_task, |
| 119 const TestPendingTask& second_task) const; | 121 const TestOrderedPendingTask& second_task) const; |
| 120 }; | 122 }; |
| 121 | 123 |
| 122 typedef std::priority_queue<TestPendingTask, | 124 typedef std::priority_queue<TestOrderedPendingTask, |
| 123 std::vector<TestPendingTask>, | 125 std::vector<TestOrderedPendingTask>, |
| 124 TemporalOrder> TaskPriorityQueue; | 126 TemporalOrder> TaskPriorityQueue; |
| 125 | 127 |
| 126 // Core of the implementation for all flavors of fast-forward methods. Given a | 128 // Core of the implementation for all flavors of fast-forward methods. Given a |
| 127 // non-negative |max_delta|, runs all tasks with a remaining delay less than | 129 // non-negative |max_delta|, runs all tasks with a remaining delay less than |
| 128 // or equal to |max_delta|, and moves virtual time forward as needed for each | 130 // or equal to |max_delta|, and moves virtual time forward as needed for each |
| 129 // processed task. Pass in TimeDelta::Max() as |max_delta| to run all tasks. | 131 // processed task. Pass in TimeDelta::Max() as |max_delta| to run all tasks. |
| 130 void ProcessAllTasksNoLaterThan(TimeDelta max_delta); | 132 void ProcessAllTasksNoLaterThan(TimeDelta max_delta); |
| 131 | 133 |
| 132 // Forwards |now_ticks_| until it equals |later_ticks|, and forwards |now_| by | 134 // Forwards |now_ticks_| until it equals |later_ticks|, and forwards |now_| by |
| 133 // the same amount. Calls OnAfterTimePassed() if |later_ticks| > |now_ticks_|. | 135 // the same amount. Calls OnAfterTimePassed() if |later_ticks| > |now_ticks_|. |
| 134 // Does nothing if |later_ticks| <= |now_ticks_|. | 136 // Does nothing if |later_ticks| <= |now_ticks_|. |
| 135 void ForwardClocksUntilTickTime(TimeTicks later_ticks); | 137 void ForwardClocksUntilTickTime(TimeTicks later_ticks); |
| 136 | 138 |
| 137 // Returns the |next_task| to run if there is any with a running time that is | 139 // Returns the |next_task| to run if there is any with a running time that is |
| 138 // at most |reference| + |max_delta|. This additional complexity is required | 140 // at most |reference| + |max_delta|. This additional complexity is required |
| 139 // so that |max_delta| == TimeDelta::Max() can be supported. | 141 // so that |max_delta| == TimeDelta::Max() can be supported. |
| 140 bool DequeueNextTask(const TimeTicks& reference, | 142 bool DequeueNextTask(const TimeTicks& reference, |
| 141 const TimeDelta& max_delta, | 143 const TimeDelta& max_delta, |
| 142 TestPendingTask* next_task); | 144 TestPendingTask* next_task); |
| 143 | 145 |
| 144 ThreadChecker thread_checker_; | 146 ThreadChecker thread_checker_; |
| 145 Time now_; | 147 Time now_; |
| 146 TimeTicks now_ticks_; | 148 TimeTicks now_ticks_; |
| 147 | 149 |
| 148 // Temporally ordered heap of pending tasks. Must only be accessed while the | 150 // Temporally ordered heap of pending tasks. Must only be accessed while the |
| 149 // |tasks_lock_| is held. | 151 // |tasks_lock_| is held. |
| 150 TaskPriorityQueue tasks_; | 152 TaskPriorityQueue tasks_; |
| 153 |
| 154 // The ordinal to use for the next task. Must only be accessed while the |
| 155 // |tasks_lock_| is held. |
| 156 size_t next_task_ordinal_; |
| 157 |
| 151 Lock tasks_lock_; | 158 Lock tasks_lock_; |
| 152 | 159 |
| 153 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); | 160 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); |
| 154 }; | 161 }; |
| 155 | 162 |
| 156 } // namespace base | 163 } // namespace base |
| 157 | 164 |
| 158 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ | 165 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ |
| OLD | NEW |