OLD | NEW |
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/threading/sequenced_worker_pool.h" | 5 #include "base/threading/sequenced_worker_pool.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
22 #include "base/time.h" | 22 #include "base/time.h" |
23 #include "base/tracked_objects.h" | 23 #include "base/tracked_objects.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 | 27 |
28 // IMPORTANT NOTE: | 28 // IMPORTANT NOTE: |
29 // | 29 // |
30 // Many of these tests have failure modes where they'll hang forever. These | 30 // Many of these tests have failure modes where they'll hang forever. These |
31 // tests should not be flaky, and hangling indicates a type of failure. Do not | 31 // tests should not be flaky, and hanging indicates a type of failure. Do not |
32 // mark as flaky if they're hanging, it's likely an actual bug. | 32 // mark as flaky if they're hanging, it's likely an actual bug. |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 const size_t kNumWorkerThreads = 3; | 36 const size_t kNumWorkerThreads = 3; |
37 | 37 |
38 // Allows a number of threads to all be blocked on the same event, and | 38 // Allows a number of threads to all be blocked on the same event, and |
39 // provides a way to unblock a certain number of them. | 39 // provides a way to unblock a certain number of them. |
40 class ThreadBlocker { | 40 class ThreadBlocker { |
41 public: | 41 public: |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 for (size_t i = 0; i < kNumFastTasks; i++) { | 774 for (size_t i = 0; i < kNumFastTasks; i++) { |
775 pool()->PostWorkerTask(FROM_HERE, | 775 pool()->PostWorkerTask(FROM_HERE, |
776 base::Bind(&TestTracker::FastTask, tracker(), 0)); | 776 base::Bind(&TestTracker::FastTask, tracker(), 0)); |
777 } | 777 } |
778 pool()->PostWorkerTask( | 778 pool()->PostWorkerTask( |
779 FROM_HERE, | 779 FROM_HERE, |
780 base::Bind(&TestTracker::PostAdditionalTasks, tracker(), 0, pool(), | 780 base::Bind(&TestTracker::PostAdditionalTasks, tracker(), 0, pool(), |
781 true)); | 781 true)); |
782 | 782 |
783 // We expect all except the delayed task to have been run. We verify all | 783 // We expect all except the delayed task to have been run. We verify all |
784 // closures have been deleted deleted by looking at the refcount of the | 784 // closures have been deleted by looking at the refcount of the |
785 // tracker. | 785 // tracker. |
786 EXPECT_FALSE(tracker()->HasOneRef()); | 786 EXPECT_FALSE(tracker()->HasOneRef()); |
787 pool()->FlushForTesting(); | 787 pool()->FlushForTesting(); |
788 EXPECT_TRUE(tracker()->HasOneRef()); | 788 EXPECT_TRUE(tracker()->HasOneRef()); |
789 EXPECT_EQ(1 + kNumFastTasks + 1 + 3, tracker()->GetTasksCompletedCount()); | 789 EXPECT_EQ(1 + kNumFastTasks + 1 + 3, tracker()->GetTasksCompletedCount()); |
790 | 790 |
791 // Should be fine to call on an idle instance with all threads created, and | 791 // Should be fine to call on an idle instance with all threads created, and |
792 // spamming the method shouldn't deadlock or confuse the class. | 792 // spamming the method shouldn't deadlock or confuse the class. |
793 pool()->FlushForTesting(); | 793 pool()->FlushForTesting(); |
794 pool()->FlushForTesting(); | 794 pool()->FlushForTesting(); |
795 | 795 |
796 // Should be fine to call after shutdown too. | 796 // Should be fine to call after shutdown too. |
797 pool()->Shutdown(); | 797 pool()->Shutdown(); |
798 pool()->FlushForTesting(); | 798 pool()->FlushForTesting(); |
799 } | 799 } |
800 | 800 |
| 801 TEST(SequencedWorkerPoolRefPtrTest, ShutsDownCleanWithContinueOnShutdown) { |
| 802 MessageLoop loop; |
| 803 scoped_refptr<SequencedWorkerPool> pool(new SequencedWorkerPool(3, "Pool")); |
| 804 scoped_refptr<SequencedTaskRunner> task_runner = |
| 805 pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 806 pool->GetSequenceToken(), |
| 807 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 808 |
| 809 // Upon test exit, should shut down without hanging. |
| 810 pool->Shutdown(); |
| 811 } |
| 812 |
801 class SequencedWorkerPoolTaskRunnerTestDelegate { | 813 class SequencedWorkerPoolTaskRunnerTestDelegate { |
802 public: | 814 public: |
803 SequencedWorkerPoolTaskRunnerTestDelegate() {} | 815 SequencedWorkerPoolTaskRunnerTestDelegate() {} |
804 | 816 |
805 ~SequencedWorkerPoolTaskRunnerTestDelegate() {} | 817 ~SequencedWorkerPoolTaskRunnerTestDelegate() {} |
806 | 818 |
807 void StartTaskRunner() { | 819 void StartTaskRunner() { |
808 pool_owner_.reset( | 820 pool_owner_.reset( |
809 new SequencedWorkerPoolOwner(10, "SequencedWorkerPoolTaskRunnerTest")); | 821 new SequencedWorkerPoolOwner(10, "SequencedWorkerPoolTaskRunnerTest")); |
810 } | 822 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 SequencedWorkerPoolSequencedTaskRunner, TaskRunnerTest, | 929 SequencedWorkerPoolSequencedTaskRunner, TaskRunnerTest, |
918 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); | 930 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); |
919 | 931 |
920 INSTANTIATE_TYPED_TEST_CASE_P( | 932 INSTANTIATE_TYPED_TEST_CASE_P( |
921 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest, | 933 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest, |
922 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); | 934 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); |
923 | 935 |
924 } // namespace | 936 } // namespace |
925 | 937 |
926 } // namespace base | 938 } // namespace base |
OLD | NEW |