| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/platform_thread.h" | 7 #include "base/platform_thread.h" |
| 8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
| 9 #include "base/thread.h" | 9 #include "base/thread.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 283 |
| 284 loop.PostDelayedTask( | 284 loop.PostDelayedTask( |
| 285 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); | 285 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 1); |
| 286 | 286 |
| 287 loop.Run(); | 287 loop.Run(); |
| 288 EXPECT_EQ(0, num_tasks); | 288 EXPECT_EQ(0, num_tasks); |
| 289 | 289 |
| 290 EXPECT_TRUE(run_time2 > run_time1); | 290 EXPECT_TRUE(run_time2 > run_time1); |
| 291 } | 291 } |
| 292 | 292 |
| 293 void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) { |
| 294 MessageLoop loop(message_loop_type); |
| 295 |
| 296 // Test that the interval of the timer, used to run the next delayed task, is |
| 297 // set to a value corresponding to when the next delayed task should run. |
| 298 |
| 299 // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 300 // run loop to exit. |
| 301 int num_tasks = 1; |
| 302 Time run_time1, run_time2; |
| 303 |
| 304 loop.PostDelayedTask( |
| 305 FROM_HERE, new RecordRunTimeTask(&run_time1, &num_tasks), 1000000); |
| 306 loop.PostDelayedTask( |
| 307 FROM_HERE, new RecordRunTimeTask(&run_time2, &num_tasks), 10); |
| 308 |
| 309 Time start_time = Time::Now(); |
| 310 |
| 311 loop.Run(); |
| 312 EXPECT_EQ(0, num_tasks); |
| 313 |
| 314 // Ensure that we ran in far less time than the slower timer. |
| 315 TimeDelta run_time = Time::Now() - start_time; |
| 316 EXPECT_GT(5000, run_time.InMilliseconds()); |
| 317 |
| 318 // In case both timers somehow run at nearly the same time, sleep a little |
| 319 // and then run all pending to force them both to have run. This is just |
| 320 // encouraging flakiness if there is any. |
| 321 PlatformThread::Sleep(100); |
| 322 loop.RunAllPending(); |
| 323 |
| 324 EXPECT_TRUE(run_time1.is_null()); |
| 325 EXPECT_FALSE(run_time2.is_null()); |
| 326 } |
| 327 |
| 328 #if defined(OS_WIN) |
| 329 |
| 330 class SubPumpTask : public Task { |
| 331 public: |
| 332 virtual void Run() { |
| 333 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 334 MSG msg; |
| 335 while (GetMessage(&msg, NULL, 0, 0)) { |
| 336 TranslateMessage(&msg); |
| 337 DispatchMessage(&msg); |
| 338 } |
| 339 MessageLoop::current()->Quit(); |
| 340 } |
| 341 }; |
| 342 |
| 343 class SubPumpQuitTask : public Task { |
| 344 public: |
| 345 SubPumpQuitTask() { |
| 346 } |
| 347 virtual void Run() { |
| 348 PostQuitMessage(0); |
| 349 } |
| 350 }; |
| 351 |
| 352 void RunTest_PostDelayedTask_SharedTimer_SubPump() { |
| 353 MessageLoop loop(MessageLoop::TYPE_UI); |
| 354 |
| 355 // Test that the interval of the timer, used to run the next delayed task, is |
| 356 // set to a value corresponding to when the next delayed task should run. |
| 357 |
| 358 // By setting num_tasks to 1, we ensure that the first task to run causes the |
| 359 // run loop to exit. |
| 360 int num_tasks = 1; |
| 361 Time run_time; |
| 362 |
| 363 loop.PostTask(FROM_HERE, new SubPumpTask()); |
| 364 |
| 365 // This very delayed task should never run. |
| 366 loop.PostDelayedTask( |
| 367 FROM_HERE, new RecordRunTimeTask(&run_time, &num_tasks), 1000000); |
| 368 |
| 369 // This slightly delayed task should run from within SubPumpTask::Run(). |
| 370 loop.PostDelayedTask( |
| 371 FROM_HERE, new SubPumpQuitTask(), 10); |
| 372 |
| 373 Time start_time = Time::Now(); |
| 374 |
| 375 loop.Run(); |
| 376 EXPECT_EQ(1, num_tasks); |
| 377 |
| 378 // Ensure that we ran in far less time than the slower timer. |
| 379 TimeDelta run_time = Time::Now() - start_time; |
| 380 EXPECT_GT(5000, run_time.InMilliseconds()); |
| 381 |
| 382 // In case both timers somehow run at nearly the same time, sleep a little |
| 383 // and then run all pending to force them both to have run. This is just |
| 384 // encouraging flakiness if there is any. |
| 385 PlatformThread::Sleep(100); |
| 386 loop.RunAllPending(); |
| 387 |
| 388 EXPECT_TRUE(run_time.is_null()); |
| 389 } |
| 390 |
| 391 #endif // defined(OS_WIN) |
| 392 |
| 293 class RecordDeletionTask : public Task { | 393 class RecordDeletionTask : public Task { |
| 294 public: | 394 public: |
| 295 RecordDeletionTask(Task* post_on_delete, bool* was_deleted) | 395 RecordDeletionTask(Task* post_on_delete, bool* was_deleted) |
| 296 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { | 396 : post_on_delete_(post_on_delete), was_deleted_(was_deleted) { |
| 297 } | 397 } |
| 298 ~RecordDeletionTask() { | 398 ~RecordDeletionTask() { |
| 299 *was_deleted_ = true; | 399 *was_deleted_ = true; |
| 300 if (post_on_delete_) | 400 if (post_on_delete_) |
| 301 MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_); | 401 MessageLoop::current()->PostTask(FROM_HERE, post_on_delete_); |
| 302 } | 402 } |
| (...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); | 1188 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI); |
| 1089 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); | 1189 RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO); |
| 1090 } | 1190 } |
| 1091 | 1191 |
| 1092 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { | 1192 TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) { |
| 1093 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); | 1193 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT); |
| 1094 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); | 1194 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI); |
| 1095 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); | 1195 RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO); |
| 1096 } | 1196 } |
| 1097 | 1197 |
| 1198 TEST(MessageLoopTest, PostDelayedTask_SharedTimer) { |
| 1199 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT); |
| 1200 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI); |
| 1201 RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_IO); |
| 1202 } |
| 1203 |
| 1204 #if defined(OS_WIN) |
| 1205 TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) { |
| 1206 RunTest_PostDelayedTask_SharedTimer_SubPump(); |
| 1207 } |
| 1208 #endif |
| 1209 |
| 1098 // TODO(darin): re-enable these tests once MessageLoop supports them again. | 1210 // TODO(darin): re-enable these tests once MessageLoop supports them again. |
| 1099 #if 0 | 1211 #if 0 |
| 1100 TEST(MessageLoopTest, EnsureTaskDeletion) { | 1212 TEST(MessageLoopTest, EnsureTaskDeletion) { |
| 1101 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); | 1213 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_DEFAULT); |
| 1102 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); | 1214 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_UI); |
| 1103 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); | 1215 RunTest_EnsureTaskDeletion(MessageLoop::TYPE_IO); |
| 1104 } | 1216 } |
| 1105 | 1217 |
| 1106 TEST(MessageLoopTest, EnsureTaskDeletion_Chain) { | 1218 TEST(MessageLoopTest, EnsureTaskDeletion_Chain) { |
| 1107 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); | 1219 RunTest_EnsureTaskDeletion_Chain(MessageLoop::TYPE_DEFAULT); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 TEST(MessageLoopTest, AutoresetEvents) { | 1283 TEST(MessageLoopTest, AutoresetEvents) { |
| 1172 // This test requires an IO loop | 1284 // This test requires an IO loop |
| 1173 RunTest_AutoresetEvents(MessageLoop::TYPE_IO); | 1285 RunTest_AutoresetEvents(MessageLoop::TYPE_IO); |
| 1174 } | 1286 } |
| 1175 | 1287 |
| 1176 TEST(MessageLoopTest, Dispatcher) { | 1288 TEST(MessageLoopTest, Dispatcher) { |
| 1177 // This test requires a UI loop | 1289 // This test requires a UI loop |
| 1178 RunTest_Dispatcher(MessageLoop::TYPE_UI); | 1290 RunTest_Dispatcher(MessageLoop::TYPE_UI); |
| 1179 } | 1291 } |
| 1180 #endif // defined(OS_WIN) | 1292 #endif // defined(OS_WIN) |
| OLD | NEW |