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

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

Issue 851703002: Add TestMockTimeTaskRunner in base/test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added comment. 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
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/test_mock_time_task_runner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
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 // The returned TickClock will hold a reference to |this|.
60 scoped_ptr<TickClock> GetMockTickClock() const;
61
62 bool HasPendingTask() const;
63 TimeDelta NextPendingTaskDelay() const;
64
65 // SingleThreadTaskRunner:
66 bool RunsTasksOnCurrentThread() const override;
67 bool PostDelayedTask(const tracked_objects::Location& from_here,
68 const base::Closure& task,
69 TimeDelta delay) override;
70 bool PostNonNestableDelayedTask(
71 const tracked_objects::Location& from_here,
72 const base::Closure& task,
73 TimeDelta delay) override;
74
75 protected:
76 ~TestMockTimeTaskRunner() override;
77
78 // Called before the next task to run is selected, so that subclasses have a
79 // last chance to make sure all tasks are posted.
80 virtual void OnBeforeSelectingTask();
81
82 // Called after the current mock time has been incremented so that subclasses
83 // can react to the passing of time.
84 virtual void OnAfterTimePassed();
85
86 // Called after each task is run so that subclasses may perform additional
87 // activities, e.g., pump additional task runners.
88 virtual void OnAfterTaskRun();
89
90 private:
91 // Predicate that defines a strict weak temporal ordering of tasks.
92 class TemporalOrder {
93 public:
94 bool operator()(const TestPendingTask& first_task,
95 const TestPendingTask& second_task) const;
96 };
97
98 typedef std::priority_queue<TestPendingTask,
99 std::vector<TestPendingTask>,
100 TemporalOrder> TaskPriorityQueue;
101
102 // Returns the |next_task| to run if there is any with a running time that is
103 // at most |reference| + |max_delta|. This additional complexity is required
104 // so that |max_delta| == TimeDelta::Max() can be supported.
105 bool DequeueNextTask(const base::TimeTicks& reference,
106 const base::TimeDelta& max_delta,
107 TestPendingTask* next_task);
108
109 base::ThreadChecker thread_checker_;
110 base::TimeTicks now_;
111
112 // Temporally ordered heap of pending tasks. Must only be accessed while the
113 // |tasks_lock_| is held.
114 TaskPriorityQueue tasks_;
115 base::Lock tasks_lock_;
116
117 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
118 };
119
120 } // namespace base
121
122 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/test_mock_time_task_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698