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().WithWait().WithShutdownBehavior(GetParam()), TimeDelta()); | 267 TaskTraits().WithSyncPrimitives().WithShutdownBehavior(GetParam()), |
| 268 TimeDelta()); |
268 | 269 |
269 // Inform |task_tracker_| that |blocked_task| will be posted. | 270 // Inform |task_tracker_| that |blocked_task| will be posted. |
270 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); | 271 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
271 | 272 |
272 // Run the task asynchronouly. | 273 // Run the task asynchronouly. |
273 ThreadPostingAndRunningTask thread_running_task( | 274 ThreadPostingAndRunningTask thread_running_task( |
274 &tracker_, std::move(blocked_task), | 275 &tracker_, std::move(blocked_task), |
275 ThreadPostingAndRunningTask::Action::RUN, false); | 276 ThreadPostingAndRunningTask::Action::RUN, false); |
276 thread_running_task.Start(); | 277 thread_running_task.Start(); |
277 | 278 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 425 |
425 // Running the task should fail iff the task isn't allowed to use singletons. | 426 // Running the task should fail iff the task isn't allowed to use singletons. |
426 if (can_use_singletons) { | 427 if (can_use_singletons) { |
427 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); | 428 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
428 } else { | 429 } else { |
429 EXPECT_DCHECK_DEATH( | 430 EXPECT_DCHECK_DEATH( |
430 { tracker.RunTask(std::move(task), SequenceToken::Create()); }); | 431 { tracker.RunTask(std::move(task), SequenceToken::Create()); }); |
431 } | 432 } |
432 } | 433 } |
433 | 434 |
434 // Verify that AssertIOAllowed() succeeds only for a WithFileIO() task. | 435 // Verify that AssertIOAllowed() succeeds only for a MayBlock() task. |
435 TEST_P(TaskSchedulerTaskTrackerTest, IOAllowed) { | 436 TEST_P(TaskSchedulerTaskTrackerTest, IOAllowed) { |
436 TaskTracker tracker; | 437 TaskTracker tracker; |
437 | 438 |
438 // Unset the IO allowed bit. Expect TaskTracker to set it before running a | 439 // Unset the IO allowed bit. Expect TaskTracker to set it before running a |
439 // task with the WithFileIO() trait. | 440 // task with the MayBlock() trait. |
440 ThreadRestrictions::SetIOAllowed(false); | 441 ThreadRestrictions::SetIOAllowed(false); |
441 auto task_with_file_io = MakeUnique<Task>( | 442 auto task_with_may_block = MakeUnique<Task>( |
442 FROM_HERE, Bind([]() { | 443 FROM_HERE, Bind([]() { |
443 // Shouldn't fail. | 444 // Shouldn't fail. |
444 ThreadRestrictions::AssertIOAllowed(); | 445 ThreadRestrictions::AssertIOAllowed(); |
445 }), | 446 }), |
446 TaskTraits().WithFileIO().WithShutdownBehavior(GetParam()), TimeDelta()); | 447 TaskTraits().MayBlock().WithShutdownBehavior(GetParam()), TimeDelta()); |
447 EXPECT_TRUE(tracker.WillPostTask(task_with_file_io.get())); | 448 EXPECT_TRUE(tracker.WillPostTask(task_with_may_block.get())); |
448 tracker.RunTask(std::move(task_with_file_io), SequenceToken::Create()); | 449 tracker.RunTask(std::move(task_with_may_block), SequenceToken::Create()); |
449 | 450 |
450 // Set the IO allowed bit. Expect TaskTracker to unset it before running a | 451 // Set the IO allowed bit. Expect TaskTracker to unset it before running a |
451 // task without the WithFileIO() trait. | 452 // task without the MayBlock() trait. |
452 ThreadRestrictions::SetIOAllowed(true); | 453 ThreadRestrictions::SetIOAllowed(true); |
453 auto task_without_file_io = MakeUnique<Task>( | 454 auto task_without_may_block = MakeUnique<Task>( |
454 FROM_HERE, Bind([]() { | 455 FROM_HERE, Bind([]() { |
455 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); }); | 456 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); }); |
456 }), | 457 }), |
457 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 458 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
458 EXPECT_TRUE(tracker.WillPostTask(task_without_file_io.get())); | 459 EXPECT_TRUE(tracker.WillPostTask(task_without_may_block.get())); |
459 tracker.RunTask(std::move(task_without_file_io), SequenceToken::Create()); | 460 tracker.RunTask(std::move(task_without_may_block), SequenceToken::Create()); |
460 } | 461 } |
461 | 462 |
462 static void RunTaskRunnerHandleVerificationTask( | 463 static void RunTaskRunnerHandleVerificationTask( |
463 TaskTracker* tracker, | 464 TaskTracker* tracker, |
464 std::unique_ptr<Task> verify_task) { | 465 std::unique_ptr<Task> verify_task) { |
465 // Pretend |verify_task| is posted to respect TaskTracker's contract. | 466 // Pretend |verify_task| is posted to respect TaskTracker's contract. |
466 EXPECT_TRUE(tracker->WillPostTask(verify_task.get())); | 467 EXPECT_TRUE(tracker->WillPostTask(verify_task.get())); |
467 | 468 |
468 // Confirm that the test conditions are right (no TaskRunnerHandles set | 469 // Confirm that the test conditions are right (no TaskRunnerHandles set |
469 // already). | 470 // already). |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 837 |
837 class WaitAllowedTestThread : public SimpleThread { | 838 class WaitAllowedTestThread : public SimpleThread { |
838 public: | 839 public: |
839 WaitAllowedTestThread() : SimpleThread("WaitAllowedTestThread") {} | 840 WaitAllowedTestThread() : SimpleThread("WaitAllowedTestThread") {} |
840 | 841 |
841 private: | 842 private: |
842 void Run() override { | 843 void Run() override { |
843 TaskTracker tracker; | 844 TaskTracker tracker; |
844 | 845 |
845 // Waiting is allowed by default. Expect TaskTracker to disallow it before | 846 // Waiting is allowed by default. Expect TaskTracker to disallow it before |
846 // running a task without the WithWait() trait. | 847 // running a task without the WithSyncPrimitives() trait. |
847 ThreadRestrictions::AssertWaitAllowed(); | 848 ThreadRestrictions::AssertWaitAllowed(); |
848 auto task_without_wait = MakeUnique<Task>( | 849 auto task_without_sync_primitives = MakeUnique<Task>( |
849 FROM_HERE, Bind([]() { | 850 FROM_HERE, Bind([]() { |
850 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertWaitAllowed(); }); | 851 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertWaitAllowed(); }); |
851 }), | 852 }), |
852 TaskTraits(), TimeDelta()); | 853 TaskTraits(), TimeDelta()); |
853 EXPECT_TRUE(tracker.WillPostTask(task_without_wait.get())); | 854 EXPECT_TRUE(tracker.WillPostTask(task_without_sync_primitives.get())); |
854 tracker.RunTask(std::move(task_without_wait), SequenceToken::Create()); | 855 tracker.RunTask(std::move(task_without_sync_primitives), |
| 856 SequenceToken::Create()); |
855 | 857 |
856 // Disallow waiting. Expect TaskTracker to allow it before running a task | 858 // Disallow waiting. Expect TaskTracker to allow it before running a task |
857 // with the WithWait() trait. | 859 // with the WithSyncPrimitives() trait. |
858 ThreadRestrictions::DisallowWaiting(); | 860 ThreadRestrictions::DisallowWaiting(); |
859 auto task_with_wait = | 861 auto task_with_sync_primitives = |
860 MakeUnique<Task>(FROM_HERE, Bind([]() { | 862 MakeUnique<Task>(FROM_HERE, Bind([]() { |
861 // Shouldn't fail. | 863 // Shouldn't fail. |
862 ThreadRestrictions::AssertWaitAllowed(); | 864 ThreadRestrictions::AssertWaitAllowed(); |
863 }), | 865 }), |
864 TaskTraits().WithWait(), TimeDelta()); | 866 TaskTraits().WithSyncPrimitives(), TimeDelta()); |
865 EXPECT_TRUE(tracker.WillPostTask(task_with_wait.get())); | 867 EXPECT_TRUE(tracker.WillPostTask(task_with_sync_primitives.get())); |
866 tracker.RunTask(std::move(task_with_wait), SequenceToken::Create()); | 868 tracker.RunTask(std::move(task_with_sync_primitives), |
| 869 SequenceToken::Create()); |
867 } | 870 } |
868 | 871 |
869 DISALLOW_COPY_AND_ASSIGN(WaitAllowedTestThread); | 872 DISALLOW_COPY_AND_ASSIGN(WaitAllowedTestThread); |
870 }; | 873 }; |
871 | 874 |
872 } // namespace | 875 } // namespace |
873 | 876 |
874 // Verify that AssertIOAllowed() succeeds for a WithWait() task. | 877 // Verify that AssertIOAllowed() succeeds only for a WithSyncPrimitives() task. |
875 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { | 878 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { |
876 // Run the test on the separate thread since it is not possible to reset the | 879 // Run the test on the separate thread since it is not possible to reset the |
877 // "wait allowed" bit of a thread without being a friend of | 880 // "wait allowed" bit of a thread without being a friend of |
878 // ThreadRestrictions. | 881 // ThreadRestrictions. |
879 WaitAllowedTestThread wait_allowed_test_thread; | 882 WaitAllowedTestThread wait_allowed_test_thread; |
880 wait_allowed_test_thread.Start(); | 883 wait_allowed_test_thread.Start(); |
881 wait_allowed_test_thread.Join(); | 884 wait_allowed_test_thread.Join(); |
882 } | 885 } |
883 | 886 |
884 } // namespace internal | 887 } // namespace internal |
885 } // namespace base | 888 } // namespace base |
OLD | NEW |