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_chromeos.h" | 12 #include "components/timers/alarm_timer.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::OneShotAlarmTimer()) {} | 42 timer_(new timers::AlarmTimer(false, false)) { |
| 43 } |
43 void Start() { | 44 void Start() { |
44 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, | 45 timer_->Start(FROM_HERE, |
45 base::Unretained(this))); | 46 delay_, |
| 47 base::Bind(&OneShotAlarmTimerTester::Run, |
| 48 base::Unretained(this))); |
46 } | 49 } |
47 | 50 |
48 private: | 51 private: |
49 void Run() { | 52 void Run() { |
50 *did_run_ = true; | 53 *did_run_ = true; |
51 | 54 |
52 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
53 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 56 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
54 } | 57 } |
55 | 58 |
56 bool* did_run_; | 59 bool* did_run_; |
57 const base::TimeDelta delay_; | 60 const base::TimeDelta delay_; |
58 scoped_ptr<timers::OneShotAlarmTimer> timer_; | 61 scoped_ptr<timers::AlarmTimer> timer_; |
59 | 62 |
60 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); | 63 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
61 }; | 64 }; |
62 | 65 |
63 class OneShotSelfDeletingAlarmTimerTester { | 66 class OneShotSelfDeletingAlarmTimerTester { |
64 public: | 67 public: |
65 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 68 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
66 : did_run_(did_run), | 69 : did_run_(did_run), |
67 delay_(delay), | 70 delay_(delay), |
68 timer_(new timers::OneShotAlarmTimer()) {} | 71 timer_(new timers::AlarmTimer(false, false)) { |
| 72 } |
69 void Start() { | 73 void Start() { |
70 timer_->Start(FROM_HERE, delay_, | 74 timer_->Start(FROM_HERE, |
| 75 delay_, |
71 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, | 76 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
72 base::Unretained(this))); | 77 base::Unretained(this))); |
73 } | 78 } |
74 | 79 |
75 private: | 80 private: |
76 void Run() { | 81 void Run() { |
77 *did_run_ = true; | 82 *did_run_ = true; |
78 timer_.reset(); | 83 timer_.reset(); |
79 | 84 |
80 base::MessageLoop::current()->PostTask( | 85 base::MessageLoop::current()->PostTask( |
81 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 86 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
82 } | 87 } |
83 | 88 |
84 bool* did_run_; | 89 bool* did_run_; |
85 const base::TimeDelta delay_; | 90 const base::TimeDelta delay_; |
86 scoped_ptr<timers::OneShotAlarmTimer> timer_; | 91 scoped_ptr<timers::AlarmTimer> timer_; |
87 | 92 |
88 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); | 93 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
89 }; | 94 }; |
90 | 95 |
91 class RepeatingAlarmTimerTester { | 96 class RepeatingAlarmTimerTester { |
92 public: | 97 public: |
93 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 98 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
94 : did_run_(did_run), | 99 : did_run_(did_run), |
95 delay_(delay), | 100 delay_(delay), |
96 counter_(10), | 101 counter_(10), |
97 timer_(new timers::RepeatingAlarmTimer()) {} | 102 timer_(new timers::AlarmTimer(true, true)) { |
| 103 } |
98 void Start() { | 104 void Start() { |
99 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, | 105 timer_->Start(FROM_HERE, |
100 base::Unretained(this))); | 106 delay_, |
| 107 base::Bind(&RepeatingAlarmTimerTester::Run, |
| 108 base::Unretained(this))); |
101 } | 109 } |
102 | 110 |
103 private: | 111 private: |
104 void Run() { | 112 void Run() { |
105 if (--counter_ == 0) { | 113 if (--counter_ == 0) { |
106 *did_run_ = true; | 114 *did_run_ = true; |
107 timer_->Stop(); | 115 timer_->Stop(); |
108 | 116 |
109 base::MessageLoop::current()->PostTask( | 117 base::MessageLoop::current()->PostTask( |
110 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 118 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
111 } | 119 } |
112 } | 120 } |
113 | 121 |
114 bool* did_run_; | 122 bool* did_run_; |
115 const base::TimeDelta delay_; | 123 const base::TimeDelta delay_; |
116 int counter_; | 124 int counter_; |
117 scoped_ptr<timers::RepeatingAlarmTimer> timer_; | 125 scoped_ptr<timers::AlarmTimer> timer_; |
118 | 126 |
119 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); | 127 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
120 }; | 128 }; |
121 | 129 |
| 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 |
122 } // namespace | 216 } // namespace |
123 | 217 |
124 //----------------------------------------------------------------------------- | 218 //----------------------------------------------------------------------------- |
125 // Each test is run against each type of MessageLoop. That way we are sure | 219 // Each test is run against each type of MessageLoop. That way we are sure |
126 // that timers work properly in all configurations. | 220 // that timers work properly in all configurations. |
127 | 221 |
128 TEST(AlarmTimerTest, OneShotAlarmTimer) { | 222 TEST(AlarmTimerTest, OneShotAlarmTimer) { |
129 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 223 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
130 base::MessageLoop loop(testing_message_loops[i]); | 224 RunTest_OneShotAlarmTimer(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); | |
139 } | 225 } |
140 } | 226 } |
141 | 227 |
142 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { | 228 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { |
143 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 229 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
144 base::MessageLoop loop(testing_message_loops[i]); | 230 RunTest_OneShotAlarmTimer_Cancel(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); | |
164 } | 231 } |
165 } | 232 } |
166 | 233 |
167 // If underlying timer does not handle this properly, we will crash or fail | 234 // If underlying timer does not handle this properly, we will crash or fail |
168 // in full page heap environment. | 235 // in full page heap environment. |
169 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { | 236 TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { |
170 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 237 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
171 base::MessageLoop loop(testing_message_loops[i]); | 238 RunTest_OneShotSelfDeletingAlarmTimer(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); | |
180 } | 239 } |
181 } | 240 } |
182 | 241 |
183 TEST(AlarmTimerTest, RepeatingAlarmTimer) { | 242 TEST(AlarmTimerTest, RepeatingAlarmTimer) { |
184 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 243 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
185 base::MessageLoop loop(testing_message_loops[i]); | 244 RunTest_RepeatingAlarmTimer(testing_message_loops[i], |
186 | 245 kTenMilliseconds); |
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); | |
194 } | 246 } |
195 } | 247 } |
196 | 248 |
197 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { | 249 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
198 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 250 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
199 base::MessageLoop loop(testing_message_loops[i]); | 251 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], |
200 | 252 kTenMilliseconds); |
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); | |
219 } | 253 } |
220 } | 254 } |
221 | 255 |
222 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { | 256 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { |
223 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 257 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
224 base::MessageLoop loop(testing_message_loops[i]); | 258 RunTest_RepeatingAlarmTimer(testing_message_loops[i], |
225 | 259 base::TimeDelta::FromMilliseconds(0)); |
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); | |
233 } | 260 } |
234 } | 261 } |
235 | 262 |
236 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { | 263 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { |
237 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 264 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
238 base::MessageLoop loop(testing_message_loops[i]); | 265 RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], |
239 | 266 base::TimeDelta::FromMilliseconds(0)); |
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); | |
258 } | 267 } |
259 } | 268 } |
260 | 269 |
261 TEST(AlarmTimerTest, MessageLoopShutdown) { | 270 TEST(AlarmTimerTest, MessageLoopShutdown) { |
262 // This test is designed to verify that shutdown of the | 271 // This test is designed to verify that shutdown of the |
263 // message loop does not cause crashes if there were pending | 272 // message loop does not cause crashes if there were pending |
264 // timers not yet fired. It may only trigger exceptions | 273 // timers not yet fired. It may only trigger exceptions |
265 // if debug heap checking is enabled. | 274 // if debug heap checking is enabled. |
266 bool did_run = false; | 275 bool did_run = false; |
267 { | 276 { |
268 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); | 277 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds); |
269 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); | 278 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds); |
270 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); | 279 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds); |
271 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); | 280 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds); |
272 { | 281 { |
273 base::MessageLoop loop; | 282 base::MessageLoop loop; |
274 a.Start(); | 283 a.Start(); |
275 b.Start(); | 284 b.Start(); |
276 } // MessageLoop destructs by falling out of scope. | 285 } // MessageLoop destructs by falling out of scope. |
277 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. | 286 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
278 | 287 |
279 EXPECT_FALSE(did_run); | 288 EXPECT_FALSE(did_run); |
280 } | 289 } |
281 | 290 |
282 TEST(AlarmTimerTest, NonRepeatIsRunning) { | 291 TEST(AlarmTimerTest, NonRepeatIsRunning) { |
283 { | 292 { |
284 base::MessageLoop loop; | 293 base::MessageLoop loop; |
285 timers::OneShotAlarmTimer timer; | 294 timers::AlarmTimer timer(false, false); |
286 EXPECT_FALSE(timer.IsRunning()); | 295 EXPECT_FALSE(timer.IsRunning()); |
287 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 296 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
288 base::Bind(&base::DoNothing)); | 297 base::Bind(&base::DoNothing)); |
289 EXPECT_TRUE(timer.IsRunning()); | 298 EXPECT_TRUE(timer.IsRunning()); |
290 timer.Stop(); | 299 timer.Stop(); |
291 EXPECT_FALSE(timer.IsRunning()); | 300 EXPECT_FALSE(timer.IsRunning()); |
292 EXPECT_TRUE(timer.user_task().is_null()); | 301 EXPECT_TRUE(timer.user_task().is_null()); |
293 } | 302 } |
294 | 303 |
295 { | 304 { |
296 timers::SimpleAlarmTimer timer; | 305 timers::AlarmTimer timer(true, false); |
297 base::MessageLoop loop; | 306 base::MessageLoop loop; |
298 EXPECT_FALSE(timer.IsRunning()); | 307 EXPECT_FALSE(timer.IsRunning()); |
299 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 308 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
300 base::Bind(&base::DoNothing)); | 309 base::Bind(&base::DoNothing)); |
301 EXPECT_TRUE(timer.IsRunning()); | 310 EXPECT_TRUE(timer.IsRunning()); |
302 timer.Stop(); | 311 timer.Stop(); |
303 EXPECT_FALSE(timer.IsRunning()); | 312 EXPECT_FALSE(timer.IsRunning()); |
304 ASSERT_FALSE(timer.user_task().is_null()); | 313 ASSERT_FALSE(timer.user_task().is_null()); |
305 timer.Reset(); | 314 timer.Reset(); |
306 EXPECT_TRUE(timer.IsRunning()); | 315 EXPECT_TRUE(timer.IsRunning()); |
307 } | 316 } |
308 } | 317 } |
309 | 318 |
310 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { | 319 TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { |
311 timers::OneShotAlarmTimer timer; | 320 timers::AlarmTimer timer(false, false); |
312 { | 321 { |
313 base::MessageLoop loop; | 322 base::MessageLoop loop; |
314 EXPECT_FALSE(timer.IsRunning()); | 323 EXPECT_FALSE(timer.IsRunning()); |
315 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), | 324 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
316 base::Bind(&base::DoNothing)); | 325 base::Bind(&base::DoNothing)); |
317 EXPECT_TRUE(timer.IsRunning()); | 326 EXPECT_TRUE(timer.IsRunning()); |
318 } | 327 } |
319 EXPECT_FALSE(timer.IsRunning()); | 328 EXPECT_FALSE(timer.IsRunning()); |
320 EXPECT_TRUE(timer.user_task().is_null()); | 329 EXPECT_TRUE(timer.user_task().is_null()); |
321 } | 330 } |
322 | 331 |
323 TEST(AlarmTimerTest, RetainRepeatIsRunning) { | 332 TEST(AlarmTimerTest, RetainRepeatIsRunning) { |
324 base::MessageLoop loop; | 333 base::MessageLoop loop; |
325 timers::RepeatingAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 334 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
326 base::Bind(&base::DoNothing)); | 335 base::Bind(&base::DoNothing), true); |
327 EXPECT_FALSE(timer.IsRunning()); | 336 EXPECT_FALSE(timer.IsRunning()); |
328 timer.Reset(); | 337 timer.Reset(); |
329 EXPECT_TRUE(timer.IsRunning()); | 338 EXPECT_TRUE(timer.IsRunning()); |
330 timer.Stop(); | 339 timer.Stop(); |
331 EXPECT_FALSE(timer.IsRunning()); | 340 EXPECT_FALSE(timer.IsRunning()); |
332 timer.Reset(); | 341 timer.Reset(); |
333 EXPECT_TRUE(timer.IsRunning()); | 342 EXPECT_TRUE(timer.IsRunning()); |
334 } | 343 } |
335 | 344 |
336 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { | 345 TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { |
337 base::MessageLoop loop; | 346 base::MessageLoop loop; |
338 timers::SimpleAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), | 347 timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
339 base::Bind(&base::DoNothing)); | 348 base::Bind(&base::DoNothing), false); |
340 EXPECT_FALSE(timer.IsRunning()); | 349 EXPECT_FALSE(timer.IsRunning()); |
341 timer.Reset(); | 350 timer.Reset(); |
342 EXPECT_TRUE(timer.IsRunning()); | 351 EXPECT_TRUE(timer.IsRunning()); |
343 timer.Stop(); | 352 timer.Stop(); |
344 EXPECT_FALSE(timer.IsRunning()); | 353 EXPECT_FALSE(timer.IsRunning()); |
345 timer.Reset(); | 354 timer.Reset(); |
346 EXPECT_TRUE(timer.IsRunning()); | 355 EXPECT_TRUE(timer.IsRunning()); |
347 } | 356 } |
348 | 357 |
349 namespace { | 358 namespace { |
(...skipping 15 matching lines...) Expand all Loading... |
365 void SetCallbackHappened2() { | 374 void SetCallbackHappened2() { |
366 g_callback_happened2 = true; | 375 g_callback_happened2 = true; |
367 base::MessageLoop::current()->PostTask( | 376 base::MessageLoop::current()->PostTask( |
368 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 377 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
369 } | 378 } |
370 | 379 |
371 TEST(AlarmTimerTest, ContinuationStopStart) { | 380 TEST(AlarmTimerTest, ContinuationStopStart) { |
372 { | 381 { |
373 ClearAllCallbackHappened(); | 382 ClearAllCallbackHappened(); |
374 base::MessageLoop loop; | 383 base::MessageLoop loop; |
375 timers::OneShotAlarmTimer timer; | 384 timers::AlarmTimer timer(false, false); |
376 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 385 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
377 base::Bind(&SetCallbackHappened1)); | 386 base::Bind(&SetCallbackHappened1)); |
378 timer.Stop(); | 387 timer.Stop(); |
379 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), | 388 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40), |
380 base::Bind(&SetCallbackHappened2)); | 389 base::Bind(&SetCallbackHappened2)); |
381 base::RunLoop().Run(); | 390 base::RunLoop().Run(); |
382 EXPECT_FALSE(g_callback_happened1); | 391 EXPECT_FALSE(g_callback_happened1); |
383 EXPECT_TRUE(g_callback_happened2); | 392 EXPECT_TRUE(g_callback_happened2); |
384 } | 393 } |
385 } | 394 } |
386 | 395 |
387 TEST(AlarmTimerTest, ContinuationReset) { | 396 TEST(AlarmTimerTest, ContinuationReset) { |
388 { | 397 { |
389 ClearAllCallbackHappened(); | 398 ClearAllCallbackHappened(); |
390 base::MessageLoop loop; | 399 base::MessageLoop loop; |
391 timers::OneShotAlarmTimer timer; | 400 timers::AlarmTimer timer(false, false); |
392 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 401 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
393 base::Bind(&SetCallbackHappened1)); | 402 base::Bind(&SetCallbackHappened1)); |
394 timer.Reset(); | 403 timer.Reset(); |
395 // Since Reset happened before task ran, the user_task must not be cleared: | 404 // Since Reset happened before task ran, the user_task must not be cleared: |
396 ASSERT_FALSE(timer.user_task().is_null()); | 405 ASSERT_FALSE(timer.user_task().is_null()); |
397 base::RunLoop().Run(); | 406 base::RunLoop().Run(); |
398 EXPECT_TRUE(g_callback_happened1); | 407 EXPECT_TRUE(g_callback_happened1); |
399 } | 408 } |
400 } | 409 } |
401 | 410 |
402 } // namespace | 411 } // namespace |
403 | 412 |
| 413 |
404 namespace { | 414 namespace { |
405 void TimerRanCallback(bool* did_run) { | 415 void TimerRanCallback(bool* did_run) { |
406 *did_run = true; | 416 *did_run = true; |
407 | 417 |
408 base::MessageLoop::current()->PostTask( | 418 base::MessageLoop::current()->PostTask( |
409 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 419 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
410 } | 420 } |
411 | 421 |
| 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 |
412 TEST(AlarmTimerTest, OneShotTimerConcurrentResetAndTimerFired) { | 481 TEST(AlarmTimerTest, OneShotTimerConcurrentResetAndTimerFired) { |
413 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 482 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
414 base::MessageLoop loop(testing_message_loops[i]); | 483 RunTest_OneShotTimerConcurrentResetAndTimerFired(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 } | |
443 } | 484 } |
444 } | 485 } |
445 | 486 |
446 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { | 487 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { |
447 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 488 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
448 base::MessageLoop loop(testing_message_loops[i]); | 489 RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
449 | 490 testing_message_loops[i]); |
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 } | |
477 } | 491 } |
478 } | 492 } |
479 | 493 |
480 } // namespace | 494 } // namespace |
481 | 495 |
482 } // namespace timers | 496 } // namespace timers |
OLD | NEW |