Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: base/task_scheduler/task_tracker_unittest.cc

Issue 2531663003: TaskScheduler: Add TaskTraits::WithWait(). (Closed)
Patch Set: CR robliao #9 (wording, order of ThreadRestrictions reset) Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task_scheduler/task_tracker.cc ('k') | base/task_scheduler/task_traits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 ThreadRestrictions::SetIOAllowed(true); 452 ThreadRestrictions::SetIOAllowed(true);
453 auto task_without_file_io = MakeUnique<Task>( 453 auto task_without_file_io = MakeUnique<Task>(
454 FROM_HERE, Bind([]() { 454 FROM_HERE, Bind([]() {
455 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); }); 455 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); });
456 }), 456 }),
457 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); 457 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta());
458 EXPECT_TRUE(tracker.WillPostTask(task_without_file_io.get())); 458 EXPECT_TRUE(tracker.WillPostTask(task_without_file_io.get()));
459 tracker.RunTask(std::move(task_without_file_io), SequenceToken::Create()); 459 tracker.RunTask(std::move(task_without_file_io), SequenceToken::Create());
460 } 460 }
461 461
462 namespace {
463
464 class WaitAllowedTestThread : public SimpleThread {
465 public:
466 WaitAllowedTestThread(TaskShutdownBehavior shutdown_behavior)
467 : SimpleThread("WaitAllowedTestThread"),
468 shutdown_behavior_(shutdown_behavior) {}
469
470 private:
471 void Run() override {
472 TaskTracker tracker;
473
474 // Waiting is allowed by default. Expect TaskTracker to disallow it before
475 // running a task without the WithWait() trait.
476 auto task_without_wait = MakeUnique<Task>(
477 FROM_HERE, Bind([]() {
478 EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertWaitAllowed(); });
479 }),
480 TaskTraits().WithShutdownBehavior(shutdown_behavior_), TimeDelta());
481 EXPECT_TRUE(tracker.WillPostTask(task_without_wait.get()));
482 tracker.RunTask(std::move(task_without_wait), SequenceToken::Create());
483
484 // Disallow waiting. Expect TaskTracker to allow it before running a task
485 // with the WithWait() trait.
486 ThreadRestrictions::DisallowWaiting();
487 auto task_with_wait = MakeUnique<Task>(
488 FROM_HERE, Bind([]() {
489 // Shouldn't fail.
490 ThreadRestrictions::AssertWaitAllowed();
491 }),
492 TaskTraits().WithWait().WithShutdownBehavior(shutdown_behavior_),
493 TimeDelta());
494 EXPECT_TRUE(tracker.WillPostTask(task_with_wait.get()));
495 tracker.RunTask(std::move(task_with_wait), SequenceToken::Create());
496 }
497
498 const TaskShutdownBehavior shutdown_behavior_;
danakj 2016/11/28 21:59:16 What does this have to do with what is being teste
fdoray 2016/11/28 22:44:27 Done.
499
500 DISALLOW_COPY_AND_ASSIGN(WaitAllowedTestThread);
501 };
502
503 } // namespace
504
505 // Verify that AssertIOAllowed() succeeds for a WithWait() task.
506 TEST_P(TaskSchedulerTaskTrackerTest, WaitAllowed) {
507 // Run the test on the separate thread since it is not possible to reset the
508 // "wait allowed" bit of a thread without being a friend of
509 // ThreadRestrictions.
510 WaitAllowedTestThread wait_allowed_test_thread(GetParam());
511 wait_allowed_test_thread.Start();
512 wait_allowed_test_thread.Join();
513 }
514
462 static void RunTaskRunnerHandleVerificationTask( 515 static void RunTaskRunnerHandleVerificationTask(
463 TaskTracker* tracker, 516 TaskTracker* tracker,
464 std::unique_ptr<Task> verify_task) { 517 std::unique_ptr<Task> verify_task) {
465 // Pretend |verify_task| is posted to respect TaskTracker's contract. 518 // Pretend |verify_task| is posted to respect TaskTracker's contract.
466 EXPECT_TRUE(tracker->WillPostTask(verify_task.get())); 519 EXPECT_TRUE(tracker->WillPostTask(verify_task.get()));
467 520
468 // Confirm that the test conditions are right (no TaskRunnerHandles set 521 // Confirm that the test conditions are right (no TaskRunnerHandles set
469 // already). 522 // already).
470 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); 523 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet());
471 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); 524 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet());
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
827 880
828 // Unblock shutdown by running |block_shutdown_task|. 881 // Unblock shutdown by running |block_shutdown_task|.
829 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), 882 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task),
830 SequenceToken::Create())); 883 SequenceToken::Create()));
831 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); 884 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted());
832 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); 885 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED();
833 } 886 }
834 887
835 } // namespace internal 888 } // namespace internal
836 } // namespace base 889 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/task_tracker.cc ('k') | base/task_scheduler/task_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698