blob: 1a10edb5c0cc4235bcb26c55fe03992844d03bbf [file] [log] [blame]
chirantana4351572014-10-25 04:44:42 +09001// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
chirantanc88389f2015-04-08 05:43:11 +09005#include <sys/timerfd.h>
6
dcheng7822a7f2016-04-26 09:35:02 +09007#include <memory>
8
chirantana4351572014-10-25 04:44:42 +09009#include "base/bind.h"
10#include "base/bind_helpers.h"
fdoray5db52172016-10-12 02:31:13 +090011#include "base/files/file_descriptor_watcher_posix.h"
fdorayc6741742016-06-16 01:01:03 +090012#include "base/location.h"
chirantana4351572014-10-25 04:44:42 +090013#include "base/macros.h"
14#include "base/message_loop/message_loop.h"
15#include "base/run_loop.h"
fdorayc6741742016-06-16 01:01:03 +090016#include "base/single_thread_task_runner.h"
chirantana4351572014-10-25 04:44:42 +090017#include "base/threading/platform_thread.h"
fdorayc6741742016-06-16 01:01:03 +090018#include "base/threading/thread_task_runner_handle.h"
chirantana4351572014-10-25 04:44:42 +090019#include "base/time/time.h"
chirantanc88389f2015-04-08 05:43:11 +090020#include "components/timers/alarm_timer_chromeos.h"
chirantana4351572014-10-25 04:44:42 +090021#include "testing/gtest/include/gtest/gtest.h"
22
23// Most of these tests have been lifted right out of timer_unittest.cc with only
24// cosmetic changes (like replacing calls to MessageLoop::current()->Run() with
25// a RunLoop). We want the AlarmTimer to be a drop-in replacement for the
26// regular Timer so it should pass the same tests as the Timer class.
chirantana4351572014-10-25 04:44:42 +090027namespace timers {
28namespace {
chirantana4351572014-10-25 04:44:42 +090029const base::TimeDelta kTenMilliseconds = base::TimeDelta::FromMilliseconds(10);
30
31class OneShotAlarmTimerTester {
32 public:
33 OneShotAlarmTimerTester(bool* did_run, base::TimeDelta delay)
34 : did_run_(did_run),
35 delay_(delay),
chirantanc88389f2015-04-08 05:43:11 +090036 timer_(new timers::OneShotAlarmTimer()) {}
chirantana4351572014-10-25 04:44:42 +090037 void Start() {
chirantanc88389f2015-04-08 05:43:11 +090038 timer_->Start(FROM_HERE, delay_, base::Bind(&OneShotAlarmTimerTester::Run,
39 base::Unretained(this)));
chirantana4351572014-10-25 04:44:42 +090040 }
41
42 private:
43 void Run() {
44 *did_run_ = true;
45
fdorayc6741742016-06-16 01:01:03 +090046 base::ThreadTaskRunnerHandle::Get()->PostTask(
chirantana4351572014-10-25 04:44:42 +090047 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
48 }
49
50 bool* did_run_;
51 const base::TimeDelta delay_;
dcheng7822a7f2016-04-26 09:35:02 +090052 std::unique_ptr<timers::OneShotAlarmTimer> timer_;
chirantana4351572014-10-25 04:44:42 +090053
54 DISALLOW_COPY_AND_ASSIGN(OneShotAlarmTimerTester);
55};
56
57class OneShotSelfDeletingAlarmTimerTester {
58 public:
59 OneShotSelfDeletingAlarmTimerTester(bool* did_run, base::TimeDelta delay)
60 : did_run_(did_run),
61 delay_(delay),
chirantanc88389f2015-04-08 05:43:11 +090062 timer_(new timers::OneShotAlarmTimer()) {}
chirantana4351572014-10-25 04:44:42 +090063 void Start() {
chirantanc88389f2015-04-08 05:43:11 +090064 timer_->Start(FROM_HERE, delay_,
chirantana4351572014-10-25 04:44:42 +090065 base::Bind(&OneShotSelfDeletingAlarmTimerTester::Run,
66 base::Unretained(this)));
67 }
68
69 private:
70 void Run() {
71 *did_run_ = true;
72 timer_.reset();
73
fdorayc6741742016-06-16 01:01:03 +090074 base::ThreadTaskRunnerHandle::Get()->PostTask(
chirantana4351572014-10-25 04:44:42 +090075 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
76 }
77
78 bool* did_run_;
79 const base::TimeDelta delay_;
dcheng7822a7f2016-04-26 09:35:02 +090080 std::unique_ptr<timers::OneShotAlarmTimer> timer_;
chirantana4351572014-10-25 04:44:42 +090081
82 DISALLOW_COPY_AND_ASSIGN(OneShotSelfDeletingAlarmTimerTester);
83};
84
85class RepeatingAlarmTimerTester {
86 public:
87 RepeatingAlarmTimerTester(bool* did_run, base::TimeDelta delay)
88 : did_run_(did_run),
89 delay_(delay),
90 counter_(10),
chirantanc88389f2015-04-08 05:43:11 +090091 timer_(new timers::RepeatingAlarmTimer()) {}
chirantana4351572014-10-25 04:44:42 +090092 void Start() {
chirantanc88389f2015-04-08 05:43:11 +090093 timer_->Start(FROM_HERE, delay_, base::Bind(&RepeatingAlarmTimerTester::Run,
94 base::Unretained(this)));
chirantana4351572014-10-25 04:44:42 +090095 }
96
97 private:
98 void Run() {
99 if (--counter_ == 0) {
100 *did_run_ = true;
101 timer_->Stop();
102
fdorayc6741742016-06-16 01:01:03 +0900103 base::ThreadTaskRunnerHandle::Get()->PostTask(
chirantana4351572014-10-25 04:44:42 +0900104 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
105 }
106 }
107
108 bool* did_run_;
109 const base::TimeDelta delay_;
110 int counter_;
dcheng7822a7f2016-04-26 09:35:02 +0900111 std::unique_ptr<timers::RepeatingAlarmTimer> timer_;
chirantana4351572014-10-25 04:44:42 +0900112
113 DISALLOW_COPY_AND_ASSIGN(RepeatingAlarmTimerTester);
114};
115
chirantana4351572014-10-25 04:44:42 +0900116} // namespace
117
118//-----------------------------------------------------------------------------
119// Each test is run against each type of MessageLoop. That way we are sure
120// that timers work properly in all configurations.
121
122TEST(AlarmTimerTest, OneShotAlarmTimer) {
fdoray5db52172016-10-12 02:31:13 +0900123 base::MessageLoopForIO loop;
124 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900125
fdoray5db52172016-10-12 02:31:13 +0900126 bool did_run = false;
127 OneShotAlarmTimerTester f(&did_run, kTenMilliseconds);
128 f.Start();
chirantanc88389f2015-04-08 05:43:11 +0900129
fdoray5db52172016-10-12 02:31:13 +0900130 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900131
fdoray5db52172016-10-12 02:31:13 +0900132 EXPECT_TRUE(did_run);
chirantana4351572014-10-25 04:44:42 +0900133}
134
135TEST(AlarmTimerTest, OneShotAlarmTimer_Cancel) {
fdoray5db52172016-10-12 02:31:13 +0900136 base::MessageLoopForIO loop;
137 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900138
fdoray5db52172016-10-12 02:31:13 +0900139 bool did_run_a = false;
140 OneShotAlarmTimerTester* a =
141 new OneShotAlarmTimerTester(&did_run_a, kTenMilliseconds);
chirantanc88389f2015-04-08 05:43:11 +0900142
fdoray5db52172016-10-12 02:31:13 +0900143 // This should run before the timer expires.
144 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a);
chirantanc88389f2015-04-08 05:43:11 +0900145
fdoray5db52172016-10-12 02:31:13 +0900146 // Now start the timer.
147 a->Start();
chirantanc88389f2015-04-08 05:43:11 +0900148
fdoray5db52172016-10-12 02:31:13 +0900149 bool did_run_b = false;
150 OneShotAlarmTimerTester b(&did_run_b, kTenMilliseconds);
151 b.Start();
chirantanc88389f2015-04-08 05:43:11 +0900152
fdoray5db52172016-10-12 02:31:13 +0900153 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900154
fdoray5db52172016-10-12 02:31:13 +0900155 EXPECT_FALSE(did_run_a);
156 EXPECT_TRUE(did_run_b);
chirantana4351572014-10-25 04:44:42 +0900157}
158
159// If underlying timer does not handle this properly, we will crash or fail
160// in full page heap environment.
161TEST(AlarmTimerTest, OneShotSelfDeletingAlarmTimer) {
fdoray5db52172016-10-12 02:31:13 +0900162 base::MessageLoopForIO loop;
163 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900164
fdoray5db52172016-10-12 02:31:13 +0900165 bool did_run = false;
166 OneShotSelfDeletingAlarmTimerTester f(&did_run, kTenMilliseconds);
167 f.Start();
chirantanc88389f2015-04-08 05:43:11 +0900168
fdoray5db52172016-10-12 02:31:13 +0900169 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900170
fdoray5db52172016-10-12 02:31:13 +0900171 EXPECT_TRUE(did_run);
chirantana4351572014-10-25 04:44:42 +0900172}
173
174TEST(AlarmTimerTest, RepeatingAlarmTimer) {
fdoray5db52172016-10-12 02:31:13 +0900175 base::MessageLoopForIO loop;
176 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900177
fdoray5db52172016-10-12 02:31:13 +0900178 bool did_run = false;
179 RepeatingAlarmTimerTester f(&did_run, kTenMilliseconds);
180 f.Start();
chirantanc88389f2015-04-08 05:43:11 +0900181
fdoray5db52172016-10-12 02:31:13 +0900182 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900183
fdoray5db52172016-10-12 02:31:13 +0900184 EXPECT_TRUE(did_run);
chirantana4351572014-10-25 04:44:42 +0900185}
186
187TEST(AlarmTimerTest, RepeatingAlarmTimer_Cancel) {
fdoray5db52172016-10-12 02:31:13 +0900188 base::MessageLoopForIO loop;
189 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900190
fdoray5db52172016-10-12 02:31:13 +0900191 bool did_run_a = false;
192 RepeatingAlarmTimerTester* a =
193 new RepeatingAlarmTimerTester(&did_run_a, kTenMilliseconds);
chirantanc88389f2015-04-08 05:43:11 +0900194
fdoray5db52172016-10-12 02:31:13 +0900195 // This should run before the timer expires.
196 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a);
chirantanc88389f2015-04-08 05:43:11 +0900197
fdoray5db52172016-10-12 02:31:13 +0900198 // Now start the timer.
199 a->Start();
chirantanc88389f2015-04-08 05:43:11 +0900200
fdoray5db52172016-10-12 02:31:13 +0900201 bool did_run_b = false;
202 RepeatingAlarmTimerTester b(&did_run_b, kTenMilliseconds);
203 b.Start();
chirantanc88389f2015-04-08 05:43:11 +0900204
fdoray5db52172016-10-12 02:31:13 +0900205 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900206
fdoray5db52172016-10-12 02:31:13 +0900207 EXPECT_FALSE(did_run_a);
208 EXPECT_TRUE(did_run_b);
chirantana4351572014-10-25 04:44:42 +0900209}
210
211TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay) {
fdoray5db52172016-10-12 02:31:13 +0900212 base::MessageLoopForIO loop;
213 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900214
fdoray5db52172016-10-12 02:31:13 +0900215 bool did_run = false;
216 RepeatingAlarmTimerTester f(&did_run, base::TimeDelta());
217 f.Start();
chirantanc88389f2015-04-08 05:43:11 +0900218
fdoray5db52172016-10-12 02:31:13 +0900219 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900220
fdoray5db52172016-10-12 02:31:13 +0900221 EXPECT_TRUE(did_run);
chirantana4351572014-10-25 04:44:42 +0900222}
223
224TEST(AlarmTimerTest, RepeatingAlarmTimerZeroDelay_Cancel) {
fdoray5db52172016-10-12 02:31:13 +0900225 base::MessageLoopForIO loop;
226 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900227
fdoray5db52172016-10-12 02:31:13 +0900228 bool did_run_a = false;
229 RepeatingAlarmTimerTester* a =
230 new RepeatingAlarmTimerTester(&did_run_a, base::TimeDelta());
chirantanc88389f2015-04-08 05:43:11 +0900231
fdoray5db52172016-10-12 02:31:13 +0900232 // This should run before the timer expires.
233 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a);
chirantanc88389f2015-04-08 05:43:11 +0900234
fdoray5db52172016-10-12 02:31:13 +0900235 // Now start the timer.
236 a->Start();
chirantanc88389f2015-04-08 05:43:11 +0900237
fdoray5db52172016-10-12 02:31:13 +0900238 bool did_run_b = false;
239 RepeatingAlarmTimerTester b(&did_run_b, base::TimeDelta());
240 b.Start();
chirantanc88389f2015-04-08 05:43:11 +0900241
fdoray5db52172016-10-12 02:31:13 +0900242 base::RunLoop().Run();
chirantanc88389f2015-04-08 05:43:11 +0900243
fdoray5db52172016-10-12 02:31:13 +0900244 EXPECT_FALSE(did_run_a);
245 EXPECT_TRUE(did_run_b);
chirantana4351572014-10-25 04:44:42 +0900246}
247
248TEST(AlarmTimerTest, MessageLoopShutdown) {
249 // This test is designed to verify that shutdown of the
250 // message loop does not cause crashes if there were pending
251 // timers not yet fired. It may only trigger exceptions
252 // if debug heap checking is enabled.
253 bool did_run = false;
254 {
Gyuyoung Kimf2bab8d2018-01-23 22:34:37 +0900255 auto loop = std::make_unique<base::MessageLoopForIO>();
fdoray5db52172016-10-12 02:31:13 +0900256 auto file_descriptor_watcher =
Gyuyoung Kimf2bab8d2018-01-23 22:34:37 +0900257 std::make_unique<base::FileDescriptorWatcher>(loop.get());
chirantana4351572014-10-25 04:44:42 +0900258 OneShotAlarmTimerTester a(&did_run, kTenMilliseconds);
259 OneShotAlarmTimerTester b(&did_run, kTenMilliseconds);
260 OneShotAlarmTimerTester c(&did_run, kTenMilliseconds);
261 OneShotAlarmTimerTester d(&did_run, kTenMilliseconds);
fdoray5db52172016-10-12 02:31:13 +0900262
263 a.Start();
264 b.Start();
265
266 // Allow FileDescriptorWatcher to start watching the timers. Without this,
267 // tasks posted by FileDescriptorWatcher::WatchReadable() are leaked.
268 base::RunLoop().RunUntilIdle();
269
270 // MessageLoop and FileDescriptorWatcher destruct.
271 file_descriptor_watcher.reset();
272 loop.reset();
273 } // OneShotTimers destruct. SHOULD NOT CRASH, of course.
chirantana4351572014-10-25 04:44:42 +0900274
275 EXPECT_FALSE(did_run);
276}
277
278TEST(AlarmTimerTest, NonRepeatIsRunning) {
279 {
fdoray5db52172016-10-12 02:31:13 +0900280 base::MessageLoopForIO loop;
281 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900282 timers::OneShotAlarmTimer timer;
chirantana4351572014-10-25 04:44:42 +0900283 EXPECT_FALSE(timer.IsRunning());
Peter Kasting24efe5e2018-02-24 09:03:01 +0900284 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing());
fdoray5db52172016-10-12 02:31:13 +0900285
286 // Allow FileDescriptorWatcher to start watching the timer. Without this, a
287 // task posted by FileDescriptorWatcher::WatchReadable() is leaked.
288 base::RunLoop().RunUntilIdle();
289
chirantana4351572014-10-25 04:44:42 +0900290 EXPECT_TRUE(timer.IsRunning());
291 timer.Stop();
292 EXPECT_FALSE(timer.IsRunning());
293 EXPECT_TRUE(timer.user_task().is_null());
294 }
295
296 {
fdoray5db52172016-10-12 02:31:13 +0900297 base::MessageLoopForIO loop;
298 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
chirantanc88389f2015-04-08 05:43:11 +0900299 timers::SimpleAlarmTimer timer;
chirantana4351572014-10-25 04:44:42 +0900300 EXPECT_FALSE(timer.IsRunning());
Peter Kasting24efe5e2018-02-24 09:03:01 +0900301 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing());
fdoray5db52172016-10-12 02:31:13 +0900302
303 // Allow FileDescriptorWatcher to start watching the timer. Without this, a
304 // task posted by FileDescriptorWatcher::WatchReadable() is leaked.
305 base::RunLoop().RunUntilIdle();
306
chirantana4351572014-10-25 04:44:42 +0900307 EXPECT_TRUE(timer.IsRunning());
308 timer.Stop();
309 EXPECT_FALSE(timer.IsRunning());
310 ASSERT_FALSE(timer.user_task().is_null());
311 timer.Reset();
fdoray5db52172016-10-12 02:31:13 +0900312 base::RunLoop().RunUntilIdle();
chirantana4351572014-10-25 04:44:42 +0900313 EXPECT_TRUE(timer.IsRunning());
314 }
315}
316
naskod3fd0db2016-10-07 06:22:32 +0900317TEST(AlarmTimerTest, RetainRepeatIsRunning) {
fdoray5db52172016-10-12 02:31:13 +0900318 base::MessageLoopForIO loop;
319 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
320 timers::RepeatingAlarmTimer timer;
naskod3fd0db2016-10-07 06:22:32 +0900321 EXPECT_FALSE(timer.IsRunning());
Peter Kasting24efe5e2018-02-24 09:03:01 +0900322 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing());
fdoray5db52172016-10-12 02:31:13 +0900323
324 // Allow FileDescriptorWatcher to start watching the timer. Without this, a
325 // task posted by FileDescriptorWatcher::WatchReadable() is leaked.
326 base::RunLoop().RunUntilIdle();
327
328 EXPECT_TRUE(timer.IsRunning());
chirantana4351572014-10-25 04:44:42 +0900329 timer.Reset();
fdoray5db52172016-10-12 02:31:13 +0900330 base::RunLoop().RunUntilIdle();
chirantana4351572014-10-25 04:44:42 +0900331 EXPECT_TRUE(timer.IsRunning());
332 timer.Stop();
333 EXPECT_FALSE(timer.IsRunning());
334 timer.Reset();
fdoray5db52172016-10-12 02:31:13 +0900335 base::RunLoop().RunUntilIdle();
chirantana4351572014-10-25 04:44:42 +0900336 EXPECT_TRUE(timer.IsRunning());
337}
338
339TEST(AlarmTimerTest, RetainNonRepeatIsRunning) {
fdoray5db52172016-10-12 02:31:13 +0900340 base::MessageLoopForIO loop;
341 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
342 timers::SimpleAlarmTimer timer;
chirantana4351572014-10-25 04:44:42 +0900343 EXPECT_FALSE(timer.IsRunning());
Peter Kasting24efe5e2018-02-24 09:03:01 +0900344 timer.Start(FROM_HERE, base::TimeDelta::FromDays(1), base::DoNothing());
fdoray5db52172016-10-12 02:31:13 +0900345
346 // Allow FileDescriptorWatcher to start watching the timer. Without this, a
347 // task posted by FileDescriptorWatcher::WatchReadable() is leaked.
348 base::RunLoop().RunUntilIdle();
349
350 EXPECT_TRUE(timer.IsRunning());
chirantana4351572014-10-25 04:44:42 +0900351 timer.Reset();
fdoray5db52172016-10-12 02:31:13 +0900352 base::RunLoop().RunUntilIdle();
chirantana4351572014-10-25 04:44:42 +0900353 EXPECT_TRUE(timer.IsRunning());
354 timer.Stop();
355 EXPECT_FALSE(timer.IsRunning());
356 timer.Reset();
fdoray5db52172016-10-12 02:31:13 +0900357 base::RunLoop().RunUntilIdle();
chirantana4351572014-10-25 04:44:42 +0900358 EXPECT_TRUE(timer.IsRunning());
359}
360
361namespace {
362
363bool g_callback_happened1 = false;
364bool g_callback_happened2 = false;
365
366void ClearAllCallbackHappened() {
367 g_callback_happened1 = false;
368 g_callback_happened2 = false;
369}
370
371void SetCallbackHappened1() {
372 g_callback_happened1 = true;
fdorayc6741742016-06-16 01:01:03 +0900373 base::ThreadTaskRunnerHandle::Get()->PostTask(
chirantana4351572014-10-25 04:44:42 +0900374 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
375}
376
377void SetCallbackHappened2() {
378 g_callback_happened2 = true;
fdorayc6741742016-06-16 01:01:03 +0900379 base::ThreadTaskRunnerHandle::Get()->PostTask(
chirantana4351572014-10-25 04:44:42 +0900380 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
381}
382
383TEST(AlarmTimerTest, ContinuationStopStart) {
fdoray5db52172016-10-12 02:31:13 +0900384 ClearAllCallbackHappened();
385 base::MessageLoopForIO loop;
386 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
387 timers::OneShotAlarmTimer timer;
388 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10),
389 base::Bind(&SetCallbackHappened1));
390 timer.Stop();
391 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(40),
392 base::Bind(&SetCallbackHappened2));
393 base::RunLoop().Run();
394 EXPECT_FALSE(g_callback_happened1);
395 EXPECT_TRUE(g_callback_happened2);
chirantana4351572014-10-25 04:44:42 +0900396}
397
398TEST(AlarmTimerTest, ContinuationReset) {
fdoray5db52172016-10-12 02:31:13 +0900399 ClearAllCallbackHappened();
400 base::MessageLoopForIO loop;
401 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
402 timers::OneShotAlarmTimer timer;
403 timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10),
404 base::Bind(&SetCallbackHappened1));
405 timer.Reset();
406 // Since Reset happened before task ran, the user_task must not be cleared:
407 ASSERT_FALSE(timer.user_task().is_null());
408 base::RunLoop().Run();
409 EXPECT_TRUE(g_callback_happened1);
410}
411
412// Verify that no crash occurs if a timer is deleted while its callback is
413// running.
414TEST(AlarmTimerTest, DeleteTimerWhileCallbackIsRunning) {
415 base::MessageLoopForIO loop;
416 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
417 base::RunLoop run_loop;
418
419 // Will be deleted by the callback.
420 timers::OneShotAlarmTimer* timer = new timers::OneShotAlarmTimer;
421
422 timer->Start(
423 FROM_HERE, base::TimeDelta::FromMilliseconds(10),
424 base::Bind(
425 [](timers::OneShotAlarmTimer* timer, base::RunLoop* run_loop) {
426 delete timer;
427 run_loop->Quit();
428 },
429 timer, &run_loop));
430 run_loop.Run();
431}
432
433// Verify that no crash occurs if a zero-delay timer is deleted while its
434// callback is running.
435TEST(AlarmTimerTest, DeleteTimerWhileCallbackIsRunningZeroDelay) {
436 base::MessageLoopForIO loop;
437 base::FileDescriptorWatcher file_descriptor_watcher(&loop);
438 base::RunLoop run_loop;
439
440 // Will be deleted by the callback.
441 timers::OneShotAlarmTimer* timer = new timers::OneShotAlarmTimer;
442
443 timer->Start(
444 FROM_HERE, base::TimeDelta(),
445 base::Bind(
446 [](timers::OneShotAlarmTimer* timer, base::RunLoop* run_loop) {
447 delete timer;
448 run_loop->Quit();
449 },
450 timer, &run_loop));
451 run_loop.Run();
chirantana4351572014-10-25 04:44:42 +0900452}
453
454} // namespace
chirantana4351572014-10-25 04:44:42 +0900455} // namespace timers