OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "components/timers/alarm_timer.h" | 12 #include "components/timers/alarm_timer_chromeos.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 // Most of these tests have been lifted right out of timer_unittest.cc with only | 15 // Most of these tests have been lifted right out of timer_unittest.cc with only |
16 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with | 16 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with |
17 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the | 17 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the |
18 // regular Timer so it should pass the same tests as the Timer class. | 18 // regular Timer so it should pass the same tests as the Timer class. |
19 // | 19 // |
20 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test | 20 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test |
21 // that race conditions that can come up in the AlarmTimer::Delegate are | 21 // that race conditions that can come up in the AlarmTimer::Delegate are |
22 // properly handled. | 22 // properly handled. |
23 namespace timers { | 23 namespace timers { |
24 namespace { | 24 namespace { |
25 // The message loops on which each timer should be tested. | 25 // The message loops on which each timer should be tested. |
26 const base::MessageLoop::Type testing_message_loops[] = { | 26 const base::MessageLoop::Type testing_message_loops[] = { |
27 base::MessageLoop::TYPE_DEFAULT, | 27 base::MessageLoop::TYPE_DEFAULT, |
28 base::MessageLoop::TYPE_IO, | 28 base::MessageLoop::TYPE_IO, |
29 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | 29 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
30 base::MessageLoop::TYPE_UI, | 30 base::MessageLoop::TYPE_UI, |
31 #endif | 31 #endif |
32 }; | 32 }; |
33 | 33 |
34 const int kNumTestingMessageLoops = arraysize(testing_message_loops); | 34 const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
35 const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10); | 35 const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10); |
36 | 36 |
37 class OneShotAlarmTimerTester { | 37 class OneShotAlarmTimerTester { |
38 public: | 38 public: |
39 OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 39 OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
40 : did_run_(did_run), | 40 : did_run_(did_run), |
41 delay_(delay), | 41 delay_(delay), |
42 timer_(new timers::AlarmTimer(false, false)) { | 42 timer_(new timers::OneShotAlarmTimer()) {} |
43 } | |
44 void Start() { | 43 void Start() { |
45 timer_->Start(FROM_HERE, | 44 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, |
46 delay_, | 45 base::Unretained(this))); |
47 base::Bind(&OneShotAlarmTimerTester::Run, | |
48 base::Unretained(this))); | |
49 } | 46 } |
50 | 47 |
51 private: | 48 private: |
52 void Run() { | 49 void Run() { |
53 *did_run_ = true; | 50 *did_run_ = true; |
54 | 51 |
55 base::MessageLoop::current()->PostTask( | 52 base::MessageLoop::current()->PostTask( |
56 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 53 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
57 } | 54 } |
58 | 55 |
59 bool* did_run_; | 56 bool* did_run_; |
60 const base::TimeDelta delay_; | 57 const base::TimeDelta delay_; |
61 scoped_ptr<timers::AlarmTimer> timer_; | 58 scoped_ptr<timers::OneShotAlarmTimer> timer_; |
62 | 59 |
63 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); | 60 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
64 }; | 61 }; |
65 | 62 |
66 class OneShotSelfDeletingAlarmTimerTester { | 63 class OneShotSelfDeletingAlarmTimerTester { |
67 public: | 64 public: |
68 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 65 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
69 : did_run_(did_run), | 66 : did_run_(did_run), |
70 delay_(delay), | 67 delay_(delay), |
71 timer_(new timers::AlarmTimer(false, false)) { | 68 timer_(new timers::OneShotAlarmTimer()) {} |
72 } | |
73 void Start() { | 69 void Start() { |
74 timer_->Start(FROM_HERE, | 70 timer_->Start(FROM_HERE, delay_, |
75 delay_, | |
76 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, | 71 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
77 base::Unretained(this))); | 72 base::Unretained(this))); |
78 } | 73 } |
79 | 74 |
80 private: | 75 private: |
81 void Run() { | 76 void Run() { |
82 *did_run_ = true; | 77 *did_run_ = true; |
83 timer_.reset(); | 78 timer_.reset(); |
84 | 79 |
85 base::MessageLoop::current()->PostTask( | 80 base::MessageLoop::current()->PostTask( |
86 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 81 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
87 } | 82 } |
88 | 83 |
89 bool* did_run_; | 84 bool* did_run_; |
90 const base::TimeDelta delay_; | 85 const base::TimeDelta delay_; |
91 scoped_ptr<timers::AlarmTimer> timer_; | 86 scoped_ptr<timers::OneShotAlarmTimer> timer_; |
92 | 87 |
93 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); | 88 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
94 }; | 89 }; |
95 | 90 |
96 class RepeatingAlarmTimerTester { | 91 class RepeatingAlarmTimerTester { |
97 public: | 92 public: |
98 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 93 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
99 : did_run_(did_run), | 94 : did_run_(did_run), |
100 delay_(delay), | 95 delay_(delay), |
101 counter_(10), | 96 counter_(10), |
102 timer_(new timers::AlarmTimer(true, true)) { | 97 timer_(new timers::RepeatingAlarmTimer()) {} |
103 } | |
104 void Start() { | 98 void Start() { |
105 timer_->Start(FROM_HERE, | 99 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, |
106 delay_, | 100 base::Unretained(this))); |
107 base::Bind(&RepeatingAlarmTimerTester::Run, | |
108 base::Unretained(this))); | |
109 } | 101 } |
110 | 102 |
111 private: | 103 private: |
112 void Run() { | 104 void Run() { |
113 if (--counter_ == 0) { | 105 if (--counter_ == 0) { |
114 *did_run_ = true; | 106 *did_run_ = true; |
115 timer_->Stop(); | 107 timer_->Stop(); |
116 | 108 |
117 base::MessageLoop::current()->PostTask( | 109 base::MessageLoop::current()->PostTask( |
118 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 110 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
119 } | 111 } |
120 } | 112 } |
121 | 113 |
122 bool* did_run_; | 114 bool* did_run_; |
123 const base::TimeDelta delay_; | 115 const base::TimeDelta delay_; |
124 int counter_; | 116 int counter_; |
125 scoped_ptr<timers::AlarmTimer> timer_; | 117 scoped_ptr<timers::RepeatingAlarmTimer> timer_; |
126 | 118 |
127 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); | 119 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
128 }; | 120 }; |
129 | 121 |
130 void RunTest_OneShotAlarmTimer(base::MessageLoop::Type message_loop_type) { | |
131 base::MessageLoop loop(message_loop_type); | |
132 | |
133 bool did_run = false; | |
134 OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); | |
135 f.Start(); | |
136 | |
137 base::RunLoop().Run(); | |
138 | |
139 EXPECT_TRUE(did_run); | |
140 } | |
141 | |
142 void RunTest_OneShotAlarmTimer_Cancel( | |
143 base::MessageLoop::Type message_loop_type) { | |
144 base::MessageLoop loop(message_loop_type); | |
145 | |
146 bool did_run_a = false; | |
147 OneShotAlarmTimerTester* a = new OneShotAlarmTimerTester(&did_run_a, | |
148 kTenMilliseconds); | |
149 | |
150 // This should run before the timer expires. | |
151 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | |
152 | |
153 // Now start the timer. | |
154 a->Start(); | |
155 | |
156 bool did_run_b = false; | |
157 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); | |
158 b.Start(); | |
159 | |
160 base::RunLoop().Run(); | |
161 | |
162 EXPECT_FALSE(did_run_a); | |
163 EXPECT_TRUE(did_run_b); | |
164 } | |
165 | |
166 void RunTest_OneShotSelfDeletingAlarmTimer( | |
167 base::MessageLoop::Type message_loop_type) { | |
168 base::MessageLoop loop(message_loop_type); | |
169 | |
170 bool did_run = false; | |
171 OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds); | |
172 f.Start(); | |
173 | |
174 base::RunLoop().Run(); | |
175 | |
176 EXPECT_TRUE(did_run); | |
177 } | |
178 | |
179 void RunTest_RepeatingAlarmTimer(base::MessageLoop::Type message_loop_type, | |
180 const base::TimeDelta& delay) { | |
181 base::MessageLoop loop(message_loop_type); | |
182 | |
183 bool did_run = false; | |
184 RepeatingAlarmTimerTester f(&did_run, delay); | |
185 f.Start(); | |
186 | |
187 base::RunLoop().Run(); | |
188 | |
189 EXPECT_TRUE(did_run); | |
190 } | |
191 | |
192 void RunTest_RepeatingAlarmTimer_Cancel( | |
193 base::MessageLoop::Type message_loop_type, const base::TimeDelta& delay) { | |
194 base::MessageLoop loop(message_loop_type); | |
195 | |
196 bool did_run_a = false; | |
197 RepeatingAlarmTimerTester* a = new RepeatingAlarmTimerTester(&did_run_a, | |
198 delay); | |
199 | |
200 // This should run before the timer expires. | |
201 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | |
202 | |
203 // Now start the timer. | |
204 a->Start(); | |
205 | |
206 bool did_run_b = false; | |
207 RepeatingAlarmTimerTester b(&did_run_b, delay); | |
208 b.Start(); | |
209 | |
210 base::RunLoop().Run(); | |
211 | |
212 EXPECT_FALSE(did_run_a); | |
213 EXPECT_TRUE(did_run_b); | |
214 } | |
215 | |
216 } // namespace | 122 } // namespace |
217 | 123 |
218 //----------------------------------------------------------------------------- | 124 //----------------------------------------------------------------------------- |
219 // Each test is run against each type of MessageLoop. That way we are sure | 125 // Each test is run against each type of MessageLoop. That way we are sure |
220 // that timers work properly in all configurations. | 126 // that timers work properly in all configurations. |
221 | 127 |
222 TEST(AlarmTimerTest, OneShotAlarmTimer) { | 128 TEST(AlarmTimerTest, OneShotAlarmTimer) { |
223 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 129 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
224 RunTest_OneShotAlarmTimer(testing_message_loops[i]); | 130 base::MessageLoop loop(testing_message_loops[i]); |
| 131 |
| 132 bool did_run = false; |
| 133 OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 134 f.Start(); |
| 135 |
| 136 base::RunLoop().Run(); |
| 137 |
| 138 EXPECT_TRUE(did_run); |
225 } | 139 } |
226 } | 140 } |
227 | 141 |
228 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { | 142 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { |
229 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 143 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
230 RunTest_OneShotAlarmTimer_Cancel(testing_message_loops[i]); | 144 base::MessageLoop loop(testing_message_loops[i]); |
| 145 |
| 146 bool did_run_a = false; |
| 147 OneShotAlarmTimerTester* a = |
| 148 new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); |
| 149 |
| 150 // This should run before the timer expires. |
| 151 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
| 152 |
| 153 // Now start the timer. |
| 154 a->Start(); |
| 155 |
| 156 bool did_run_b = false; |
| 157 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
| 158 b.Start(); |
| 159 |
| 160 base::RunLoop().Run(); |
| 161 |
| 162 EXPECT_FALSE(did_run_a); |
| 163 EXPECT_TRUE(did_run_b); |
231 } | 164 } |
232 } | 165 } |
233 | 166 |
234 // If underlying timer does not handle this properly, we will crash or fail | 167 // If underlying timer does not handle this properly, we will crash or fail |
235 // in full page heap environment. | 168 // in full page heap environment. |
236 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { | 169 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { |
237 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 170 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
238 RunTest_OneShotSelfDeletingAlarmTimer(testing_message_loops[i]); | 171 base::MessageLoop loop(testing_message_loops[i]); |
| 172 |
| 173 bool did_run = false; |
| 174 OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 175 f.Start(); |
| 176 |
| 177 base::RunLoop().Run(); |
| 178 |
| 179 EXPECT_TRUE(did_run); |
239 } | 180 } |
240 } | 181 } |
241 | 182 |
242 TEST(AlarmTimerTest, RepeatingAlarmTimer) { | 183 TEST(AlarmTimerTest, RepeatingAlarmTimer) { |
243 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 184 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
244 RunTest_RepeatingAlarmTimer(testing_message_loops[i], | 185 base::MessageLoop loop(testing_message_loops[i]); |
245 kTenMilliseconds); | 186 |
| 187 bool did_run = false; |
| 188 RepeatingAlarmTimerTester f(&did_run, kTenMilliseconds); |
| 189 f.Start(); |
| 190 |
| 191 base::RunLoop().Run(); |
| 192 |
| 193 EXPECT_TRUE(did_run); |
246 } | 194 } |
247 } | 195 } |
248 | 196 |
249 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { | 197 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
250 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 198 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
251 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], | 199 base::MessageLoop loop(testing_message_loops[i]); |
252 kTenMilliseconds); | 200 |
| 201 bool did_run_a = false; |
| 202 RepeatingAlarmTimerTester* a = |
| 203 new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds); |
| 204 |
| 205 // This should run before the timer expires. |
| 206 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
| 207 |
| 208 // Now start the timer. |
| 209 a->Start(); |
| 210 |
| 211 bool did_run_b = false; |
| 212 RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
| 213 b.Start(); |
| 214 |
| 215 base::RunLoop().Run(); |
| 216 |
| 217 EXPECT_FALSE(did_run_a); |
| 218 EXPECT_TRUE(did_run_b); |
253 } | 219 } |
254 } | 220 } |
255 | 221 |
256 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { | 222 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { |
257 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 223 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
258 RunTest_RepeatingAlarmTimer(testing_message_loops[i], | 224 base::MessageLoop loop(testing_message_loops[i]); |
259 base::TimeDelta::FromMilliseconds(0)); | 225 |
| 226 bool did_run = false; |
| 227 RepeatingAlarmTimerTester f(&did_run, base::TimeDelta()); |
| 228 f.Start(); |
| 229 |
| 230 base::RunLoop().Run(); |
| 231 |
| 232 EXPECT_TRUE(did_run); |
260 } | 233 } |
261 } | 234 } |
262 | 235 |
263 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { | 236 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { |
264 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 237 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
265 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], | 238 base::MessageLoop loop(testing_message_loops[i]); |
266 base::TimeDelta::FromMilliseconds(0)); | 239 |
| 240 bool did_run_a = false; |
| 241 RepeatingAlarmTimerTester* a = |
| 242 new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta()); |
| 243 |
| 244 // This should run before the timer expires. |
| 245 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
| 246 |
| 247 // Now start the timer. |
| 248 a->Start(); |
| 249 |
| 250 bool did_run_b = false; |
| 251 RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta()); |
| 252 b.Start(); |
| 253 |
| 254 base::RunLoop().Run(); |
| 255 |
| 256 EXPECT_FALSE(did_run_a); |
| 257 EXPECT_TRUE(did_run_b); |
267 } | 258 } |
268 } | 259 } |
269 | 260 |
270 TEST(AlarmTimerTest, MessageLoopShutdown) { | 261 TEST(AlarmTimerTest, MessageLoopShutdown) { |
271 // This test is designed to verify that shutdown of the | 262 // This test is designed to verify that shutdown of the |
272 // message loop does not cause crashes if there were pending | 263 // message loop does not cause crashes if there were pending |
273 // timers not yet fired. It may only trigger exceptions | 264 // timers not yet fired. It may only trigger exceptions |
274 // if debug heap checking is enabled. | 265 // if debug heap checking is enabled. |
275 bool did_run = false; | 266 bool did_run = false; |
276 { | 267 { |
277 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); | 268 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); |
278 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); | 269 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); |
279 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); | 270 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); |
280 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); | 271 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); |
281 { | 272 { |
282 base::MessageLoop loop; | 273 base::MessageLoop loop; |
283 a.Start(); | 274 a.Start(); |
284 b.Start(); | 275 b.Start(); |
285 } // MessageLoop destructs by falling out of scope. | 276 } // MessageLoop destructs by falling out of scope. |
286 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. | 277 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
287 | 278 |
288 EXPECT_FALSE(did_run); | 279 EXPECT_FALSE(did_run); |
289 } | 280 } |
290 | 281 |
291 TEST(AlarmTimerTest, NonRepeatIsRunning) { | 282 TEST(AlarmTimerTest, NonRepeatIsRunning) { |
292 { | 283 { |
293 base::MessageLoop loop; | 284 base::MessageLoop loop; |
294 timers::AlarmTimer timer(false, false); | 285 timers::OneShotAlarmTimer timer; |
295 EXPECT_FALSE(timer.IsRunning()); | 286 EXPECT_FALSE(timer.IsRunning()); |
296 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 287 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
297 base::Bind(&base::DoNothing)); | 288 base::Bind(&base::DoNothing)); |
298 EXPECT_TRUE(timer.IsRunning()); | 289 EXPECT_TRUE(timer.IsRunning()); |
299 timer.Stop(); | 290 timer.Stop(); |
300 EXPECT_FALSE(timer.IsRunning()); | 291 EXPECT_FALSE(timer.IsRunning()); |
301 EXPECT_TRUE(timer.user_task().is_null()); | 292 EXPECT_TRUE(timer.user_task().is_null()); |
302 } | 293 } |
303 | 294 |
304 { | 295 { |
305 timers::AlarmTimer timer(true, false); | 296 timers::SimpleAlarmTimer timer; |
306 base::MessageLoop loop; | 297 base::MessageLoop loop; |
307 EXPECT_FALSE(timer.IsRunning()); | 298 EXPECT_FALSE(timer.IsRunning()); |
308 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 299 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
309 base::Bind(&base::DoNothing)); | 300 base::Bind(&base::DoNothing)); |
310 EXPECT_TRUE(timer.IsRunning()); | 301 EXPECT_TRUE(timer.IsRunning()); |
311 timer.Stop(); | 302 timer.Stop(); |
312 EXPECT_FALSE(timer.IsRunning()); | 303 EXPECT_FALSE(timer.IsRunning()); |
313 ASSERT_FALSE(timer.user_task().is_null()); | 304 ASSERT_FALSE(timer.user_task().is_null()); |
314 timer.Reset(); | 305 timer.Reset(); |
315 EXPECT_TRUE(timer.IsRunning()); | 306 EXPECT_TRUE(timer.IsRunning()); |
316 } | 307 } |
317 } | 308 } |
318 | 309 |
319 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { | 310 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { |
320 timers::AlarmTimer timer(false, false); | 311 timers::OneShotAlarmTimer timer; |
321 { | 312 { |
322 base::MessageLoop loop; | 313 base::MessageLoop loop; |
323 EXPECT_FALSE(timer.IsRunning()); | 314 EXPECT_FALSE(timer.IsRunning()); |
324 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 315 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
325 base::Bind(&base::DoNothing)); | 316 base::Bind(&base::DoNothing)); |
326 EXPECT_TRUE(timer.IsRunning()); | 317 EXPECT_TRUE(timer.IsRunning()); |
327 } | 318 } |
328 EXPECT_FALSE(timer.IsRunning()); | 319 EXPECT_FALSE(timer.IsRunning()); |
329 EXPECT_TRUE(timer.user_task().is_null()); | 320 EXPECT_TRUE(timer.user_task().is_null()); |
330 } | 321 } |
331 | 322 |
332 TEST(AlarmTimerTest, RetainRepeatIsRunning) { | 323 TEST(AlarmTimerTest, RetainRepeatIsRunning) { |
333 base::MessageLoop loop; | 324 base::MessageLoop loop; |
334 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 325 timers::RepeatingAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
335 base::Bind(&base::DoNothing), true); | 326 base::Bind(&base::DoNothing)); |
336 EXPECT_FALSE(timer.IsRunning()); | 327 EXPECT_FALSE(timer.IsRunning()); |
337 timer.Reset(); | 328 timer.Reset(); |
338 EXPECT_TRUE(timer.IsRunning()); | 329 EXPECT_TRUE(timer.IsRunning()); |
339 timer.Stop(); | 330 timer.Stop(); |
340 EXPECT_FALSE(timer.IsRunning()); | 331 EXPECT_FALSE(timer.IsRunning()); |
341 timer.Reset(); | 332 timer.Reset(); |
342 EXPECT_TRUE(timer.IsRunning()); | 333 EXPECT_TRUE(timer.IsRunning()); |
343 } | 334 } |
344 | 335 |
345 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { | 336 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { |
346 base::MessageLoop loop; | 337 base::MessageLoop loop; |
347 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 338 timers::SimpleAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
348 base::Bind(&base::DoNothing), false); | 339 base::Bind(&base::DoNothing)); |
349 EXPECT_FALSE(timer.IsRunning()); | 340 EXPECT_FALSE(timer.IsRunning()); |
350 timer.Reset(); | 341 timer.Reset(); |
351 EXPECT_TRUE(timer.IsRunning()); | 342 EXPECT_TRUE(timer.IsRunning()); |
352 timer.Stop(); | 343 timer.Stop(); |
353 EXPECT_FALSE(timer.IsRunning()); | 344 EXPECT_FALSE(timer.IsRunning()); |
354 timer.Reset(); | 345 timer.Reset(); |
355 EXPECT_TRUE(timer.IsRunning()); | 346 EXPECT_TRUE(timer.IsRunning()); |
356 } | 347 } |
357 | 348 |
358 namespace { | 349 namespace { |
(...skipping 15 matching lines...) Expand all Loading... |
374 void SetCallbackHappened2() { | 365 void SetCallbackHappened2() { |
375 g_callback_happened2 = true; | 366 g_callback_happened2 = true; |
376 base::MessageLoop::current()->PostTask( | 367 base::MessageLoop::current()->PostTask( |
377 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 368 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
378 } | 369 } |
379 | 370 |
380 TEST(AlarmTimerTest, ContinuationStopStart) { | 371 TEST(AlarmTimerTest, ContinuationStopStart) { |
381 { | 372 { |
382 ClearAllCallbackHappened(); | 373 ClearAllCallbackHappened(); |
383 base::MessageLoop loop; | 374 base::MessageLoop loop; |
384 timers::AlarmTimer timer(false, false); | 375 timers::OneShotAlarmTimer timer; |
385 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 376 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
386 base::Bind(&SetCallbackHappened1)); | 377 base::Bind(&SetCallbackHappened1)); |
387 timer.Stop(); | 378 timer.Stop(); |
388 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), | 379 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), |
389 base::Bind(&SetCallbackHappened2)); | 380 base::Bind(&SetCallbackHappened2)); |
390 base::RunLoop().Run(); | 381 base::RunLoop().Run(); |
391 EXPECT_FALSE(g_callback_happened1); | 382 EXPECT_FALSE(g_callback_happened1); |
392 EXPECT_TRUE(g_callback_happened2); | 383 EXPECT_TRUE(g_callback_happened2); |
393 } | 384 } |
394 } | 385 } |
395 | 386 |
396 TEST(AlarmTimerTest, ContinuationReset) { | 387 TEST(AlarmTimerTest, ContinuationReset) { |
397 { | 388 { |
398 ClearAllCallbackHappened(); | 389 ClearAllCallbackHappened(); |
399 base::MessageLoop loop; | 390 base::MessageLoop loop; |
400 timers::AlarmTimer timer(false, false); | 391 timers::OneShotAlarmTimer timer; |
401 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 392 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
402 base::Bind(&SetCallbackHappened1)); | 393 base::Bind(&SetCallbackHappened1)); |
403 timer.Reset(); | 394 timer.Reset(); |
404 // Since Reset happened before task ran, the user_task must not be cleared: | 395 // Since Reset happened before task ran, the user_task must not be cleared: |
405 ASSERT_FALSE(timer.user_task().is_null()); | 396 ASSERT_FALSE(timer.user_task().is_null()); |
406 base::RunLoop().Run(); | 397 base::RunLoop().Run(); |
407 EXPECT_TRUE(g_callback_happened1); | 398 EXPECT_TRUE(g_callback_happened1); |
408 } | 399 } |
409 } | 400 } |
410 | 401 |
411 } // namespace | 402 } // namespace |
412 | 403 |
413 | |
414 namespace { | 404 namespace { |
415 void TimerRanCallback(bool* did_run) { | 405 void TimerRanCallback(bool* did_run) { |
416 *did_run = true; | 406 *did_run = true; |
417 | 407 |
418 base::MessageLoop::current()->PostTask( | 408 base::MessageLoop::current()->PostTask( |
419 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 409 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
420 } | 410 } |
421 | 411 |
422 void RunTest_OneShotTimerConcurrentResetAndTimerFired( | |
423 base::MessageLoop::Type message_loop_type) { | |
424 base::MessageLoop loop(message_loop_type); | |
425 | |
426 timers::AlarmTimer timer(false, false); | |
427 bool did_run = false; | |
428 | |
429 timer.Start( | |
430 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); | |
431 | |
432 // Sleep twice as long as the timer to ensure that the timer task gets queued. | |
433 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | |
434 | |
435 // Now reset the timer. This is attempting to simulate the timer firing and | |
436 // being reset at the same time. The previously queued task should be | |
437 // removed. | |
438 timer.Reset(); | |
439 | |
440 base::RunLoop().RunUntilIdle(); | |
441 EXPECT_FALSE(did_run); | |
442 | |
443 // If the previous check failed, running the message loop again will hang the | |
444 // test so we only do it if the callback has not run yet. | |
445 if (!did_run) { | |
446 base::RunLoop().Run(); | |
447 EXPECT_TRUE(did_run); | |
448 } | |
449 } | |
450 | |
451 void RunTest_RepeatingTimerConcurrentResetAndTimerFired( | |
452 base::MessageLoop::Type message_loop_type) { | |
453 base::MessageLoop loop(message_loop_type); | |
454 | |
455 timers::AlarmTimer timer(true, true); | |
456 bool did_run = false; | |
457 | |
458 timer.Start( | |
459 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); | |
460 | |
461 // Sleep more that three times as long as the timer duration to ensure that | |
462 // multiple tasks get queued. | |
463 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(35)); | |
464 | |
465 // Now reset the timer. This is attempting to simulate a very busy message | |
466 // loop where multiple tasks get queued but the timer gets reset before any of | |
467 // them have a chance to run. | |
468 timer.Reset(); | |
469 | |
470 base::RunLoop().RunUntilIdle(); | |
471 EXPECT_FALSE(did_run); | |
472 | |
473 // If the previous check failed, running the message loop again will hang the | |
474 // test so we only do it if the callback has not run yet. | |
475 if (!did_run) { | |
476 base::RunLoop().Run(); | |
477 EXPECT_TRUE(did_run); | |
478 } | |
479 } | |
480 | |
481 TEST(AlarmTimerTest, OneShotTimerConcurrentResetAndTimerFired) { | 412 TEST(AlarmTimerTest, OneShotTimerConcurrentResetAndTimerFired) { |
482 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 413 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
483 RunTest_OneShotTimerConcurrentResetAndTimerFired(testing_message_loops[i]); | 414 base::MessageLoop loop(testing_message_loops[i]); |
| 415 |
| 416 timers::OneShotAlarmTimer timer; |
| 417 bool did_run = false; |
| 418 |
| 419 base::RunLoop run_loop; |
| 420 |
| 421 timer.SetTimerFiredCallbackForTest(run_loop.QuitClosure()); |
| 422 timer.Start(FROM_HERE, kTenMilliseconds, |
| 423 base::Bind(&TimerRanCallback, &did_run)); |
| 424 |
| 425 // Wait until the timer has fired and a task has been queue in the |
| 426 // MessageLoop. |
| 427 run_loop.Run(); |
| 428 |
| 429 // Now reset the timer. This is attempting to simulate the timer firing and |
| 430 // being reset at the same time. The previously queued task should be |
| 431 // removed. |
| 432 timer.Reset(); |
| 433 |
| 434 base::RunLoop().RunUntilIdle(); |
| 435 EXPECT_FALSE(did_run); |
| 436 |
| 437 // If the previous check failed, running the message loop again will hang |
| 438 // the test so we only do it if the callback has not run yet. |
| 439 if (!did_run) { |
| 440 base::RunLoop().Run(); |
| 441 EXPECT_TRUE(did_run); |
| 442 } |
484 } | 443 } |
485 } | 444 } |
486 | 445 |
487 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { | 446 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { |
488 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 447 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
489 RunTest_RepeatingTimerConcurrentResetAndTimerFired( | 448 base::MessageLoop loop(testing_message_loops[i]); |
490 testing_message_loops[i]); | 449 |
| 450 timers::RepeatingAlarmTimer timer; |
| 451 bool did_run = false; |
| 452 |
| 453 base::RunLoop run_loop; |
| 454 |
| 455 timer.SetTimerFiredCallbackForTest(run_loop.QuitClosure()); |
| 456 timer.Start(FROM_HERE, kTenMilliseconds, |
| 457 base::Bind(&TimerRanCallback, &did_run)); |
| 458 |
| 459 // Wait until the timer has fired and a task has been queue in the |
| 460 // MessageLoop. |
| 461 run_loop.Run(); |
| 462 |
| 463 // Now reset the timer. This is attempting to simulate the timer firing and |
| 464 // being reset at the same time. The previously queued task should be |
| 465 // removed. |
| 466 timer.Reset(); |
| 467 |
| 468 base::RunLoop().RunUntilIdle(); |
| 469 EXPECT_FALSE(did_run); |
| 470 |
| 471 // If the previous check failed, running the message loop again will hang |
| 472 // the test so we only do it if the callback has not run yet. |
| 473 if (!did_run) { |
| 474 base::RunLoop().Run(); |
| 475 EXPECT_TRUE(did_run); |
| 476 } |
491 } | 477 } |
492 } | 478 } |
493 | 479 |
494 } // namespace | 480 } // namespace |
495 | 481 |
496 } // namespace timers | 482 } // namespace timers |
OLD | NEW |