Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: base/timer/timer_unittest.cc

Issue 637983003: Add support to base::Timer for custom task runners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update for review feedback Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/timer/timer.cc ('K') | « base/timer/timer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/test/test_simple_task_runner.h"
7 #include "base/timer/timer.h" 8 #include "base/timer/timer.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 using base::TimeDelta; 11 using base::TimeDelta;
12 using base::SingleThreadTaskRunner;
11 13
12 namespace { 14 namespace {
13 15
14 // The message loops on which each timer should be tested. 16 // The message loops on which each timer should be tested.
15 const base::MessageLoop::Type testing_message_loops[] = { 17 const base::MessageLoop::Type testing_message_loops[] = {
16 base::MessageLoop::TYPE_DEFAULT, 18 base::MessageLoop::TYPE_DEFAULT,
17 base::MessageLoop::TYPE_IO, 19 base::MessageLoop::TYPE_IO,
18 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. 20 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
19 base::MessageLoop::TYPE_UI, 21 base::MessageLoop::TYPE_UI,
20 #endif 22 #endif
21 }; 23 };
22 24
23 const int kNumTestingMessageLoops = arraysize(testing_message_loops); 25 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
24 26
25 class OneShotTimerTester { 27 class OneShotTimerTester {
26 public: 28 public:
27 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) 29 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10)
28 : did_run_(did_run), 30 : did_run_(did_run),
29 delay_ms_(milliseconds) { 31 delay_ms_(milliseconds),
32 quit_message_loop_(true) {
30 } 33 }
31 void Start() { 34 void Start() {
32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, 35 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this,
33 &OneShotTimerTester::Run); 36 &OneShotTimerTester::Run);
34 } 37 }
38 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
39 quit_message_loop_ = false;
40 timer_.SetTaskRunner(task_runner);
41 }
35 private: 42 private:
36 void Run() { 43 void Run() {
37 *did_run_ = true; 44 *did_run_ = true;
38 base::MessageLoop::current()->QuitWhenIdle(); 45 if (quit_message_loop_) {
46 base::MessageLoop::current()->QuitWhenIdle();
47 }
39 } 48 }
40 bool* did_run_; 49 bool* did_run_;
41 base::OneShotTimer<OneShotTimerTester> timer_; 50 base::OneShotTimer<OneShotTimerTester> timer_;
42 const unsigned delay_ms_; 51 const unsigned delay_ms_;
52 bool quit_message_loop_;
43 }; 53 };
44 54
45 class OneShotSelfDeletingTimerTester { 55 class OneShotSelfDeletingTimerTester {
46 public: 56 public:
47 explicit OneShotSelfDeletingTimerTester(bool* did_run) : 57 explicit OneShotSelfDeletingTimerTester(bool* did_run) :
48 did_run_(did_run), 58 did_run_(did_run),
49 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { 59 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) {
50 } 60 }
51 void Start() { 61 void Start() {
52 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, 62 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 135
126 bool did_run = false; 136 bool did_run = false;
127 OneShotSelfDeletingTimerTester f(&did_run); 137 OneShotSelfDeletingTimerTester f(&did_run);
128 f.Start(); 138 f.Start();
129 139
130 base::MessageLoop::current()->Run(); 140 base::MessageLoop::current()->Run();
131 141
132 EXPECT_TRUE(did_run); 142 EXPECT_TRUE(did_run);
133 } 143 }
134 144
145 void RunTest_OneShotTimer_CustomTaskRunner(
146 base::MessageLoop::Type message_loop_type) {
147 base::MessageLoop loop(message_loop_type);
148 scoped_refptr<base::TestSimpleTaskRunner> task_runner =
149 new base::TestSimpleTaskRunner();
150
151 // Create three timers. While the second one uses the message loop (default
152 // behavior), the first and third one use the task runner.
153 bool did_run1 = false;
154 bool did_run2 = false;
155 bool did_run3 = false;
156 OneShotTimerTester f1(&did_run1);
157 OneShotTimerTester f2(&did_run2);
158 OneShotTimerTester f3(&did_run3);
159 f1.SetTaskRunner(task_runner);
160 f3.SetTaskRunner(task_runner);
161
162 // Start the first and second timer and run the task runner. Only the first
163 // timer should go off.
164 f1.Start();
danakj 2014/10/16 17:14:59 I think this test could be a bit more simple and a
petrcermak 2014/10/17 15:43:15 My point was to check that the main loop and the t
danakj 2014/10/18 18:59:59 I think you just need to test where the task gets
petrcermak 2014/10/20 12:42:41 Done.
165 f2.Start();
166 task_runner->RunUntilIdle();
167
168 EXPECT_TRUE(did_run1);
169 EXPECT_FALSE(did_run2);
170 EXPECT_FALSE(did_run3);
171 did_run1 = false;
172
173 // Start the third timer and run the message loop. The second timer should
174 // go off this time.
175 f3.Start();
176 base::MessageLoop::current()->Run();
177
178 EXPECT_FALSE(did_run1);
179 EXPECT_TRUE(did_run2);
180 EXPECT_FALSE(did_run3);
181 did_run2 = false;
182
183 // Run the task runner again. Finally, the third timer should go off.
184 task_runner->RunUntilIdle();
185
186 EXPECT_FALSE(did_run1);
187 EXPECT_FALSE(did_run2);
188 EXPECT_TRUE(did_run3);
189 }
190
135 void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type, 191 void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type,
136 const TimeDelta& delay) { 192 const TimeDelta& delay) {
137 base::MessageLoop loop(message_loop_type); 193 base::MessageLoop loop(message_loop_type);
138 194
139 bool did_run = false; 195 bool did_run = false;
140 RepeatingTimerTester f(&did_run, delay); 196 RepeatingTimerTester f(&did_run, delay);
141 f.Start(); 197 f.Start();
142 198
143 base::MessageLoop::current()->Run(); 199 base::MessageLoop::current()->Run();
144 200
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 359 }
304 360
305 // If underline timer does not handle properly, we will crash or fail 361 // If underline timer does not handle properly, we will crash or fail
306 // in full page heap environment. 362 // in full page heap environment.
307 TEST(TimerTest, OneShotSelfDeletingTimer) { 363 TEST(TimerTest, OneShotSelfDeletingTimer) {
308 for (int i = 0; i < kNumTestingMessageLoops; i++) { 364 for (int i = 0; i < kNumTestingMessageLoops; i++) {
309 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); 365 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]);
310 } 366 }
311 } 367 }
312 368
369 TEST(TimerTest, RunTest_OneShotTimer_CustomTaskRunner) {
Avi (use Gerrit) 2014/10/16 18:02:55 Drop "RunTest_" from the test name.
petrcermak 2014/10/17 15:43:15 Done.
370 for (int i = 0; i < kNumTestingMessageLoops; i++) {
Avi (use Gerrit) 2014/10/16 18:02:55 Completely optional, but we have for ( : ) now if
petrcermak 2014/10/17 15:43:15 Acknowledged. I kept the original for loop for con
371 RunTest_OneShotTimer_CustomTaskRunner(testing_message_loops[i]);
372 }
373 }
374
313 TEST(TimerTest, RepeatingTimer) { 375 TEST(TimerTest, RepeatingTimer) {
314 for (int i = 0; i < kNumTestingMessageLoops; i++) { 376 for (int i = 0; i < kNumTestingMessageLoops; i++) {
315 RunTest_RepeatingTimer(testing_message_loops[i], 377 RunTest_RepeatingTimer(testing_message_loops[i],
316 TimeDelta::FromMilliseconds(10)); 378 TimeDelta::FromMilliseconds(10));
317 } 379 }
318 } 380 }
319 381
320 TEST(TimerTest, RepeatingTimer_Cancel) { 382 TEST(TimerTest, RepeatingTimer_Cancel) {
321 for (int i = 0; i < kNumTestingMessageLoops; i++) { 383 for (int i = 0; i < kNumTestingMessageLoops; i++) {
322 RunTest_RepeatingTimer_Cancel(testing_message_loops[i], 384 RunTest_RepeatingTimer_Cancel(testing_message_loops[i],
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 base::Bind(&SetCallbackHappened1)); 561 base::Bind(&SetCallbackHappened1));
500 timer.Reset(); 562 timer.Reset();
501 // Since Reset happened before task ran, the user_task must not be cleared: 563 // Since Reset happened before task ran, the user_task must not be cleared:
502 ASSERT_FALSE(timer.user_task().is_null()); 564 ASSERT_FALSE(timer.user_task().is_null());
503 base::MessageLoop::current()->Run(); 565 base::MessageLoop::current()->Run();
504 EXPECT_TRUE(g_callback_happened1); 566 EXPECT_TRUE(g_callback_happened1);
505 } 567 }
506 } 568 }
507 569
508 } // namespace 570 } // namespace
OLDNEW
« base/timer/timer.cc ('K') | « base/timer/timer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698