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

Side by Side Diff: components/timers/alarm_timer_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698