Index: components/timers/alarm_timer_unittest.cc |
diff --git a/components/timers/alarm_timer_unittest.cc b/components/timers/alarm_timer_unittest.cc |
index 3e11e868d611c4f217e5549c23d8ddfbd4c16461..efa88d4229e813c28e5b991b915cc4492836a99a 100644 |
--- a/components/timers/alarm_timer_unittest.cc |
+++ b/components/timers/alarm_timer_unittest.cc |
@@ -9,7 +9,7 @@ |
#include "base/run_loop.h" |
#include "base/threading/platform_thread.h" |
#include "base/time/time.h" |
-#include "components/timers/alarm_timer_chromeos.h" |
+#include "components/timers/alarm_timer.h" |
#include "testing/gtest/include/gtest/gtest.h" |
// Most of these tests have been lifted right out of timer_unittest.cc with only |
@@ -24,10 +24,10 @@ |
namespace { |
// The message loops on which each timer should be tested. |
const base::MessageLoop::Type testing_message_loops[] = { |
- base::MessageLoop::TYPE_DEFAULT, |
- base::MessageLoop::TYPE_IO, |
+ base::MessageLoop::TYPE_DEFAULT, |
+ base::MessageLoop::TYPE_IO, |
#if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
- base::MessageLoop::TYPE_UI, |
+ base::MessageLoop::TYPE_UI, |
#endif |
}; |
@@ -39,10 +39,13 @@ |
OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
: did_run_(did_run), |
delay_(delay), |
- timer_(new timers::OneShotAlarmTimer()) {} |
+ timer_(new timers::AlarmTimer(false, false)) { |
+ } |
void Start() { |
- timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run, |
- base::Unretained(this))); |
+ timer_->Start(FROM_HERE, |
+ delay_, |
+ base::Bind(&OneShotAlarmTimerTester::Run, |
+ base::Unretained(this))); |
} |
private: |
@@ -55,7 +58,7 @@ |
bool* did_run_; |
const base::TimeDelta delay_; |
- scoped_ptr<timers::OneShotAlarmTimer> timer_; |
+ scoped_ptr<timers::AlarmTimer> timer_; |
DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester); |
}; |
@@ -65,9 +68,11 @@ |
OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay) |
: did_run_(did_run), |
delay_(delay), |
- timer_(new timers::OneShotAlarmTimer()) {} |
+ timer_(new timers::AlarmTimer(false, false)) { |
+ } |
void Start() { |
- timer_->Start(FROM_HERE, delay_, |
+ timer_->Start(FROM_HERE, |
+ delay_, |
base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run, |
base::Unretained(this))); |
} |
@@ -83,7 +88,7 @@ |
bool* did_run_; |
const base::TimeDelta delay_; |
- scoped_ptr<timers::OneShotAlarmTimer> timer_; |
+ scoped_ptr<timers::AlarmTimer> timer_; |
DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester); |
}; |
@@ -94,10 +99,13 @@ |
: did_run_(did_run), |
delay_(delay), |
counter_(10), |
- timer_(new timers::RepeatingAlarmTimer()) {} |
+ timer_(new timers::AlarmTimer(true, true)) { |
+ } |
void Start() { |
- timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run, |
- base::Unretained(this))); |
+ timer_->Start(FROM_HERE, |
+ delay_, |
+ base::Bind(&RepeatingAlarmTimerTester::Run, |
+ base::Unretained(this))); |
} |
private: |
@@ -114,10 +122,96 @@ |
bool* did_run_; |
const base::TimeDelta delay_; |
int counter_; |
- scoped_ptr<timers::RepeatingAlarmTimer> timer_; |
+ scoped_ptr<timers::AlarmTimer> timer_; |
DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester); |
}; |
+ |
+void RunTest_OneShotAlarmTimer(base::MessageLoop::Type message_loop_type) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ bool did_run = false; |
+ OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); |
+ f.Start(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(did_run); |
+} |
+ |
+void RunTest_OneShotAlarmTimer_Cancel( |
+ base::MessageLoop::Type message_loop_type) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ bool did_run_a = false; |
+ OneShotAlarmTimerTester* a = new OneShotAlarmTimerTester(&did_run_a, |
+ kTenMilliseconds); |
+ |
+ // This should run before the timer expires. |
+ base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
+ |
+ // Now start the timer. |
+ a->Start(); |
+ |
+ bool did_run_b = false; |
+ OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
+ b.Start(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_FALSE(did_run_a); |
+ EXPECT_TRUE(did_run_b); |
+} |
+ |
+void RunTest_OneShotSelfDeletingAlarmTimer( |
+ base::MessageLoop::Type message_loop_type) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ bool did_run = false; |
+ OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds); |
+ f.Start(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(did_run); |
+} |
+ |
+void RunTest_RepeatingAlarmTimer(base::MessageLoop::Type message_loop_type, |
+ const base::TimeDelta& delay) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ bool did_run = false; |
+ RepeatingAlarmTimerTester f(&did_run, delay); |
+ f.Start(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(did_run); |
+} |
+ |
+void RunTest_RepeatingAlarmTimer_Cancel( |
+ base::MessageLoop::Type message_loop_type, const base::TimeDelta& delay) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ bool did_run_a = false; |
+ RepeatingAlarmTimerTester* a = new RepeatingAlarmTimerTester(&did_run_a, |
+ delay); |
+ |
+ // This should run before the timer expires. |
+ base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
+ |
+ // Now start the timer. |
+ a->Start(); |
+ |
+ bool did_run_b = false; |
+ RepeatingAlarmTimerTester b(&did_run_b, delay); |
+ b.Start(); |
+ |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_FALSE(did_run_a); |
+ EXPECT_TRUE(did_run_b); |
+} |
} // namespace |
@@ -127,40 +221,13 @@ |
TEST(AlarmTimerTest, OneShotAlarmTimer) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run = false; |
- OneShotAlarmTimerTester f(&did_run, kTenMilliseconds); |
- f.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_TRUE(did_run); |
+ RunTest_OneShotAlarmTimer(testing_message_loops[i]); |
} |
} |
TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run_a = false; |
- OneShotAlarmTimerTester* a = |
- new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds); |
- |
- // This should run before the timer expires. |
- base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
- |
- // Now start the timer. |
- a->Start(); |
- |
- bool did_run_b = false; |
- OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
- b.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_FALSE(did_run_a); |
- EXPECT_TRUE(did_run_b); |
+ RunTest_OneShotAlarmTimer_Cancel(testing_message_loops[i]); |
} |
} |
@@ -168,93 +235,35 @@ |
// in full page heap environment. |
TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run = false; |
- OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds); |
- f.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_TRUE(did_run); |
+ RunTest_OneShotSelfDeletingAlarmTimer(testing_message_loops[i]); |
} |
} |
TEST(AlarmTimerTest, RepeatingAlarmTimer) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run = false; |
- RepeatingAlarmTimerTester f(&did_run, kTenMilliseconds); |
- f.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_TRUE(did_run); |
+ RunTest_RepeatingAlarmTimer(testing_message_loops[i], |
+ kTenMilliseconds); |
} |
} |
TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run_a = false; |
- RepeatingAlarmTimerTester* a = |
- new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds); |
- |
- // This should run before the timer expires. |
- base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
- |
- // Now start the timer. |
- a->Start(); |
- |
- bool did_run_b = false; |
- RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds); |
- b.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_FALSE(did_run_a); |
- EXPECT_TRUE(did_run_b); |
+ RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], |
+ kTenMilliseconds); |
} |
} |
TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run = false; |
- RepeatingAlarmTimerTester f(&did_run, base::TimeDelta()); |
- f.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_TRUE(did_run); |
+ RunTest_RepeatingAlarmTimer(testing_message_loops[i], |
+ base::TimeDelta::FromMilliseconds(0)); |
} |
} |
TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- bool did_run_a = false; |
- RepeatingAlarmTimerTester* a = |
- new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta()); |
- |
- // This should run before the timer expires. |
- base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
- |
- // Now start the timer. |
- a->Start(); |
- |
- bool did_run_b = false; |
- RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta()); |
- b.Start(); |
- |
- base::RunLoop().Run(); |
- |
- EXPECT_FALSE(did_run_a); |
- EXPECT_TRUE(did_run_b); |
+ RunTest_RepeatingAlarmTimer_Cancel(testing_message_loops[i], |
+ base::TimeDelta::FromMilliseconds(0)); |
} |
} |
@@ -274,7 +283,7 @@ |
a.Start(); |
b.Start(); |
} // MessageLoop destructs by falling out of scope. |
- } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
+ } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
EXPECT_FALSE(did_run); |
} |
@@ -282,7 +291,7 @@ |
TEST(AlarmTimerTest, NonRepeatIsRunning) { |
{ |
base::MessageLoop loop; |
- timers::OneShotAlarmTimer timer; |
+ timers::AlarmTimer timer(false, false); |
EXPECT_FALSE(timer.IsRunning()); |
timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
base::Bind(&base::DoNothing)); |
@@ -293,7 +302,7 @@ |
} |
{ |
- timers::SimpleAlarmTimer timer; |
+ timers::AlarmTimer timer(true, false); |
base::MessageLoop loop; |
EXPECT_FALSE(timer.IsRunning()); |
timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), |
@@ -308,7 +317,7 @@ |
} |
TEST(AlarmTimerTest, NonRepeatMessageLoopDeath) { |
- timers::OneShotAlarmTimer timer; |
+ timers::AlarmTimer timer(false, false); |
{ |
base::MessageLoop loop; |
EXPECT_FALSE(timer.IsRunning()); |
@@ -322,8 +331,8 @@ |
TEST(AlarmTimerTest, RetainRepeatIsRunning) { |
base::MessageLoop loop; |
- timers::RepeatingAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
- base::Bind(&base::DoNothing)); |
+ timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
+ base::Bind(&base::DoNothing), true); |
EXPECT_FALSE(timer.IsRunning()); |
timer.Reset(); |
EXPECT_TRUE(timer.IsRunning()); |
@@ -335,8 +344,8 @@ |
TEST(AlarmTimerTest, RetainNonRepeatIsRunning) { |
base::MessageLoop loop; |
- timers::SimpleAlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
- base::Bind(&base::DoNothing)); |
+ timers::AlarmTimer timer(FROM_HERE, base::TimeDelta::FromDays(1), |
+ base::Bind(&base::DoNothing), false); |
EXPECT_FALSE(timer.IsRunning()); |
timer.Reset(); |
EXPECT_TRUE(timer.IsRunning()); |
@@ -372,7 +381,7 @@ |
{ |
ClearAllCallbackHappened(); |
base::MessageLoop loop; |
- timers::OneShotAlarmTimer timer; |
+ timers::AlarmTimer timer(false, false); |
timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
base::Bind(&SetCallbackHappened1)); |
timer.Stop(); |
@@ -388,7 +397,7 @@ |
{ |
ClearAllCallbackHappened(); |
base::MessageLoop loop; |
- timers::OneShotAlarmTimer timer; |
+ timers::AlarmTimer timer(false, false); |
timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), |
base::Bind(&SetCallbackHappened1)); |
timer.Reset(); |
@@ -401,6 +410,7 @@ |
} // namespace |
+ |
namespace { |
void TimerRanCallback(bool* did_run) { |
*did_run = true; |
@@ -409,71 +419,75 @@ |
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
} |
+void RunTest_OneShotTimerConcurrentResetAndTimerFired( |
+ base::MessageLoop::Type message_loop_type) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ timers::AlarmTimer timer(false, false); |
+ bool did_run = false; |
+ |
+ timer.Start( |
+ FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); |
+ |
+ // Sleep twice as long as the timer to ensure that the timer task gets queued. |
+ base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
+ |
+ // Now reset the timer. This is attempting to simulate the timer firing and |
+ // being reset at the same time. The previously queued task should be |
+ // removed. |
+ timer.Reset(); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_FALSE(did_run); |
+ |
+ // If the previous check failed, running the message loop again will hang the |
+ // test so we only do it if the callback has not run yet. |
+ if (!did_run) { |
+ base::RunLoop().Run(); |
+ EXPECT_TRUE(did_run); |
+ } |
+} |
+ |
+void RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
+ base::MessageLoop::Type message_loop_type) { |
+ base::MessageLoop loop(message_loop_type); |
+ |
+ timers::AlarmTimer timer(true, true); |
+ bool did_run = false; |
+ |
+ timer.Start( |
+ FROM_HERE, kTenMilliseconds, base::Bind(&TimerRanCallback, &did_run)); |
+ |
+ // Sleep more that three times as long as the timer duration to ensure that |
+ // multiple tasks get queued. |
+ base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(35)); |
+ |
+ // Now reset the timer. This is attempting to simulate a very busy message |
+ // loop where multiple tasks get queued but the timer gets reset before any of |
+ // them have a chance to run. |
+ timer.Reset(); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_FALSE(did_run); |
+ |
+ // If the previous check failed, running the message loop again will hang the |
+ // test so we only do it if the callback has not run yet. |
+ if (!did_run) { |
+ base::RunLoop().Run(); |
+ EXPECT_TRUE(did_run); |
+ } |
+} |
+ |
TEST(AlarmTimerTest, OneShotTimerConcurrentResetAndTimerFired) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- timers::OneShotAlarmTimer timer; |
- bool did_run = false; |
- |
- base::RunLoop run_loop; |
- |
- timer.SetTimerFiredCallbackForTest(run_loop.QuitClosure()); |
- timer.Start(FROM_HERE, kTenMilliseconds, |
- base::Bind(&TimerRanCallback, &did_run)); |
- |
- // Wait until the timer has fired and a task has been queue in the |
- // MessageLoop. |
- run_loop.Run(); |
- |
- // Now reset the timer. This is attempting to simulate the timer firing and |
- // being reset at the same time. The previously queued task should be |
- // removed. |
- timer.Reset(); |
- |
- base::RunLoop().RunUntilIdle(); |
- EXPECT_FALSE(did_run); |
- |
- // If the previous check failed, running the message loop again will hang |
- // the test so we only do it if the callback has not run yet. |
- if (!did_run) { |
- base::RunLoop().Run(); |
- EXPECT_TRUE(did_run); |
- } |
+ RunTest_OneShotTimerConcurrentResetAndTimerFired(testing_message_loops[i]); |
} |
} |
TEST(AlarmTimerTest, RepeatingTimerConcurrentResetAndTimerFired) { |
for (int i = 0; i < kNumTestingMessageLoops; i++) { |
- base::MessageLoop loop(testing_message_loops[i]); |
- |
- timers::RepeatingAlarmTimer timer; |
- bool did_run = false; |
- |
- base::RunLoop run_loop; |
- |
- timer.SetTimerFiredCallbackForTest(run_loop.QuitClosure()); |
- timer.Start(FROM_HERE, kTenMilliseconds, |
- base::Bind(&TimerRanCallback, &did_run)); |
- |
- // Wait until the timer has fired and a task has been queue in the |
- // MessageLoop. |
- run_loop.Run(); |
- |
- // Now reset the timer. This is attempting to simulate the timer firing and |
- // being reset at the same time. The previously queued task should be |
- // removed. |
- timer.Reset(); |
- |
- base::RunLoop().RunUntilIdle(); |
- EXPECT_FALSE(did_run); |
- |
- // If the previous check failed, running the message loop again will hang |
- // the test so we only do it if the callback has not run yet. |
- if (!did_run) { |
- base::RunLoop().Run(); |
- EXPECT_TRUE(did_run); |
- } |
+ RunTest_RepeatingTimerConcurrentResetAndTimerFired( |
+ testing_message_loops[i]); |
} |
} |