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

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

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: nvm : still need the completion closure e.g. profile_impl_io_data.cc -> Clear() Created 3 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 <stddef.h> 8 #include <stddef.h>
9 9
10 #include <deque> 10 #include <deque>
11 #include <memory> 11 #include <memory>
12 #include <queue> 12 #include <queue>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
18 #include "base/test/test_pending_task.h" 18 #include "base/test/test_pending_task.h"
19 #include "base/threading/thread_checker_impl.h" 19 #include "base/threading/thread_checker_impl.h"
20 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 22
22 namespace base { 23 namespace base {
23 24
24 class Clock; 25 class Clock;
25 class TickClock; 26 class TickClock;
26 27
27 // Runs pending tasks in the order of the tasks' post time + delay, and keeps 28 // Runs pending tasks in the order of the tasks' post time + delay, and keeps
28 // track of a mock (virtual) tick clock time that can be fast-forwarded. 29 // track of a mock (virtual) tick clock time that can be fast-forwarded.
29 // 30 //
(...skipping 10 matching lines...) Expand all
40 // condition for preventing overflows is to make sure that the sum of all 41 // condition for preventing overflows is to make sure that the sum of all
41 // posted task delays and fast-forward increments is still representable by 42 // posted task delays and fast-forward increments is still representable by
42 // a TimeDelta, and that adding this delta to the starting values of Time 43 // a TimeDelta, and that adding this delta to the starting values of Time
43 // and TickTime is still within their respective range. 44 // and TickTime is still within their respective range.
44 // - Tasks aren't guaranteed to be destroyed immediately after they're run. 45 // - Tasks aren't guaranteed to be destroyed immediately after they're run.
45 // 46 //
46 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in 47 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in
47 // that it supports running delayed tasks in the correct temporal order. 48 // that it supports running delayed tasks in the correct temporal order.
48 class TestMockTimeTaskRunner : public SingleThreadTaskRunner { 49 class TestMockTimeTaskRunner : public SingleThreadTaskRunner {
49 public: 50 public:
51 // Everything that is executed in the scope of a ScopedContext will behave as
52 // though it ran under |scope| (i.e. ThreadTaskRunnerHandle,
53 // RunsTasksOnCurrentThread, etc.). This allows to have the test body all in
danakj 2017/02/16 17:57:24 allows having the test body or allows the test b
gab 2017/02/16 21:03:36 Done.
54 // one block when multiple TestMockTimeTaskRunners share the main thread. For
55 // example:
56 //
57 // class ExampleFixture {
58 // protected:
59 // DoBarOnFoo() {
60 // DCHECK(foo_task_runner_->RunsOnCurrentThread());
61 // EXPECT_EQ(foo_task_runner_, ThreadTaskRunnerHandle::Get());
62 // DoBar();
63 // }
64 //
65 // // Mock main task runner.
66 // base::MessageLoop message_loop_;
67 // base::ScopedMockTimeMessageLoopTaskRunner main_task_runner_;
68 //
69 // // Mock foo task runner.
70 // scoped_refptr<TestMockTimeTaskRunner> foo_task_runner_ =
71 // new TestMockTimeTaskRunner();
72 // };
73 //
74 // TEST_F(ExampleFixture, DoBarOnFoo) {
75 // DoThingsOnMain();
76 // {
77 // TestMockTimeTaskRunner::ScopedContext(foo_task_runner_.get());
78 // DoBarOnFoo();
79 // }
80 // DoMoreThingsOnMain();
81 // }
82 //
83 class ScopedContext {
84 public:
85 // Note: |scope| is ran until idle as part of this constructor to ensure
86 // that anything which runs in the underlying scope runs after any already
87 // pending tasks (the contrary would break the SequencedTraskRunner
88 // contract).
89 explicit ScopedContext(scoped_refptr<TestMockTimeTaskRunner> scope);
90 ~ScopedContext();
91
92 private:
93 ThreadTaskRunnerHandle::NestedForTesting task_runner_handle_;
94 DISALLOW_COPY_AND_ASSIGN(ScopedContext);
95 };
96
50 // Constructs an instance whose virtual time will start at the Unix epoch, and 97 // Constructs an instance whose virtual time will start at the Unix epoch, and
51 // whose time ticks will start at zero. 98 // whose time ticks will start at zero.
52 TestMockTimeTaskRunner(); 99 TestMockTimeTaskRunner();
53 100
54 // Constructs an instance starting at the given virtual time and time ticks. 101 // Constructs an instance starting at the given virtual time and time ticks.
55 TestMockTimeTaskRunner(Time start_time, TimeTicks start_ticks); 102 TestMockTimeTaskRunner(Time start_time, TimeTicks start_ticks);
56 103
57 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining 104 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining
58 // delay less than or equal to |delta| to be executed. |delta| must be 105 // delay less than or equal to |delta| to be executed. |delta| must be
59 // non-negative. 106 // non-negative.
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 size_t next_task_ordinal_; 213 size_t next_task_ordinal_;
167 214
168 Lock tasks_lock_; 215 Lock tasks_lock_;
169 216
170 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); 217 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
171 }; 218 };
172 219
173 } // namespace base 220 } // namespace base
174 221
175 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ 222 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « no previous file | base/test/test_mock_time_task_runner.cc » ('j') | base/test/test_mock_time_task_runner.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698