| 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/utils.h" | 5 #include "base/task_scheduler/utils.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/task_scheduler/priority_queue.h" | 13 #include "base/task_scheduler/priority_queue.h" |
| 14 #include "base/task_scheduler/sequence.h" | 14 #include "base/task_scheduler/sequence.h" |
| 15 #include "base/task_scheduler/task.h" | 15 #include "base/task_scheduler/task.h" |
| 16 #include "base/task_scheduler/task_tracker.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 namespace internal { | 19 namespace internal { |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 // Checks that when PostTaskHelper is called with an empty sequence, the task | 22 // Checks that when PostTaskNowHelper is called with an empty sequence, the task |
| 24 // is added to the sequence and the sequence is added to the priority queue. | 23 // is added to the sequence and the sequence is added to the priority queue. |
| 25 TEST(TaskSchedulerPostTaskHelperTest, PostTaskInEmptySequence) { | 24 TEST(TaskSchedulerPostTaskNowHelperTest, PostTaskInEmptySequence) { |
| 26 std::unique_ptr<Task> task( | 25 std::unique_ptr<Task> task( |
| 27 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | 26 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 28 const Task* task_raw = task.get(); | 27 const Task* task_raw = task.get(); |
| 29 scoped_refptr<Sequence> sequence(new Sequence); | 28 scoped_refptr<Sequence> sequence(new Sequence); |
| 30 PriorityQueue priority_queue(Bind(&DoNothing)); | 29 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 31 TaskTracker task_tracker; | |
| 32 | 30 |
| 33 // Post |task|. | 31 // Post |task|. |
| 34 EXPECT_TRUE(PostTaskHelper(std::move(task), sequence, &priority_queue, | 32 PostTaskNowHelper(std::move(task), sequence, &priority_queue); |
| 35 &task_tracker)); | |
| 36 | 33 |
| 37 // Expect to find the sequence in the priority queue. | 34 // Expect to find the sequence in the priority queue. |
| 38 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); | 35 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); |
| 39 | 36 |
| 40 // Expect to find |task| alone in |sequence|. | 37 // Expect to find |task| alone in |sequence|. |
| 41 EXPECT_EQ(task_raw, sequence->PeekTask()); | 38 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 42 sequence->PopTask(); | 39 sequence->PopTask(); |
| 43 EXPECT_EQ(nullptr, sequence->PeekTask()); | 40 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 44 } | 41 } |
| 45 | 42 |
| 46 // Checks that when PostTaskHelper is called with a sequence that already | 43 // Checks that when PostTaskNowHelper is called with a sequence that already |
| 47 // contains a task, the task is added to the sequence but the sequence is not | 44 // contains a task, the task is added to the sequence but the sequence is not |
| 48 // added to the priority queue. | 45 // added to the priority queue. |
| 49 TEST(TaskSchedulerPostTaskHelperTest, PostTaskInNonEmptySequence) { | 46 TEST(TaskSchedulerPostTaskNowHelperTest, PostTaskInNonEmptySequence) { |
| 50 std::unique_ptr<Task> task( | 47 std::unique_ptr<Task> task( |
| 51 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | 48 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 52 const Task* task_raw = task.get(); | 49 const Task* task_raw = task.get(); |
| 53 scoped_refptr<Sequence> sequence(new Sequence); | 50 scoped_refptr<Sequence> sequence(new Sequence); |
| 54 PriorityQueue priority_queue(Bind(&DoNothing)); | 51 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 55 TaskTracker task_tracker; | |
| 56 | 52 |
| 57 // Add an initial task in |sequence|. | 53 // Add an initial task in |sequence|. |
| 58 sequence->PushTask( | 54 sequence->PushTask( |
| 59 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); | 55 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); |
| 60 | 56 |
| 61 // Post |task|. | 57 // Post |task|. |
| 62 EXPECT_TRUE(PostTaskHelper(std::move(task), sequence, &priority_queue, | 58 PostTaskNowHelper(std::move(task), sequence, &priority_queue); |
| 63 &task_tracker)); | |
| 64 | 59 |
| 65 // Expect to find the priority queue empty. | 60 // Expect to find the priority queue empty. |
| 66 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); | 61 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); |
| 67 | 62 |
| 68 // Expect to find |task| in |sequence| behind the initial task. | 63 // Expect to find |task| in |sequence| behind the initial task. |
| 69 EXPECT_NE(task_raw, sequence->PeekTask()); | 64 EXPECT_NE(task_raw, sequence->PeekTask()); |
| 70 sequence->PopTask(); | 65 sequence->PopTask(); |
| 71 EXPECT_EQ(task_raw, sequence->PeekTask()); | 66 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 72 sequence->PopTask(); | 67 sequence->PopTask(); |
| 73 EXPECT_EQ(nullptr, sequence->PeekTask()); | 68 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 74 } | 69 } |
| 75 | 70 |
| 76 } // namespace | 71 } // namespace |
| 77 } // namespace internal | 72 } // namespace internal |
| 78 } // namespace base | 73 } // namespace base |
| OLD | NEW |