Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 | |
| 5 #ifndef BASE_TEST_MOCK_TIME_TASK_RUNNER_H_ | |
| 6 #define BASE_TEST_MOCK_TIME_TASK_RUNNER_H_ | |
| 7 | |
| 8 #include <queue> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: Not used.
engedy
2015/01/08 16:41:26
Done.
| |
| 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/time/tick_clock.h" | |
| 17 #include "base/time/time.h" | |
| 18 | |
| 19 namespace base { | |
| 20 | |
| 21 // Runs pending tasks in the order of the tasks' post time + delay, and keeps | |
| 22 // track of a mock (virtual) tick clock time that can be fast forwarded. | |
| 23 // | |
| 24 // TestMockTimeTaskRunner has the following properties: | |
| 25 // | |
| 26 // - PostTask() and friends can be called from any thread, but the rest of the | |
| 27 // methods must be called on the same thread the TaskRunner was created on. | |
| 28 // - It allows for reentrancy, in that it handles the running of tasks that in | |
| 29 // turn call back into it (e.g., to post more tasks). | |
| 30 // - Tasks are stored in a priority queue, and executed in the increasing | |
| 31 // order of post time + delay. | |
| 32 // - Non-nestable tasks are not supported. | |
| 33 // - Tasks aren't guaranteed to be destroyed immediately after they're run. | |
| 34 // | |
| 35 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in | |
| 36 // that it supports running delayed tasks in the correct temporal order. | |
| 37 class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { | |
| 38 public: | |
| 39 TestMockTimeTaskRunner(); | |
| 40 | |
| 41 // Fast forwards virtual time by |delta|, causing all tasks with a remaining | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: s/Fast forwards/Fast-forwards/
engedy
2015/01/08 16:41:26
Done. Also one more place in the class comment.
| |
| 42 // delay of less than or equal to |delta| to be executed. | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: s/of //
engedy
2015/01/08 16:41:25
Done.
| |
| 43 void FastForwardBy(base::TimeDelta delta); | |
| 44 | |
| 45 // Fast forwards virtual time just until all tasks are executed. | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: s/Fast forwards/Fast-forwards/
engedy
2015/01/08 16:41:26
Done.
| |
| 46 void FastForwardUntilNoTasksRemain(); | |
| 47 | |
| 48 // Executes all tasks having no remaining delay. No virtual time will elapse. | |
|
bartfab (slow)
2015/01/08 14:14:34
Nit: Could you add one more sentence which explici
engedy
2015/01/08 16:41:26
Done.
| |
| 49 void RunUntilIdle(); | |
| 50 | |
| 51 // Returns the current virtual time. | |
| 52 TimeTicks GetCurrentMockTime() const; | |
| 53 | |
| 54 // Returns a TickClock that uses the mock time of |this| as its time source. | |
| 55 scoped_ptr<TickClock> GetMockTickClock() const; | |
| 56 | |
| 57 bool HasPendingTask() const; | |
| 58 TimeDelta NextPendingTaskDelay() const; | |
| 59 | |
| 60 // SingleThreadTaskRunner: | |
| 61 virtual bool RunsTasksOnCurrentThread() const override; | |
| 62 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 63 const base::Closure& task, | |
| 64 TimeDelta delay) override; | |
| 65 virtual bool PostNonNestableDelayedTask( | |
| 66 const tracked_objects::Location& from_here, | |
| 67 const base::Closure& task, | |
| 68 TimeDelta delay) override; | |
| 69 | |
| 70 protected: | |
| 71 virtual ~TestMockTimeTaskRunner(); | |
| 72 | |
| 73 // Called before the next task to run is selected, so that subclasses have a | |
| 74 // last chance to make sure all tasks are posted. | |
| 75 virtual void OnBeforeSelectingTask(); | |
| 76 | |
| 77 // Called after the current mock time had been incremented so that subclasses | |
|
bartfab (slow)
2015/01/08 14:14:34
Nit: s/had/has/
engedy
2015/01/08 16:41:26
Done.
| |
| 78 // can react to the passing of time. | |
| 79 virtual void OnAfterTimePasses(); | |
|
bartfab (slow)
2015/01/08 14:14:34
Nit: s/Passes/Passed/
engedy
2015/01/08 16:41:25
Done.
| |
| 80 | |
| 81 // Called after each task is run so that subclasses may perform additional | |
| 82 // activities, e.g., pumping out tasks from additional task runners. | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: s/pumping out tasks from/pump/
engedy
2015/01/08 16:41:26
Done.
| |
| 83 virtual void OnAfterRunningTask(); | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: s/RunningTask/TaskRun/
engedy
2015/01/08 16:41:26
Done.
| |
| 84 | |
| 85 private: | |
| 86 // Predicate that defines a strict weak temporal ordering of tasks. | |
| 87 class TemporalOrder { | |
| 88 public: | |
| 89 bool operator()(const TestPendingTask& first_task, | |
| 90 const TestPendingTask& second_task) const; | |
| 91 }; | |
| 92 | |
| 93 typedef std::priority_queue<TestPendingTask, | |
| 94 std::vector<TestPendingTask>, | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: #include <vector>
engedy
2015/01/08 16:41:26
Done.
| |
| 95 TemporalOrder> TaskPriorityQueue; | |
| 96 | |
| 97 // Returns the |next_task| to run if there is any with a running time that is | |
| 98 // at most |reference| + |max_delta|. This additional complexity is required | |
| 99 // so that |max_delta| == TimeDelta::Max() can be supported. | |
| 100 bool DequeueNextTask(const base::TimeTicks& reference, | |
| 101 const base::TimeDelta& max_delta, | |
| 102 TestPendingTask* next_task); | |
| 103 | |
| 104 base::ThreadChecker thread_checker_; | |
|
bartfab (slow)
2015/01/08 14:14:35
Nit: #include "base/threading/thread_checker.h"
engedy
2015/01/08 16:41:26
Done.
| |
| 105 base::TimeTicks now_; | |
| 106 | |
| 107 // Temporally ordered heap of pending tasks. Must only be accessed while the | |
| 108 // |tasks_lock_| is acquired. | |
|
bartfab (slow)
2015/01/08 14:14:34
Nit: s/acquired/held/
engedy
2015/01/08 16:41:26
Done.
| |
| 109 TaskPriorityQueue tasks_; | |
| 110 base::Lock tasks_lock_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); | |
| 113 }; | |
| 114 | |
| 115 } // namespace base | |
| 116 | |
| 117 #endif // BASE_TEST_TEMPORAL_ORDER_TASK_RUNNER_H_ | |
| OLD | NEW |