Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/task_scheduler/utils.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/task_scheduler/priority_queue.h" | |
| 15 #include "base/task_scheduler/scheduler_task_executor.h" | |
| 16 #include "base/task_scheduler/sequence.h" | |
| 17 #include "base/task_scheduler/task.h" | |
| 18 #include "base/task_scheduler/task_tracker.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace base { | |
| 23 | |
| 24 bool operator==(const Closure& lhs, const Closure& rhs) { | |
| 25 return lhs.Equals(rhs); | |
| 26 } | |
| 27 | |
| 28 namespace internal { | |
| 29 namespace { | |
| 30 | |
| 31 class MockSchedulerTaskExecutor : public SchedulerTaskExecutor { | |
| 32 public: | |
| 33 void PostTaskWithSequence(std::unique_ptr<Task> task, | |
| 34 scoped_refptr<Sequence> sequence) override { | |
| 35 PostTaskWithSequenceMock(task->task, task->traits, sequence.get()); | |
| 36 } | |
| 37 | |
| 38 MOCK_METHOD3(PostTaskWithSequenceMock, | |
| 39 void(const Closure&, const TaskTraits&, Sequence* sequence)); | |
| 40 }; | |
| 41 | |
| 42 // Verifies that when PostTaskHelper receives a Task that is allowed to be | |
| 43 // posted, it forwards it to a SchedulerTaskExecutor. | |
| 44 TEST(TaskSchedulerPostTaskHelperTest, PostTaskAllowed) { | |
| 45 const Closure closure(Bind(&DoNothing)); | |
| 46 const TaskTraits traits; | |
| 47 scoped_refptr<Sequence> sequence(new Sequence); | |
| 48 testing::StrictMock<MockSchedulerTaskExecutor> executor; | |
| 49 TaskTracker task_tracker; | |
| 50 | |
| 51 EXPECT_CALL(executor, | |
| 52 PostTaskWithSequenceMock(closure, traits, sequence.get())); | |
| 53 PostTaskHelper(FROM_HERE, closure, traits, TimeDelta(), sequence, &executor, | |
| 54 &task_tracker); | |
| 55 } | |
| 56 | |
| 57 // Verifies that when PostTaskHelper receives a Task that isn't allowed to be | |
| 58 // posted, it doesn't forward it to a SchedulerTaskExecutor. | |
| 59 TEST(TaskSchedulerPostTaskHelperTest, PostTaskNotAllowed) { | |
| 60 testing::StrictMock<MockSchedulerTaskExecutor> executor; | |
| 61 TaskTracker task_tracker; | |
| 62 task_tracker.Shutdown(); | |
| 63 | |
| 64 PostTaskHelper( | |
| 65 FROM_HERE, Bind(&DoNothing), | |
| 66 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | |
| 67 TimeDelta(), make_scoped_refptr(new Sequence), &executor, &task_tracker); | |
| 68 } | |
|
gab
2016/04/14 19:34:16
Explicitly VerifyAndClear strict mock at the end?
fdoray
2016/04/14 23:46:59
Adding VerifyAndClear isn't useful since it's alre
| |
| 69 | |
| 70 // Verifies that when PostTaskWithSequenceHelper is called with an empty | |
| 71 // sequence, the task is added to the sequence and the sequence is added to the | |
| 72 // priority queue. | |
| 73 TEST(TaskSchedulerPostTaskWithSequenceHelperTest, PostTaskInEmptySequence) { | |
| 74 std::unique_ptr<Task> task( | |
| 75 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | |
| 76 const Task* task_raw = task.get(); | |
| 77 scoped_refptr<Sequence> sequence(new Sequence); | |
| 78 PriorityQueue priority_queue; | |
| 79 | |
| 80 // Post |task|. | |
| 81 EXPECT_TRUE( | |
| 82 PostTaskWithSequenceHelper(std::move(task), sequence, &priority_queue)); | |
| 83 | |
| 84 // Expect to find the sequence in the priority queue. | |
| 85 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); | |
| 86 | |
| 87 // Expect to find |task| alone in |sequence|. | |
| 88 EXPECT_EQ(task_raw, sequence->PeekTask()); | |
| 89 sequence->PopTask(); | |
| 90 EXPECT_EQ(nullptr, sequence->PeekTask()); | |
| 91 } | |
| 92 | |
| 93 // Verifies that when PostTaskWithSequenceHelper is called with a sequence that | |
| 94 // already contains a task, the task is added to the sequence but the sequence | |
| 95 // is not added to the priority queue. | |
| 96 TEST(TaskSchedulerPostTaskWithSequenceHelperTest, PostTaskInNonEmptySequence) { | |
| 97 std::unique_ptr<Task> task( | |
| 98 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | |
| 99 const Task* task_raw = task.get(); | |
| 100 scoped_refptr<Sequence> sequence(new Sequence); | |
| 101 PriorityQueue priority_queue; | |
| 102 | |
| 103 // Add an initial task in |sequence|. | |
| 104 sequence->PushTask( | |
| 105 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); | |
| 106 | |
| 107 // Post |task|. | |
| 108 EXPECT_FALSE( | |
| 109 PostTaskWithSequenceHelper(std::move(task), sequence, &priority_queue)); | |
| 110 | |
| 111 // Expect to find the priority queue empty. | |
| 112 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); | |
| 113 | |
| 114 // Expect to find |task| in |sequence| behind the initial task. | |
| 115 EXPECT_NE(task_raw, sequence->PeekTask()); | |
| 116 sequence->PopTask(); | |
| 117 EXPECT_EQ(task_raw, sequence->PeekTask()); | |
| 118 sequence->PopTask(); | |
| 119 EXPECT_EQ(nullptr, sequence->PeekTask()); | |
| 120 } | |
| 121 | |
| 122 } // namespace | |
| 123 } // namespace internal | |
| 124 } // namespace base | |
| OLD | NEW |