OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/task_scheduler/task_scheduler_impl.h" | 5 #include "base/task_scheduler/task_scheduler_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 20 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
21 #include "base/task_scheduler/task_traits.h" | 21 #include "base/task_scheduler/task_traits.h" |
22 #include "base/task_scheduler/test_task_factory.h" | 22 #include "base/task_scheduler/test_task_factory.h" |
23 #include "base/test/test_timeouts.h" | 23 #include "base/test/test_timeouts.h" |
24 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
25 #include "base/threading/simple_thread.h" | 25 #include "base/threading/simple_thread.h" |
26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
27 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 29 #include "build/build_config.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
| 32 #if defined(OS_POSIX) |
| 33 #include <unistd.h> |
| 34 |
| 35 #include "base/debug/leak_annotations.h" |
| 36 #include "base/files/file_descriptor_watcher_posix.h" |
| 37 #include "base/files/file_util.h" |
| 38 #include "base/posix/eintr_wrapper.h" |
| 39 #endif // defined(OS_POSIX) |
| 40 |
31 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
32 #include <objbase.h> | 42 #include <objbase.h> |
33 #endif // defined(OS_WIN) | 43 #endif // defined(OS_WIN) |
34 | 44 |
35 namespace base { | 45 namespace base { |
36 namespace internal { | 46 namespace internal { |
37 | 47 |
38 namespace { | 48 namespace { |
39 | 49 |
40 struct TraitsExecutionModePair { | 50 struct TraitsExecutionModePair { |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 ADD_FAILURE() << "COM STA was not initialized on this thread"; | 450 ADD_FAILURE() << "COM STA was not initialized on this thread"; |
441 CoUninitialize(); | 451 CoUninitialize(); |
442 } | 452 } |
443 task_ran->Signal(); | 453 task_ran->Signal(); |
444 }, | 454 }, |
445 com_sta_task_runner, Unretained(&task_ran))); | 455 com_sta_task_runner, Unretained(&task_ran))); |
446 task_ran.Wait(); | 456 task_ran.Wait(); |
447 } | 457 } |
448 #endif // defined(OS_WIN) | 458 #endif // defined(OS_WIN) |
449 | 459 |
| 460 TEST_F(TaskSchedulerImplTest, DelayedTasksNotRunAfterShutdown) { |
| 461 StartTaskScheduler(); |
| 462 // As with delayed tasks in general, this is racy. If the task does happen to |
| 463 // run after Shutdown within the timeout, it will fail this test. |
| 464 // |
| 465 // The timeout should be set sufficiently long enough to ensure that the |
| 466 // delayed task did not run. 2x is generally good enough. |
| 467 // |
| 468 // A non-racy way to do this would be to post two sequenced tasks: |
| 469 // 1) Regular Post Task: A WaitableEvent.Wait |
| 470 // 2) Delayed Task: ADD_FAILURE() |
| 471 // and signalling the WaitableEvent after Shutdown() on a different thread |
| 472 // since Shutdown() will block. However, the cost of managing this extra |
| 473 // thread was deemed to be too great for the unlikely race. |
| 474 scheduler_.PostDelayedTaskWithTraits(FROM_HERE, TaskTraits(), |
| 475 BindOnce([]() { ADD_FAILURE(); }), |
| 476 TestTimeouts::tiny_timeout()); |
| 477 scheduler_.Shutdown(); |
| 478 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 2); |
| 479 } |
| 480 |
| 481 #if defined(OS_POSIX) |
| 482 |
| 483 TEST_F(TaskSchedulerImplTest, FileDescriptorWatcherNoOpsAfterShutdown) { |
| 484 StartTaskScheduler(); |
| 485 |
| 486 int pipes[2]; |
| 487 ASSERT_EQ(0, pipe(pipes)); |
| 488 |
| 489 scoped_refptr<TaskRunner> blocking_task_runner = |
| 490 scheduler_.CreateSequencedTaskRunnerWithTraits( |
| 491 TaskTraits().WithShutdownBehavior( |
| 492 TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 493 blocking_task_runner->PostTask( |
| 494 FROM_HERE, |
| 495 BindOnce( |
| 496 [](int read_fd) { |
| 497 std::unique_ptr<FileDescriptorWatcher::Controller> controller = |
| 498 FileDescriptorWatcher::WatchReadable( |
| 499 read_fd, BindRepeating([]() { NOTREACHED(); })); |
| 500 |
| 501 // This test is for components that intentionally leak their |
| 502 // watchers at shutdown. We can't clean |controller| up because its |
| 503 // destructor will assert that it's being called from the correct |
| 504 // sequence. After the task scheduler is shutdown, it is not |
| 505 // possible to run tasks on this sequence. |
| 506 // |
| 507 // Note: Do not inline the controller.release() call into the |
| 508 // ANNOTATE_LEAKING_OBJECT_PTR as the annotation is removed |
| 509 // by the preprocessor in non-LEAK_SANITIZER builds, |
| 510 // effectively breaking this test. |
| 511 ANNOTATE_LEAKING_OBJECT_PTR(controller.get()); |
| 512 controller.release(); |
| 513 }, |
| 514 pipes[0])); |
| 515 |
| 516 scheduler_.Shutdown(); |
| 517 |
| 518 constexpr char kByte = '!'; |
| 519 ASSERT_TRUE(WriteFileDescriptor(pipes[1], &kByte, sizeof(kByte))); |
| 520 |
| 521 // Give a chance for the file watcher to fire before closing the handles. |
| 522 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 523 |
| 524 EXPECT_EQ(0, IGNORE_EINTR(close(pipes[0]))); |
| 525 EXPECT_EQ(0, IGNORE_EINTR(close(pipes[1]))); |
| 526 } |
| 527 #endif // defined(OS_POSIX) |
| 528 |
450 } // namespace internal | 529 } // namespace internal |
451 } // namespace base | 530 } // namespace base |
OLD | NEW |