| 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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // Shutdown() shouldn't block. | 257 // Shutdown() shouldn't block. |
| 258 tracker_.Shutdown(); | 258 tracker_.Shutdown(); |
| 259 } | 259 } |
| 260 | 260 |
| 261 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { | 261 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { |
| 262 // Create a task that will block until |event| is signaled. | 262 // Create a task that will block until |event| is signaled. |
| 263 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, | 263 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 264 WaitableEvent::InitialState::NOT_SIGNALED); | 264 WaitableEvent::InitialState::NOT_SIGNALED); |
| 265 auto blocked_task = base::MakeUnique<Task>( | 265 auto blocked_task = base::MakeUnique<Task>( |
| 266 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), | 266 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), |
| 267 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 267 TaskTraits().WithWait().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 268 | 268 |
| 269 // Inform |task_tracker_| that |blocked_task| will be posted. | 269 // Inform |task_tracker_| that |blocked_task| will be posted. |
| 270 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); | 270 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
| 271 | 271 |
| 272 // Run the task asynchronouly. | 272 // Run the task asynchronouly. |
| 273 ThreadPostingAndRunningTask thread_running_task( | 273 ThreadPostingAndRunningTask thread_running_task( |
| 274 &tracker_, std::move(blocked_task), | 274 &tracker_, std::move(blocked_task), |
| 275 ThreadPostingAndRunningTask::Action::RUN, false); | 275 ThreadPostingAndRunningTask::Action::RUN, false); |
| 276 thread_running_task.Start(); | 276 thread_running_task.Start(); |
| 277 | 277 |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 // Shutdown() shouldn't return before |block_shutdown_task| is executed. | 797 // Shutdown() shouldn't return before |block_shutdown_task| is executed. |
| 798 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 798 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 799 | 799 |
| 800 // Unblock shutdown by running |block_shutdown_task|. | 800 // Unblock shutdown by running |block_shutdown_task|. |
| 801 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), | 801 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), |
| 802 SequenceToken::Create())); | 802 SequenceToken::Create())); |
| 803 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); | 803 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); |
| 804 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 804 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 805 } | 805 } |
| 806 | 806 |
| 807 namespace { |
| 808 |
| 809 class WaitAllowedTestThread : public SimpleThread { |
| 810 public: |
| 811 WaitAllowedTestThread() : SimpleThread("WaitAllowedTestThread") {} |
| 812 |
| 813 private: |
| 814 void Run() override { |
| 815 TaskTracker tracker; |
| 816 |
| 817 // Waiting is allowed by default. Expect TaskTracker to disallow it before |
| 818 // running a task without the WithWait() trait. |
| 819 ThreadRestrictions::AssertWaitAllowed(); |
| 820 auto task_without_wait = MakeUnique<Task>( |
| 821 FROM_HERE, Bind([]() { |
| 822 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertWaitAllowed(); }); |
| 823 }), |
| 824 TaskTraits(), TimeDelta()); |
| 825 EXPECT_TRUE(tracker.WillPostTask(task_without_wait.get())); |
| 826 tracker.RunTask(std::move(task_without_wait), SequenceToken::Create()); |
| 827 |
| 828 // Disallow waiting. Expect TaskTracker to allow it before running a task |
| 829 // with the WithWait() trait. |
| 830 ThreadRestrictions::DisallowWaiting(); |
| 831 auto task_with_wait = |
| 832 MakeUnique<Task>(FROM_HERE, Bind([]() { |
| 833 // Shouldn't fail. |
| 834 ThreadRestrictions::AssertWaitAllowed(); |
| 835 }), |
| 836 TaskTraits().WithWait(), TimeDelta()); |
| 837 EXPECT_TRUE(tracker.WillPostTask(task_with_wait.get())); |
| 838 tracker.RunTask(std::move(task_with_wait), SequenceToken::Create()); |
| 839 } |
| 840 |
| 841 DISALLOW_COPY_AND_ASSIGN(WaitAllowedTestThread); |
| 842 }; |
| 843 |
| 844 } // namespace |
| 845 |
| 846 // Verify that AssertIOAllowed() succeeds for a WithWait() task. |
| 847 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { |
| 848 // Run the test on the separate thread since it is not possible to reset the |
| 849 // "wait allowed" bit of a thread without being a friend of |
| 850 // ThreadRestrictions. |
| 851 WaitAllowedTestThread wait_allowed_test_thread; |
| 852 wait_allowed_test_thread.Start(); |
| 853 wait_allowed_test_thread.Join(); |
| 854 } |
| 855 |
| 807 } // namespace internal | 856 } // namespace internal |
| 808 } // namespace base | 857 } // namespace base |
| OLD | NEW |