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/scheduler_worker_thread_stack.h" | 5 #include "base/task_scheduler/scheduler_worker_thread_stack.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/task_scheduler/scheduler_worker_thread.h" | 10 #include "base/task_scheduler/scheduler_worker_thread.h" |
10 #include "base/task_scheduler/sequence.h" | 11 #include "base/task_scheduler/sequence.h" |
11 #include "base/task_scheduler/task_tracker.h" | 12 #include "base/task_scheduler/task_tracker.h" |
12 #include "base/task_scheduler/test_utils.h" | 13 #include "base/task_scheduler/test_utils.h" |
13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 namespace internal { | 18 namespace internal { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 class MockSchedulerWorkerThreadDelegate | 22 class MockSchedulerWorkerThreadDelegate |
22 : public SchedulerWorkerThread::Delegate { | 23 : public SchedulerWorkerThread::Delegate { |
23 public: | 24 public: |
24 void OnMainEntry() override {} | 25 void OnMainEntry() override {} |
25 scoped_refptr<Sequence> GetWork( | 26 scoped_refptr<Sequence> GetWork( |
26 SchedulerWorkerThread* worker_thread) override { | 27 SchedulerWorkerThread* worker_thread) override { |
27 return nullptr; | 28 return nullptr; |
28 } | 29 } |
29 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override { | 30 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override { |
30 NOTREACHED(); | 31 NOTREACHED(); |
31 } | 32 } |
32 }; | 33 }; |
33 | 34 |
34 class TaskSchedulerWorkerThreadStackTest : public testing::Test { | 35 class TaskSchedulerWorkerThreadStackTest : public testing::Test { |
35 protected: | 36 protected: |
36 void SetUp() override { | 37 void SetUp() override { |
37 thread_a_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( | 38 thread_a_ = SchedulerWorkerThread::Create( |
38 ThreadPriority::NORMAL, &delegate_, &task_tracker_); | 39 ThreadPriority::NORMAL, |
| 40 WrapUnique(new MockSchedulerWorkerThreadDelegate), &task_tracker_); |
39 ASSERT_TRUE(thread_a_); | 41 ASSERT_TRUE(thread_a_); |
40 thread_b_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( | 42 thread_b_ = SchedulerWorkerThread::Create( |
41 ThreadPriority::NORMAL, &delegate_, &task_tracker_); | 43 ThreadPriority::NORMAL, |
| 44 WrapUnique(new MockSchedulerWorkerThreadDelegate), &task_tracker_); |
42 ASSERT_TRUE(thread_b_); | 45 ASSERT_TRUE(thread_b_); |
43 thread_c_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( | 46 thread_c_ = SchedulerWorkerThread::Create( |
44 ThreadPriority::NORMAL, &delegate_, &task_tracker_); | 47 ThreadPriority::NORMAL, |
| 48 WrapUnique(new MockSchedulerWorkerThreadDelegate), &task_tracker_); |
45 ASSERT_TRUE(thread_c_); | 49 ASSERT_TRUE(thread_c_); |
46 } | 50 } |
47 | 51 |
48 void TearDown() override { | 52 void TearDown() override { |
49 thread_a_->JoinForTesting(); | 53 thread_a_->JoinForTesting(); |
50 thread_b_->JoinForTesting(); | 54 thread_b_->JoinForTesting(); |
51 thread_c_->JoinForTesting(); | 55 thread_c_->JoinForTesting(); |
52 } | 56 } |
53 | 57 |
54 std::unique_ptr<SchedulerWorkerThread> thread_a_; | 58 std::unique_ptr<SchedulerWorkerThread> thread_a_; |
55 std::unique_ptr<SchedulerWorkerThread> thread_b_; | 59 std::unique_ptr<SchedulerWorkerThread> thread_b_; |
56 std::unique_ptr<SchedulerWorkerThread> thread_c_; | 60 std::unique_ptr<SchedulerWorkerThread> thread_c_; |
57 | 61 |
58 private: | 62 private: |
59 MockSchedulerWorkerThreadDelegate delegate_; | |
60 TaskTracker task_tracker_; | 63 TaskTracker task_tracker_; |
61 }; | 64 }; |
62 | 65 |
63 } // namespace | 66 } // namespace |
64 | 67 |
65 // Verify that Push() and Pop() add/remove values in FIFO order. | 68 // Verify that Push() and Pop() add/remove values in FIFO order. |
66 TEST_F(TaskSchedulerWorkerThreadStackTest, PushPop) { | 69 TEST_F(TaskSchedulerWorkerThreadStackTest, PushPop) { |
67 SchedulerWorkerThreadStack stack; | 70 SchedulerWorkerThreadStack stack; |
68 EXPECT_TRUE(stack.IsEmpty()); | 71 EXPECT_TRUE(stack.IsEmpty()); |
69 EXPECT_EQ(0U, stack.Size()); | 72 EXPECT_EQ(0U, stack.Size()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 156 |
154 // Verify that Push() DCHECKs when a value is inserted twice. | 157 // Verify that Push() DCHECKs when a value is inserted twice. |
155 TEST_F(TaskSchedulerWorkerThreadStackTest, PushTwice) { | 158 TEST_F(TaskSchedulerWorkerThreadStackTest, PushTwice) { |
156 SchedulerWorkerThreadStack stack; | 159 SchedulerWorkerThreadStack stack; |
157 stack.Push(thread_a_.get()); | 160 stack.Push(thread_a_.get()); |
158 EXPECT_DCHECK_DEATH({ stack.Push(thread_a_.get()); }, ""); | 161 EXPECT_DCHECK_DEATH({ stack.Push(thread_a_.get()); }, ""); |
159 } | 162 } |
160 | 163 |
161 } // namespace internal | 164 } // namespace internal |
162 } // namespace base | 165 } // namespace base |
OLD | NEW |