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" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 std::unique_ptr<SchedulerThreadPoolImpl> scheduler_thread_pool_; | 91 std::unique_ptr<SchedulerThreadPoolImpl> scheduler_thread_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(true, false); | 101 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 102 WaitableEvent::InitialState::NOT_SIGNALED); |
102 delayed_task_manager().AddDelayedTask( | 103 delayed_task_manager().AddDelayedTask( |
103 WrapUnique(new Task(FROM_HERE, | 104 WrapUnique(new Task(FROM_HERE, |
104 Bind(&WaitableEvent::Signal, Unretained(&event)), | 105 Bind(&WaitableEvent::Signal, Unretained(&event)), |
105 TaskTraits(), TimeDelta::FromMilliseconds(100))), | 106 TaskTraits(), TimeDelta::FromMilliseconds(100))), |
106 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 107 make_scoped_refptr(new Sequence), nullptr, thread_pool()); |
107 // 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 |
108 // 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). |
109 service_thread()->WakeUp(); | 110 service_thread()->WakeUp(); |
110 // 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 |
111 // be released before their delay is passed. | 112 // be released before their delay is passed. |
112 PlatformThread::YieldCurrentThread(); | 113 PlatformThread::YieldCurrentThread(); |
113 EXPECT_FALSE(event.IsSignaled()); | 114 EXPECT_FALSE(event.IsSignaled()); |
114 // When the delay expires, the delayed task is posted, signaling |event|. | 115 // When the delay expires, the delayed task is posted, signaling |event|. |
115 event.Wait(); | 116 event.Wait(); |
116 } | 117 } |
117 | 118 |
118 // 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 |
119 // different delays. | 120 // different delays. |
120 TEST_F(TaskSchedulerServiceThreadTest, RunMultipleDelayedTasks) { | 121 TEST_F(TaskSchedulerServiceThreadTest, RunMultipleDelayedTasks) { |
121 const TimeTicks test_begin_time = TimeTicks::Now(); | 122 const TimeTicks test_begin_time = TimeTicks::Now(); |
122 const TimeDelta delay1 = TimeDelta::FromMilliseconds(100); | 123 const TimeDelta delay1 = TimeDelta::FromMilliseconds(100); |
123 const TimeDelta delay2 = TimeDelta::FromMilliseconds(200); | 124 const TimeDelta delay2 = TimeDelta::FromMilliseconds(200); |
124 | 125 |
125 WaitableEvent event1(true, false); | 126 WaitableEvent event1(WaitableEvent::ResetPolicy::MANUAL, |
| 127 WaitableEvent::InitialState::NOT_SIGNALED); |
126 delayed_task_manager().AddDelayedTask( | 128 delayed_task_manager().AddDelayedTask( |
127 WrapUnique(new Task(FROM_HERE, | 129 WrapUnique(new Task(FROM_HERE, |
128 Bind(&WaitableEvent::Signal, Unretained(&event1)), | 130 Bind(&WaitableEvent::Signal, Unretained(&event1)), |
129 TaskTraits(), delay1)), | 131 TaskTraits(), delay1)), |
130 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 132 make_scoped_refptr(new Sequence), nullptr, thread_pool()); |
131 | 133 |
132 WaitableEvent event2(true, false); | 134 WaitableEvent event2(WaitableEvent::ResetPolicy::MANUAL, |
| 135 WaitableEvent::InitialState::NOT_SIGNALED); |
133 delayed_task_manager().AddDelayedTask( | 136 delayed_task_manager().AddDelayedTask( |
134 WrapUnique(new Task(FROM_HERE, | 137 WrapUnique(new Task(FROM_HERE, |
135 Bind(&WaitableEvent::Signal, Unretained(&event2)), | 138 Bind(&WaitableEvent::Signal, Unretained(&event2)), |
136 TaskTraits(), delay2)), | 139 TaskTraits(), delay2)), |
137 make_scoped_refptr(new Sequence), nullptr, thread_pool()); | 140 make_scoped_refptr(new Sequence), nullptr, thread_pool()); |
138 | 141 |
139 // Adding the task shouldn't have caused them to be executed. | 142 // Adding the task shouldn't have caused them to be executed. |
140 EXPECT_FALSE(event1.IsSignaled()); | 143 EXPECT_FALSE(event1.IsSignaled()); |
141 EXPECT_FALSE(event2.IsSignaled()); | 144 EXPECT_FALSE(event2.IsSignaled()); |
142 | 145 |
(...skipping 28 matching lines...) Expand all Loading... |
171 | 174 |
172 // Now wait for the second task to be fired. | 175 // Now wait for the second task to be fired. |
173 event2.Wait(); | 176 event2.Wait(); |
174 | 177 |
175 // Which should only have fired after |delay2| was expired. | 178 // Which should only have fired after |delay2| was expired. |
176 EXPECT_GE(TimeTicks::Now() - test_begin_time, delay2); | 179 EXPECT_GE(TimeTicks::Now() - test_begin_time, delay2); |
177 } | 180 } |
178 | 181 |
179 } // namespace internal | 182 } // namespace internal |
180 } // namespace base | 183 } // namespace base |
OLD | NEW |