Chromium Code Reviews| 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/delayed_task_manager.h" | |
| 13 #include "base/task_scheduler/priority_queue.h" | 14 #include "base/task_scheduler/priority_queue.h" |
| 14 #include "base/task_scheduler/sequence.h" | 15 #include "base/task_scheduler/sequence.h" |
| 15 #include "base/task_scheduler/task.h" | 16 #include "base/task_scheduler/task.h" |
| 16 #include "base/task_scheduler/task_tracker.h" | 17 #include "base/task_scheduler/task_tracker.h" |
| 18 #include "base/time/time.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 namespace base { | 21 namespace base { |
| 20 namespace internal { | 22 namespace internal { |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // Checks that when PostTaskHelper is called with an empty sequence, the task | 25 // Checks that when PostTaskHelper is called with an empty sequence, the task is |
| 24 // is added to the sequence and the sequence is added to the priority queue. | 26 // added to the sequence and the sequence is added to the priority queue. |
| 25 TEST(TaskSchedulerPostTaskHelperTest, PostTaskInEmptySequence) { | 27 TEST(TaskSchedulerPostTaskNowHelperTest, PostTaskInEmptySequence) { |
|
gab
2016/04/12 13:35:48
This is using PostTaskHelper not PostTaskNowHelper
fdoray
2016/04/12 14:43:57
Done.
| |
| 26 std::unique_ptr<Task> task( | 28 std::unique_ptr<Task> task( |
| 27 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | 29 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 28 const Task* task_raw = task.get(); | 30 const Task* task_raw = task.get(); |
| 29 scoped_refptr<Sequence> sequence(new Sequence); | 31 scoped_refptr<Sequence> sequence(new Sequence); |
| 30 PriorityQueue priority_queue(Bind(&DoNothing)); | 32 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 31 TaskTracker task_tracker; | 33 TaskTracker task_tracker; |
| 34 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 32 | 35 |
| 33 // Post |task|. | 36 // Post |task|. |
| 34 EXPECT_TRUE(PostTaskHelper(std::move(task), sequence, &priority_queue, | 37 PostTaskHelper(std::move(task), sequence, &priority_queue, &task_tracker, |
| 35 &task_tracker)); | 38 &delayed_task_manager); |
| 36 | 39 |
| 37 // Expect to find the sequence in the priority queue. | 40 // Expect to find the sequence in the priority queue. |
| 38 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); | 41 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->Peek().sequence); |
| 39 | 42 |
| 40 // Expect to find |task| alone in |sequence|. | 43 // Expect to find |task| alone in |sequence|. |
| 41 EXPECT_EQ(task_raw, sequence->PeekTask()); | 44 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 42 sequence->PopTask(); | 45 sequence->PopTask(); |
| 43 EXPECT_EQ(nullptr, sequence->PeekTask()); | 46 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // Checks that when PostTaskHelper is called with a sequence that already | 49 // Checks that when PostTaskHelper is called with a sequence that already |
| 47 // contains a task, the task is added to the sequence but the sequence is not | 50 // contains a task, the task is added to the sequence but the sequence is not |
| 48 // added to the priority queue. | 51 // added to the priority queue. |
| 49 TEST(TaskSchedulerPostTaskHelperTest, PostTaskInNonEmptySequence) { | 52 TEST(TaskSchedulerPostTaskNowHelperTest, PostTaskInNonEmptySequence) { |
| 50 std::unique_ptr<Task> task( | 53 std::unique_ptr<Task> task( |
| 51 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | 54 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); |
| 52 const Task* task_raw = task.get(); | 55 const Task* task_raw = task.get(); |
| 53 scoped_refptr<Sequence> sequence(new Sequence); | 56 scoped_refptr<Sequence> sequence(new Sequence); |
| 54 PriorityQueue priority_queue(Bind(&DoNothing)); | 57 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 55 TaskTracker task_tracker; | 58 TaskTracker task_tracker; |
| 59 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 56 | 60 |
| 57 // Add an initial task in |sequence|. | 61 // Add an initial task in |sequence|. |
| 58 sequence->PushTask( | 62 sequence->PushTask( |
| 59 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); | 63 WrapUnique(new Task(FROM_HERE, Bind(&DoNothing), TaskTraits()))); |
| 60 | 64 |
| 61 // Post |task|. | 65 // Post |task|. |
| 62 EXPECT_TRUE(PostTaskHelper(std::move(task), sequence, &priority_queue, | 66 PostTaskHelper(std::move(task), sequence, &priority_queue, &task_tracker, |
| 63 &task_tracker)); | 67 &delayed_task_manager); |
| 64 | 68 |
| 65 // Expect to find the priority queue empty. | 69 // Expect to find the priority queue empty. |
| 66 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); | 70 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); |
| 67 | 71 |
| 68 // Expect to find |task| in |sequence| behind the initial task. | 72 // Expect to find |task| in |sequence| behind the initial task. |
| 69 EXPECT_NE(task_raw, sequence->PeekTask()); | 73 EXPECT_NE(task_raw, sequence->PeekTask()); |
| 70 sequence->PopTask(); | 74 sequence->PopTask(); |
| 71 EXPECT_EQ(task_raw, sequence->PeekTask()); | 75 EXPECT_EQ(task_raw, sequence->PeekTask()); |
| 72 sequence->PopTask(); | 76 sequence->PopTask(); |
| 73 EXPECT_EQ(nullptr, sequence->PeekTask()); | 77 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 74 } | 78 } |
| 75 | 79 |
| 80 // Checks that when PostTaskHelper is called with a delayed task, it is added to | |
| 81 // the DelayedTaskManager. | |
| 82 TEST(TaskSchedulerPostTaskNowHelperTest, PostDelayedTask) { | |
| 83 const TimeTicks delayed_run_time = TimeTicks::Now() + TimeDelta::FromHours(1); | |
| 84 std::unique_ptr<Task> task( | |
| 85 new Task(FROM_HERE, Bind(&DoNothing), TaskTraits())); | |
| 86 task->delayed_run_time = delayed_run_time; | |
| 87 scoped_refptr<Sequence> sequence(new Sequence); | |
| 88 PriorityQueue priority_queue(Bind(&DoNothing)); | |
| 89 TaskTracker task_tracker; | |
| 90 DelayedTaskManager delayed_task_manager(Bind(&DoNothing)); | |
| 91 | |
| 92 // Post |task|. | |
| 93 PostTaskHelper(std::move(task), sequence, &priority_queue, &task_tracker, | |
| 94 &delayed_task_manager); | |
| 95 | |
| 96 // Expect to find |priority_queue| and |sequence| empty. | |
| 97 EXPECT_TRUE(priority_queue.BeginTransaction()->Peek().is_null()); | |
| 98 EXPECT_EQ(nullptr, sequence->PeekTask()); | |
| 99 | |
| 100 // Expect the DelayedTaskManager's delayed run time to have been updated. | |
| 101 EXPECT_EQ(delayed_run_time, delayed_task_manager.GetDelayedRunTime()); | |
| 102 } | |
| 103 | |
| 76 } // namespace | 104 } // namespace |
| 77 } // namespace internal | 105 } // namespace internal |
| 78 } // namespace base | 106 } // namespace base |
| OLD | NEW |