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 <sys/timerfd.h> | 5 #include <sys/timerfd.h> |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/location.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 18 #include "base/time/time.h" |
16 #include "components/timers/alarm_timer_chromeos.h" | 19 #include "components/timers/alarm_timer_chromeos.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
18 | 21 |
19 // Most of these tests have been lifted right out of timer_unittest.cc with only | 22 // Most of these tests have been lifted right out of timer_unittest.cc with only |
20 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with | 23 // cosmetic changes (like replacing calls to MessageLoop::current()->Run() with |
21 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the | 24 // a RunLoop). We want the AlarmTimer to be a drop-in replacement for the |
22 // regular Timer so it should pass the same tests as the Timer class. | 25 // regular Timer so it should pass the same tests as the Timer class. |
23 // | 26 // |
24 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test | 27 // The only new tests are the .*ConcurrentResetAndTimerFired tests, which test |
(...skipping 21 matching lines...) Expand all Loading... |
46 timer_(new timers::OneShotAlarmTimer()) {} | 49 timer_(new timers::OneShotAlarmTimer()) {} |
47 void Start() { | 50 void Start() { |
48 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, | 51 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, |
49 base::Unretained(this))); | 52 base::Unretained(this))); |
50 } | 53 } |
51 | 54 |
52 private: | 55 private: |
53 void Run() { | 56 void Run() { |
54 *did_run_ = true; | 57 *did_run_ = true; |
55 | 58 |
56 base::MessageLoop::current()->PostTask( | 59 base::ThreadTaskRunnerHandle::Get()->PostTask( |
57 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 60 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
58 } | 61 } |
59 | 62 |
60 bool* did_run_; | 63 bool* did_run_; |
61 const base::TimeDelta delay_; | 64 const base::TimeDelta delay_; |
62 std::unique_ptr<timers::OneShotAlarmTimer> timer_; | 65 std::unique_ptr<timers::OneShotAlarmTimer> timer_; |
63 | 66 |
64 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); | 67 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
65 }; | 68 }; |
66 | 69 |
67 class OneShotSelfDeletingAlarmTimerTester { | 70 class OneShotSelfDeletingAlarmTimerTester { |
68 public: | 71 public: |
69 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 72 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
70 : did_run_(did_run), | 73 : did_run_(did_run), |
71 delay_(delay), | 74 delay_(delay), |
72 timer_(new timers::OneShotAlarmTimer()) {} | 75 timer_(new timers::OneShotAlarmTimer()) {} |
73 void Start() { | 76 void Start() { |
74 timer_->Start(FROM_HERE, delay_, | 77 timer_->Start(FROM_HERE, delay_, |
75 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, | 78 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
76 base::Unretained(this))); | 79 base::Unretained(this))); |
77 } | 80 } |
78 | 81 |
79 private: | 82 private: |
80 void Run() { | 83 void Run() { |
81 *did_run_ = true; | 84 *did_run_ = true; |
82 timer_.reset(); | 85 timer_.reset(); |
83 | 86 |
84 base::MessageLoop::current()->PostTask( | 87 base::ThreadTaskRunnerHandle::Get()->PostTask( |
85 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 88 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
86 } | 89 } |
87 | 90 |
88 bool* did_run_; | 91 bool* did_run_; |
89 const base::TimeDelta delay_; | 92 const base::TimeDelta delay_; |
90 std::unique_ptr<timers::OneShotAlarmTimer> timer_; | 93 std::unique_ptr<timers::OneShotAlarmTimer> timer_; |
91 | 94 |
92 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); | 95 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
93 }; | 96 }; |
94 | 97 |
95 class RepeatingAlarmTimerTester { | 98 class RepeatingAlarmTimerTester { |
96 public: | 99 public: |
97 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) | 100 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
98 : did_run_(did_run), | 101 : did_run_(did_run), |
99 delay_(delay), | 102 delay_(delay), |
100 counter_(10), | 103 counter_(10), |
101 timer_(new timers::RepeatingAlarmTimer()) {} | 104 timer_(new timers::RepeatingAlarmTimer()) {} |
102 void Start() { | 105 void Start() { |
103 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, | 106 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, |
104 base::Unretained(this))); | 107 base::Unretained(this))); |
105 } | 108 } |
106 | 109 |
107 private: | 110 private: |
108 void Run() { | 111 void Run() { |
109 if (--counter_ == 0) { | 112 if (--counter_ == 0) { |
110 *did_run_ = true; | 113 *did_run_ = true; |
111 timer_->Stop(); | 114 timer_->Stop(); |
112 | 115 |
113 base::MessageLoop::current()->PostTask( | 116 base::ThreadTaskRunnerHandle::Get()->PostTask( |
114 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 117 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
115 } | 118 } |
116 } | 119 } |
117 | 120 |
118 bool* did_run_; | 121 bool* did_run_; |
119 const base::TimeDelta delay_; | 122 const base::TimeDelta delay_; |
120 int counter_; | 123 int counter_; |
121 std::unique_ptr<timers::RepeatingAlarmTimer> timer_; | 124 std::unique_ptr<timers::RepeatingAlarmTimer> timer_; |
122 | 125 |
123 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); | 126 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
(...skipping 21 matching lines...) Expand all Loading... |
145 | 148 |
146 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { | 149 TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { |
147 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 150 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
148 base::MessageLoop loop(testing_message_loops[i]); | 151 base::MessageLoop loop(testing_message_loops[i]); |
149 | 152 |
150 bool did_run_a = false; | 153 bool did_run_a = false; |
151 OneShotAlarmTimerTester* a = | 154 OneShotAlarmTimerTester* a = |
152 new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); | 155 new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); |
153 | 156 |
154 // This should run before the timer expires. | 157 // This should run before the timer expires. |
155 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 158 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
156 | 159 |
157 // Now start the timer. | 160 // Now start the timer. |
158 a->Start(); | 161 a->Start(); |
159 | 162 |
160 bool did_run_b = false; | 163 bool did_run_b = false; |
161 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); | 164 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
162 b.Start(); | 165 b.Start(); |
163 | 166 |
164 base::RunLoop().Run(); | 167 base::RunLoop().Run(); |
165 | 168 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 203 |
201 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { | 204 TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
202 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 205 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
203 base::MessageLoop loop(testing_message_loops[i]); | 206 base::MessageLoop loop(testing_message_loops[i]); |
204 | 207 |
205 bool did_run_a = false; | 208 bool did_run_a = false; |
206 RepeatingAlarmTimerTester* a = | 209 RepeatingAlarmTimerTester* a = |
207 new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds); | 210 new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds); |
208 | 211 |
209 // This should run before the timer expires. | 212 // This should run before the timer expires. |
210 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 213 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
211 | 214 |
212 // Now start the timer. | 215 // Now start the timer. |
213 a->Start(); | 216 a->Start(); |
214 | 217 |
215 bool did_run_b = false; | 218 bool did_run_b = false; |
216 RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds); | 219 RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
217 b.Start(); | 220 b.Start(); |
218 | 221 |
219 base::RunLoop().Run(); | 222 base::RunLoop().Run(); |
220 | 223 |
(...skipping 18 matching lines...) Expand all Loading... |
239 | 242 |
240 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { | 243 TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { |
241 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 244 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
242 base::MessageLoop loop(testing_message_loops[i]); | 245 base::MessageLoop loop(testing_message_loops[i]); |
243 | 246 |
244 bool did_run_a = false; | 247 bool did_run_a = false; |
245 RepeatingAlarmTimerTester* a = | 248 RepeatingAlarmTimerTester* a = |
246 new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta()); | 249 new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta()); |
247 | 250 |
248 // This should run before the timer expires. | 251 // This should run before the timer expires. |
249 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 252 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
250 | 253 |
251 // Now start the timer. | 254 // Now start the timer. |
252 a->Start(); | 255 a->Start(); |
253 | 256 |
254 bool did_run_b = false; | 257 bool did_run_b = false; |
255 RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta()); | 258 RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta()); |
256 b.Start(); | 259 b.Start(); |
257 | 260 |
258 base::RunLoop().Run(); | 261 base::RunLoop().Run(); |
259 | 262 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 bool g_callback_happened1 = false; | 358 bool g_callback_happened1 = false; |
356 bool g_callback_happened2 = false; | 359 bool g_callback_happened2 = false; |
357 | 360 |
358 void ClearAllCallbackHappened() { | 361 void ClearAllCallbackHappened() { |
359 g_callback_happened1 = false; | 362 g_callback_happened1 = false; |
360 g_callback_happened2 = false; | 363 g_callback_happened2 = false; |
361 } | 364 } |
362 | 365 |
363 void SetCallbackHappened1() { | 366 void SetCallbackHappened1() { |
364 g_callback_happened1 = true; | 367 g_callback_happened1 = true; |
365 base::MessageLoop::current()->PostTask( | 368 base::ThreadTaskRunnerHandle::Get()->PostTask( |
366 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 369 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
367 } | 370 } |
368 | 371 |
369 void SetCallbackHappened2() { | 372 void SetCallbackHappened2() { |
370 g_callback_happened2 = true; | 373 g_callback_happened2 = true; |
371 base::MessageLoop::current()->PostTask( | 374 base::ThreadTaskRunnerHandle::Get()->PostTask( |
372 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 375 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
373 } | 376 } |
374 | 377 |
375 TEST(AlarmTimerTest, ContinuationStopStart) { | 378 TEST(AlarmTimerTest, ContinuationStopStart) { |
376 { | 379 { |
377 ClearAllCallbackHappened(); | 380 ClearAllCallbackHappened(); |
378 base::MessageLoop loop; | 381 base::MessageLoop loop; |
379 timers::OneShotAlarmTimer timer; | 382 timers::OneShotAlarmTimer timer; |
380 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), | 383 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
381 base::Bind(&SetCallbackHappened1)); | 384 base::Bind(&SetCallbackHappened1)); |
(...skipping 20 matching lines...) Expand all Loading... |
402 EXPECT_TRUE(g_callback_happened1); | 405 EXPECT_TRUE(g_callback_happened1); |
403 } | 406 } |
404 } | 407 } |
405 | 408 |
406 } // namespace | 409 } // namespace |
407 | 410 |
408 namespace { | 411 namespace { |
409 void TimerRanCallback(bool* did_run) { | 412 void TimerRanCallback(bool* did_run) { |
410 *did_run = true; | 413 *did_run = true; |
411 | 414 |
412 base::MessageLoop::current()->PostTask( | 415 base::ThreadTaskRunnerHandle::Get()->PostTask( |
413 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 416 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
414 } | 417 } |
415 | 418 |
416 bool IsAlarmTimerSupported() { | 419 bool IsAlarmTimerSupported() { |
417 int fd = timerfd_create(CLOCK_REALTIME_ALARM, 0); | 420 int fd = timerfd_create(CLOCK_REALTIME_ALARM, 0); |
418 | 421 |
419 if (fd == -1) { | 422 if (fd == -1) { |
420 LOG(WARNING) << "CLOCK_REALTIME_ALARM is not supported on this system. " | 423 LOG(WARNING) << "CLOCK_REALTIME_ALARM is not supported on this system. " |
421 << "Skipping test. Upgrade to at least linux version 3.11 to " | 424 << "Skipping test. Upgrade to at least linux version 3.11 to " |
422 << "support this timer."; | 425 << "support this timer."; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 if (!did_run) { | 508 if (!did_run) { |
506 base::RunLoop().Run(); | 509 base::RunLoop().Run(); |
507 EXPECT_TRUE(did_run); | 510 EXPECT_TRUE(did_run); |
508 } | 511 } |
509 } | 512 } |
510 } | 513 } |
511 | 514 |
512 } // namespace | 515 } // namespace |
513 | 516 |
514 } // namespace timers | 517 } // namespace timers |
OLD | NEW |