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 PostTaskToExecutor receives a Task that is allowed to be |
| 43 // posted, it forwards it to a SchedulerTaskExecutor. |
| 44 TEST(TaskSchedulerPostTaskToExecutorTest, 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 PostTaskToExecutor(FROM_HERE, closure, traits, TimeDelta(), sequence, |
| 54 &executor, &task_tracker); |
| 55 } |
| 56 |
| 57 // Verifies that when PostTaskToExecutor receives a Task that isn't allowed to |
| 58 // be posted, it doesn't forward it to a SchedulerTaskExecutor. |
| 59 TEST(TaskSchedulerPostTaskToExecutorTest, PostTaskNotAllowed) { |
| 60 // Use a strict mock to ensure that the test fails when there is an unexpected |
| 61 // call to the mock method of |executor|. |
| 62 testing::StrictMock<MockSchedulerTaskExecutor> executor; |
| 63 TaskTracker task_tracker; |
| 64 task_tracker.Shutdown(); |
| 65 |
| 66 PostTaskToExecutor( |
| 67 FROM_HERE, Bind(&DoNothing), |
| 68 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 69 TimeDelta(), make_scoped_refptr(new Sequence), &executor, &task_tracker); |
| 70 } |
| 71 |
| 72 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with an empty |
| 73 // sequence, the task is added to the sequence and the sequence is added to the |
| 74 // priority queue. |
| 75 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, |
| 76 PostTaskInEmptySequence) { |
| 77 std::unique_ptr<Task> task( |
| 78 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 79 const Task* task_raw = task.get(); |
| 80 scoped_refptr<Sequence> sequence(new Sequence); |
| 81 PriorityQueue priority_queue; |
| 82 |
| 83 // Post |task|. |
| 84 EXPECT_TRUE(AddTaskToSequenceAndPriorityQueue(std::move(task), sequence, |
| 85 &priority_queue)); |
| 86 |
| 87 // Expect to find the sequence in the priority queue. |
| 88 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); |
| 89 |
| 90 // Expect to find |task| alone in |sequence|. |
| 91 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 92 sequence->PopTask(); |
| 93 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 94 } |
| 95 |
| 96 // Verifies that when AddTaskToSequenceAndPriorityQueue is called with a |
| 97 // sequence that already contains a task, the task is added to the sequence but |
| 98 // the sequence is not added to the priority queue. |
| 99 TEST(TaskSchedulerAddTaskToSequenceAndPriorityQueueTest, |
| 100 PostTaskInNonEmptySequence) { |
| 101 std::unique_ptr<Task> task( |
| 102 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 103 const Task* task_raw = task.get(); |
| 104 scoped_refptr<Sequence> sequence(new Sequence); |
| 105 PriorityQueue priority_queue; |
| 106 |
| 107 // Add an initial task in |sequence|. |
| 108 sequence->PushTask( |
| 109 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); |
| 110 |
| 111 // Post |task|. |
| 112 EXPECT_FALSE(AddTaskToSequenceAndPriorityQueue(std::move(task), sequence, |
| 113 &priority_queue)); |
| 114 |
| 115 // Expect to find the priority queue empty. |
| 116 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); |
| 117 |
| 118 // Expect to find |task| in |sequence| behind the initial task. |
| 119 EXPECT_NE(task_raw, sequence->PeekTask()); |
| 120 sequence->PopTask(); |
| 121 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 122 sequence->PopTask(); |
| 123 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 124 } |
| 125 |
| 126 } // namespace |
| 127 } // namespace internal |
| 128 } // namespace base |
OLD | NEW |