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_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 // Run the task. | 256 // Run the task. |
257 EXPECT_EQ(0U, NumTasksExecuted()); | 257 EXPECT_EQ(0U, NumTasksExecuted()); |
258 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); | 258 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); |
259 EXPECT_EQ(1U, NumTasksExecuted()); | 259 EXPECT_EQ(1U, NumTasksExecuted()); |
260 | 260 |
261 // Shutdown() shouldn't block. | 261 // Shutdown() shouldn't block. |
262 tracker_.Shutdown(); | 262 tracker_.Shutdown(); |
263 } | 263 } |
264 | 264 |
265 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { | 265 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { |
266 // Create a task that will block until |event| is signaled. | 266 // Create a task that signals |task_running| and blocks until |task_barrier| |
267 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, | 267 // is signaled. |
268 WaitableEvent::InitialState::NOT_SIGNALED); | 268 WaitableEvent task_running(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 269 WaitableEvent::InitialState::NOT_SIGNALED); |
| 270 WaitableEvent task_barrier(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 271 WaitableEvent::InitialState::NOT_SIGNALED); |
269 auto blocked_task = base::MakeUnique<Task>( | 272 auto blocked_task = base::MakeUnique<Task>( |
270 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), | 273 FROM_HERE, |
| 274 Bind( |
| 275 [](WaitableEvent* task_running, WaitableEvent* task_barrier) { |
| 276 task_running->Signal(); |
| 277 task_barrier->Wait(); |
| 278 }, |
| 279 Unretained(&task_running), base::Unretained(&task_barrier)), |
271 TaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior(GetParam()), | 280 TaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior(GetParam()), |
272 TimeDelta()); | 281 TimeDelta()); |
273 | 282 |
274 // Inform |task_tracker_| that |blocked_task| will be posted. | 283 // Inform |task_tracker_| that |blocked_task| will be posted. |
275 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); | 284 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
276 | 285 |
277 // Run the task asynchronouly. | 286 // Create a thread to run the task. Wait until the task starts running. |
278 ThreadPostingAndRunningTask thread_running_task( | 287 ThreadPostingAndRunningTask thread_running_task( |
279 &tracker_, std::move(blocked_task), | 288 &tracker_, std::move(blocked_task), |
280 ThreadPostingAndRunningTask::Action::RUN, false); | 289 ThreadPostingAndRunningTask::Action::RUN, false); |
281 thread_running_task.Start(); | 290 thread_running_task.Start(); |
| 291 task_running.Wait(); |
282 | 292 |
283 // Initiate shutdown while the task is running. | 293 // Initiate shutdown after the task has been scheduled. |
284 CallShutdownAsync(); | 294 CallShutdownAsync(); |
285 | 295 |
286 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { | 296 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { |
287 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. | 297 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. |
288 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 298 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
289 } else { | 299 } else { |
290 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. | 300 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. |
291 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 301 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
292 } | 302 } |
293 | 303 |
294 // Unblock the task. | 304 // Unblock the task. |
295 event.Signal(); | 305 task_barrier.Signal(); |
296 thread_running_task.Join(); | 306 thread_running_task.Join(); |
297 | 307 |
298 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. | 308 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. |
299 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 309 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
300 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 310 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
301 } | 311 } |
302 | 312 |
303 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { | 313 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { |
304 // Inform |task_tracker_| that a task will be posted. | 314 // Inform |task_tracker_| that a task will be posted. |
305 std::unique_ptr<Task> task(CreateTask(GetParam())); | 315 std::unique_ptr<Task> task(CreateTask(GetParam())); |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 ASSERT_TRUE(tracker.WillPostTask(task.get())); | 941 ASSERT_TRUE(tracker.WillPostTask(task.get())); |
932 | 942 |
933 HistogramTester tester; | 943 HistogramTester tester; |
934 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); | 944 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
935 tester.ExpectTotalCount(test.expected_histogram, 1); | 945 tester.ExpectTotalCount(test.expected_histogram, 1); |
936 } | 946 } |
937 } | 947 } |
938 | 948 |
939 } // namespace internal | 949 } // namespace internal |
940 } // namespace base | 950 } // namespace base |
OLD | NEW |