Chromium Code Reviews| 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.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. |
|
gromer
2014/12/05 20:13:41
GUnit has mechanisms for type-parameterized tests,
Chirantan Ekbote
2014/12/31 00:58:29
Yes, there is a bug open for me to fix this: crbug
| |
| 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 | |
| 24 namespace { | 25 namespace { |
| 25 // The message loops on which each timer should be tested. | 26 // The message loops on which each timer should be tested. |
| 26 const base::MessageLoop::Type testing_message_loops[] = { | 27 const base::MessageLoop::Type testing_message_loops[] = { |
| 27 base::MessageLoop::TYPE_DEFAULT, | 28 base::MessageLoop::TYPE_DEFAULT, |
| 28 base::MessageLoop::TYPE_IO, | 29 base::MessageLoop::TYPE_IO, |
| 29 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | 30 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 30 base::MessageLoop::TYPE_UI, | 31 base::MessageLoop::TYPE_UI, |
| 31 #endif | 32 #endif |
| 32 }; | 33 }; |
| 33 | 34 |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 | 413 |
| 413 | 414 |
| 414 namespace { | 415 namespace { |
| 415 void TimerRanCallback(bool* did_run) { | 416 void TimerRanCallback(bool* did_run) { |
| 416 *did_run = true; | 417 *did_run = true; |
| 417 | 418 |
| 418 base::MessageLoop::current()->PostTask( | 419 base::MessageLoop::current()->PostTask( |
| 419 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 420 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 420 } | 421 } |
| 421 | 422 |
| 422 void RunTest_OneShotTimerConcurrentResetAndTimerFired( | 423 void RunTest_OneShotTimerConcurrentResetAndTimerFired( |
|
gromer
2014/12/05 20:13:41
Test code tends to work better in the top-level te
Chirantan Ekbote
2014/12/31 00:58:29
The issue is that the same test needs to run on mu
gromer
2015/02/10 23:47:10
I don't understand- this function only has one cal
Chirantan Ekbote
2015/02/12 01:49:58
That caller calls it multiple times with a differe
gromer
2015/02/17 22:02:08
It's called multiple times, but inside a loop; the
Chirantan Ekbote
2015/02/25 03:32:56
I see. Sorry, I misunderstood what you were sayin
| |
| 423 base::MessageLoop::Type message_loop_type) { | 424 base::MessageLoop::Type message_loop_type) { |
| 424 base::MessageLoop loop(message_loop_type); | 425 base::MessageLoop loop(message_loop_type); |
| 425 | 426 |
| 426 timers::AlarmTimer timer(false, false); | 427 timers::AlarmTimer timer(false, false); |
| 427 bool did_run = false; | 428 bool did_run = false; |
| 428 | 429 |
| 429 timer.Start( | 430 timer.Start( |
| 430 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); | 431 FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); |
| 431 | 432 |
| 432 // Sleep twice as long as the timer to ensure that the timer task gets queued. | 433 // Sleep twice as long as the timer to ensure that the timer task gets queued. |
| 433 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 434 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 434 | 435 |
| 435 // Now reset the timer. This is attempting to simulate the timer firing and | 436 // 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 // being reset at the same time. The previously queued task should be |
|
gromer
2014/12/05 20:13:41
Not quite at the same time, right? The test code s
Chirantan Ekbote
2014/12/31 00:58:29
When the timer fires, it queues a task in the Mess
| |
| 437 // removed. | 438 // removed. |
| 438 timer.Reset(); | 439 timer.Reset(); |
| 439 | 440 |
| 440 base::RunLoop().RunUntilIdle(); | 441 base::RunLoop().RunUntilIdle(); |
| 441 EXPECT_FALSE(did_run); | 442 EXPECT_FALSE(did_run); |
| 442 | 443 |
| 443 // If the previous check failed, running the message loop again will hang the | 444 // 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 // test so we only do it if the callback has not run yet. |
| 445 if (!did_run) { | 446 if (!did_run) { |
| 446 base::RunLoop().Run(); | 447 base::RunLoop().Run(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 487 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { | 488 TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { |
| 488 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 489 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 489 RunTest_RepeatingTimerConcurrentResetAndTimerFired( | 490 RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
| 490 testing_message_loops[i]); | 491 testing_message_loops[i]); |
| 491 } | 492 } |
| 492 } | 493 } |
| 493 | 494 |
| 494 } // namespace | 495 } // namespace |
| 495 | 496 |
| 496 } // namespace timers | 497 } // namespace timers |
| OLD | NEW |