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 <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/callback_helpers.h" | |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "base/test/test_pending_task.h" | 19 #include "base/test/test_pending_task.h" |
19 #include "base/threading/thread_checker_impl.h" | 20 #include "base/threading/thread_checker_impl.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; |
(...skipping 15 matching lines...) Expand all Loading... | |
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 the test body to be all in one | |
54 // 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()); | |
fdoray
2017/02/28 04:11:21
add variable name
gab
2017/02/28 19:16:03
Nice catch, done.
| |
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 ScopedClosureRunner on_destroy_; | |
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 Loading... | |
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_ |
OLD | NEW |