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