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

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

Issue 823143004: Add TestMockTimeTaskRunner in base/test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments from stevenjb@. Created 5 years, 11 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
Paweł Hajdan Jr. 2015/01/12 15:42:09 nit: 2015
engedy 2015/01/12 16:11:11 Done.
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_TEST_MOCK_TIME_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
7
8 #include <queue>
9 #include <vector>
10
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"
19
20 namespace base {
21
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.
24 //
25 // TestMockTimeTaskRunner has the following properties:
26 //
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.
36 //
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();
42
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);
46
47 // Fast-forwards virtual time just until all tasks are executed.
48 void FastForwardUntilNoTasksRemain();
49
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();
54
55 // Returns the current virtual time.
56 TimeTicks GetCurrentMockTime() const;
57
58 // Returns a TickClock that uses the mock time of |this| as its time source.
59 scoped_ptr<TickClock> GetMockTickClock() const;
60
61 bool HasPendingTask() const;
62 TimeDelta NextPendingTaskDelay() const;
63
64 // SingleThreadTaskRunner:
65 bool RunsTasksOnCurrentThread() const override;
66 bool PostDelayedTask(const tracked_objects::Location& from_here,
67 const base::Closure& task,
68 TimeDelta delay) override;
69 bool PostNonNestableDelayedTask(
70 const tracked_objects::Location& from_here,
71 const base::Closure& task,
72 TimeDelta delay) override;
73
74 protected:
75 ~TestMockTimeTaskRunner() override;
76
77 // Called before the next task to run is selected, so that subclasses have a
78 // last chance to make sure all tasks are posted.
79 virtual void OnBeforeSelectingTask();
80
81 // Called after the current mock time has been incremented so that subclasses
82 // can react to the passing of time.
83 virtual void OnAfterTimePassed();
84
85 // Called after each task is run so that subclasses may perform additional
86 // activities, e.g., pump additional task runners.
87 virtual void OnAfterTaskRun();
88
89 private:
90 // Predicate that defines a strict weak temporal ordering of tasks.
91 class TemporalOrder {
92 public:
93 bool operator()(const TestPendingTask& first_task,
94 const TestPendingTask& second_task) const;
95 };
96
97 typedef std::priority_queue<TestPendingTask,
98 std::vector<TestPendingTask>,
99 TemporalOrder> TaskPriorityQueue;
100
101 // Returns the |next_task| to run if there is any with a running time that is
102 // at most |reference| + |max_delta|. This additional complexity is required
103 // so that |max_delta| == TimeDelta::Max() can be supported.
104 bool DequeueNextTask(const base::TimeTicks& reference,
105 const base::TimeDelta& max_delta,
106 TestPendingTask* next_task);
107
108 base::ThreadChecker thread_checker_;
109 base::TimeTicks now_;
110
111 // Temporally ordered heap of pending tasks. Must only be accessed while the
112 // |tasks_lock_| is held.
113 TaskPriorityQueue tasks_;
114 base::Lock tasks_lock_;
115
116 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
117 };
118
119 } // namespace base
120
121 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698