Chromium Code Reviews| 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 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 825 // Shutdown() shouldn't return before |block_shutdown_task| is executed. | 825 // Shutdown() shouldn't return before |block_shutdown_task| is executed. |
| 826 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 826 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 827 | 827 |
| 828 // Unblock shutdown by running |block_shutdown_task|. | 828 // Unblock shutdown by running |block_shutdown_task|. |
| 829 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), | 829 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), |
| 830 SequenceToken::Create())); | 830 SequenceToken::Create())); |
| 831 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); | 831 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); |
| 832 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 832 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 833 } | 833 } |
| 834 | 834 |
| 835 namespace { | |
| 836 | |
| 837 class WaitAllowedTestThread : public SimpleThread { | |
| 838 public: | |
| 839 WaitAllowedTestThread() : SimpleThread("WaitAllowedTestThread") {} | |
| 840 | |
| 841 private: | |
| 842 void Run() override { | |
| 843 TaskTracker tracker; | |
| 844 | |
| 845 // Waiting is allowed by default. Expect TaskTracker to disallow it before | |
|
danakj
2016/11/28 23:01:30
Can you verify this by doing AssertWaitAllowed() t
fdoray
2016/11/29 15:55:14
Done.
| |
| 846 // running a task without the WithWait() trait. | |
| 847 auto task_without_wait = MakeUnique<Task>( | |
| 848 FROM_HERE, Bind([]() { | |
| 849 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertWaitAllowed(); }); | |
| 850 }), | |
| 851 TaskTraits(), TimeDelta()); | |
| 852 EXPECT_TRUE(tracker.WillPostTask(task_without_wait.get())); | |
| 853 tracker.RunTask(std::move(task_without_wait), SequenceToken::Create()); | |
| 854 | |
| 855 // Disallow waiting. Expect TaskTracker to allow it before running a task | |
| 856 // with the WithWait() trait. | |
| 857 ThreadRestrictions::DisallowWaiting(); | |
| 858 auto task_with_wait = | |
| 859 MakeUnique<Task>(FROM_HERE, Bind([]() { | |
| 860 // Shouldn't fail. | |
| 861 ThreadRestrictions::AssertWaitAllowed(); | |
| 862 }), | |
| 863 TaskTraits().WithWait(), TimeDelta()); | |
| 864 EXPECT_TRUE(tracker.WillPostTask(task_with_wait.get())); | |
| 865 tracker.RunTask(std::move(task_with_wait), SequenceToken::Create()); | |
| 866 } | |
| 867 | |
| 868 DISALLOW_COPY_AND_ASSIGN(WaitAllowedTestThread); | |
| 869 }; | |
| 870 | |
| 871 } // namespace | |
| 872 | |
| 873 // Verify that AssertIOAllowed() succeeds for a WithWait() task. | |
| 874 TEST_F(TaskSchedulerTaskTrackerTest, WaitAllowed) { | |
|
danakj
2016/11/28 23:01:30
Looks like this could just be a TEST(TaskTrackerTe
fdoray
2016/11/29 15:55:14
Done.
| |
| 875 // Run the test on the separate thread since it is not possible to reset the | |
| 876 // "wait allowed" bit of a thread without being a friend of | |
| 877 // ThreadRestrictions. | |
| 878 WaitAllowedTestThread wait_allowed_test_thread; | |
| 879 wait_allowed_test_thread.Start(); | |
| 880 wait_allowed_test_thread.Join(); | |
| 881 } | |
| 882 | |
| 835 } // namespace internal | 883 } // namespace internal |
| 836 } // namespace base | 884 } // namespace base |
| OLD | NEW |