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_service_thread.h" | 5 #include "base/task_scheduler/scheduler_service_thread.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/task_scheduler/delayed_task_manager.h" | 16 #include "base/task_scheduler/delayed_task_manager.h" |
17 #include "base/task_scheduler/scheduler_thread_pool_impl.h" | 17 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
18 #include "base/task_scheduler/sequence.h" | 18 #include "base/task_scheduler/sequence.h" |
19 #include "base/task_scheduler/task.h" | 19 #include "base/task_scheduler/task.h" |
20 #include "base/task_scheduler/task_tracker.h" | 20 #include "base/task_scheduler/task_tracker.h" |
21 #include "base/task_scheduler/task_traits.h" | 21 #include "base/task_scheduler/task_traits.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 namespace internal { | 26 namespace internal { |
27 namespace { | 27 namespace { |
(...skipping 18 matching lines...) Expand all Loading... |
46 // doesn't advance), some tests below may fail. | 46 // doesn't advance), some tests below may fail. |
47 // 3) Short waits sufficiently cover longer waits. Having tests run quickly is | 47 // 3) Short waits sufficiently cover longer waits. Having tests run quickly is |
48 // desirable. Since the tests can't change the behavior of timed waiting, the | 48 // desirable. Since the tests can't change the behavior of timed waiting, the |
49 // delay durations should be reasonably short on the order of hundreds of | 49 // delay durations should be reasonably short on the order of hundreds of |
50 // milliseconds. | 50 // milliseconds. |
51 class TaskSchedulerServiceThreadTest : public testing::Test { | 51 class TaskSchedulerServiceThreadTest : public testing::Test { |
52 protected: | 52 protected: |
53 TaskSchedulerServiceThreadTest() : delayed_task_manager_(Bind(&DoNothing)) {} | 53 TaskSchedulerServiceThreadTest() : delayed_task_manager_(Bind(&DoNothing)) {} |
54 | 54 |
55 void SetUp() override { | 55 void SetUp() override { |
56 scheduler_thread_pool_ = SchedulerThreadPoolImpl::Create( | 56 scheduler_worker_pool_ = SchedulerWorkerPoolImpl::Create( |
57 "TestThreadPoolForSchedulerThread", ThreadPriority::BACKGROUND, 1u, | 57 "TestWorkerPoolForSchedulerServiceThread", ThreadPriority::BACKGROUND, |
58 SchedulerThreadPoolImpl::IORestriction::DISALLOWED, | 58 1u, SchedulerWorkerPoolImpl::IORestriction::DISALLOWED, |
59 Bind(&ReEnqueueSequenceCallback), &task_tracker_, | 59 Bind(&ReEnqueueSequenceCallback), &task_tracker_, |
60 &delayed_task_manager_); | 60 &delayed_task_manager_); |
61 ASSERT_TRUE(scheduler_thread_pool_); | 61 ASSERT_TRUE(scheduler_worker_pool_); |
62 service_thread_ = SchedulerServiceThread::Create( | 62 service_thread_ = SchedulerServiceThread::Create( |
63 &task_tracker_, &delayed_task_manager_); | 63 &task_tracker_, &delayed_task_manager_); |
64 ASSERT_TRUE(service_thread_); | 64 ASSERT_TRUE(service_thread_); |
65 } | 65 } |
66 | 66 |
67 void TearDown() override { | 67 void TearDown() override { |
68 scheduler_thread_pool_->JoinForTesting(); | 68 scheduler_worker_pool_->JoinForTesting(); |
69 service_thread_->JoinForTesting(); | 69 service_thread_->JoinForTesting(); |
70 } | 70 } |
71 | 71 |
72 SchedulerServiceThread* service_thread() { | 72 SchedulerServiceThread* service_thread() { |
73 return service_thread_.get(); | 73 return service_thread_.get(); |
74 } | 74 } |
75 | 75 |
76 DelayedTaskManager& delayed_task_manager() { | 76 DelayedTaskManager& delayed_task_manager() { |
77 return delayed_task_manager_; | 77 return delayed_task_manager_; |
78 } | 78 } |
79 | 79 |
80 SchedulerThreadPoolImpl* thread_pool() { | 80 SchedulerWorkerPoolImpl* worker_pool() { |
81 return scheduler_thread_pool_.get(); | 81 return scheduler_worker_pool_.get(); |
82 } | 82 } |
83 | 83 |
84 private: | 84 private: |
85 static void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | 85 static void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
86 ADD_FAILURE() << "This test only expects one task per sequence."; | 86 ADD_FAILURE() << "This test only expects one task per sequence."; |
87 } | 87 } |
88 | 88 |
89 DelayedTaskManager delayed_task_manager_; | 89 DelayedTaskManager delayed_task_manager_; |
90 TaskTracker task_tracker_; | 90 TaskTracker task_tracker_; |
91 std::unique_ptr<SchedulerThreadPoolImpl> scheduler_thread_pool_; | 91 std::unique_ptr<SchedulerWorkerPoolImpl> scheduler_worker_pool_; |
92 std::unique_ptr<SchedulerServiceThread> service_thread_; | 92 std::unique_ptr<SchedulerServiceThread> service_thread_; |
93 | 93 |
94 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerServiceThreadTest); | 94 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerServiceThreadTest); |
95 }; | 95 }; |
96 | 96 |
97 } // namespace | 97 } // namespace |
98 | 98 |
99 // Tests that the service thread can handle a single delayed task. | 99 // Tests that the service thread can handle a single delayed task. |
100 TEST_F(TaskSchedulerServiceThreadTest, RunSingleDelayedTask) { | 100 TEST_F(TaskSchedulerServiceThreadTest, RunSingleDelayedTask) { |
101 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 101 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
102 WaitableEvent::InitialState::NOT_SIGNALED); | 102 WaitableEvent::InitialState::NOT_SIGNALED); |
103 delayed_task_manager().AddDelayedTask( | 103 delayed_task_manager().AddDelayedTask( |
104 WrapUnique(new Task(FROM_HERE, | 104 WrapUnique(new Task(FROM_HERE, |
105 Bind(&WaitableEvent::Signal, Unretained(&event)), | 105 Bind(&WaitableEvent::Signal, Unretained(&event)), |
106 TaskTraits(), TimeDelta::FromMilliseconds(100))), | 106 TaskTraits(), TimeDelta::FromMilliseconds(100))), |
107 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 107 make_scoped_refptr(new Sequence), nullptr, worker_pool()); |
108 // Waking the service thread shouldn't cause the task to be executed per its | 108 // Waking the service thread shouldn't cause the task to be executed per its |
109 // delay not having expired (racy in theory, see test-fixture meta-comment). | 109 // delay not having expired (racy in theory, see test-fixture meta-comment). |
110 service_thread()->WakeUp(); | 110 service_thread()->WakeUp(); |
111 // Yield to increase the likelihood of catching a bug where these tasks would | 111 // Yield to increase the likelihood of catching a bug where these tasks would |
112 // be released before their delay is passed. | 112 // be released before their delay is passed. |
113 PlatformThread::YieldCurrentThread(); | 113 PlatformThread::YieldCurrentThread(); |
114 EXPECT_FALSE(event.IsSignaled()); | 114 EXPECT_FALSE(event.IsSignaled()); |
115 // When the delay expires, the delayed task is posted, signaling |event|. | 115 // When the delay expires, the delayed task is posted, signaling |event|. |
116 event.Wait(); | 116 event.Wait(); |
117 } | 117 } |
118 | 118 |
119 // Tests that the service thread can handle more than one delayed task with | 119 // Tests that the service thread can handle more than one delayed task with |
120 // different delays. | 120 // different delays. |
121 TEST_F(TaskSchedulerServiceThreadTest, RunMultipleDelayedTasks) { | 121 TEST_F(TaskSchedulerServiceThreadTest, RunMultipleDelayedTasks) { |
122 const TimeTicks test_begin_time = TimeTicks::Now(); | 122 const TimeTicks test_begin_time = TimeTicks::Now(); |
123 const TimeDelta delay1 = TimeDelta::FromMilliseconds(100); | 123 const TimeDelta delay1 = TimeDelta::FromMilliseconds(100); |
124 const TimeDelta delay2 = TimeDelta::FromMilliseconds(200); | 124 const TimeDelta delay2 = TimeDelta::FromMilliseconds(200); |
125 | 125 |
126 WaitableEvent event1(WaitableEvent::ResetPolicy::MANUAL, | 126 WaitableEvent event1(WaitableEvent::ResetPolicy::MANUAL, |
127 WaitableEvent::InitialState::NOT_SIGNALED); | 127 WaitableEvent::InitialState::NOT_SIGNALED); |
128 delayed_task_manager().AddDelayedTask( | 128 delayed_task_manager().AddDelayedTask( |
129 WrapUnique(new Task(FROM_HERE, | 129 WrapUnique(new Task(FROM_HERE, |
130 Bind(&WaitableEvent::Signal, Unretained(&event1)), | 130 Bind(&WaitableEvent::Signal, Unretained(&event1)), |
131 TaskTraits(), delay1)), | 131 TaskTraits(), delay1)), |
132 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 132 make_scoped_refptr(new Sequence), nullptr, worker_pool()); |
133 | 133 |
134 WaitableEvent event2(WaitableEvent::ResetPolicy::MANUAL, | 134 WaitableEvent event2(WaitableEvent::ResetPolicy::MANUAL, |
135 WaitableEvent::InitialState::NOT_SIGNALED); | 135 WaitableEvent::InitialState::NOT_SIGNALED); |
136 delayed_task_manager().AddDelayedTask( | 136 delayed_task_manager().AddDelayedTask( |
137 WrapUnique(new Task(FROM_HERE, | 137 WrapUnique(new Task(FROM_HERE, |
138 Bind(&WaitableEvent::Signal, Unretained(&event2)), | 138 Bind(&WaitableEvent::Signal, Unretained(&event2)), |
139 TaskTraits(), delay2)), | 139 TaskTraits(), delay2)), |
140 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 140 make_scoped_refptr(new Sequence), nullptr, worker_pool()); |
141 | 141 |
142 // Adding the task shouldn't have caused them to be executed. | 142 // Adding the task shouldn't have caused them to be executed. |
143 EXPECT_FALSE(event1.IsSignaled()); | 143 EXPECT_FALSE(event1.IsSignaled()); |
144 EXPECT_FALSE(event2.IsSignaled()); | 144 EXPECT_FALSE(event2.IsSignaled()); |
145 | 145 |
146 // Waking the service thread shouldn't cause the tasks to be executed per | 146 // Waking the service thread shouldn't cause the tasks to be executed per |
147 // their delays not having expired (note: this is racy if the delay somehow | 147 // their delays not having expired (note: this is racy if the delay somehow |
148 // expires before this runs but 100ms is a long time in a unittest...). It | 148 // expires before this runs but 100ms is a long time in a unittest...). It |
149 // should instead cause the service thread to schedule itself for wakeup when | 149 // should instead cause the service thread to schedule itself for wakeup when |
150 // |delay1| expires. | 150 // |delay1| expires. |
(...skipping 23 matching lines...) Expand all Loading... |
174 | 174 |
175 // Now wait for the second task to be fired. | 175 // Now wait for the second task to be fired. |
176 event2.Wait(); | 176 event2.Wait(); |
177 | 177 |
178 // Which should only have fired after |delay2| was expired. | 178 // Which should only have fired after |delay2| was expired. |
179 EXPECT_GE(TimeTicks::Now() - test_begin_time, delay2); | 179 EXPECT_GE(TimeTicks::Now() - test_begin_time, delay2); |
180 } | 180 } |
181 | 181 |
182 } // namespace internal | 182 } // namespace internal |
183 } // namespace base | 183 } // namespace base |
OLD | NEW |