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 <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/task_scheduler/delayed_task_manager.h" |
| 14 #include "base/task_scheduler/priority_queue.h" |
| 15 #include "base/task_scheduler/sequence.h" |
| 16 #include "base/task_scheduler/shutdown_manager.h" |
| 17 #include "base/task_scheduler/task.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace base { |
| 21 namespace task_scheduler { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class TestDelayedTaskManager : public DelayedTaskManager { |
| 26 public: |
| 27 TestDelayedTaskManager(ShutdownManager* shutdown_manager) |
| 28 : DelayedTaskManager(Bind(&DoNothing), shutdown_manager) {} |
| 29 |
| 30 TimeTicks Now() override { return TimeTicks(); } |
| 31 }; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 // Checks that when PostTaskHelper is called with a task that has a |
| 36 // |delayed_run_time|, the task is added to the provided DelayedTaskManager. |
| 37 TEST(TaskSchedulerUtilsTest, PostTaskHelperWithDelayedRunTime) { |
| 38 const TimeDelta delay(TimeDelta::FromSeconds(1)); |
| 39 Task task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks::Now()); |
| 40 task.delayed_run_time = TimeTicks() + delay; |
| 41 scoped_refptr<Sequence> sequence(new Sequence); |
| 42 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 43 ShutdownManager shutdown_manager; |
| 44 TestDelayedTaskManager delayed_task_manager(&shutdown_manager); |
| 45 |
| 46 // Post a delayed task. |
| 47 PostTaskHelper(task, sequence, &priority_queue, &shutdown_manager, |
| 48 &delayed_task_manager); |
| 49 |
| 50 // Expect the sequence and the priority queue to be empty. |
| 51 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 52 EXPECT_TRUE(priority_queue.UnsynchronizedEmpty()); |
| 53 |
| 54 // Expect to find the task in the delayed task manager. |
| 55 EXPECT_EQ(task.delayed_run_time, |
| 56 delayed_task_manager.GetNextDelayedTaskReadyTime()); |
| 57 } |
| 58 |
| 59 // Checks that when PostTaskHelper is called with an empty sequence, the task |
| 60 // is added to the sequence and the sequence is added to the priority queue. |
| 61 TEST(TaskSchedulerUtilsTest, PostTaskNowHelperEmptySequence) { |
| 62 const Task task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks::Now()); |
| 63 scoped_refptr<Sequence> sequence(new Sequence); |
| 64 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 65 ShutdownManager shutdown_manager; |
| 66 |
| 67 // Post a task. |
| 68 PostTaskNowHelper(task, sequence, &priority_queue, &shutdown_manager); |
| 69 |
| 70 // Expect to find the sequence in the priority queue. |
| 71 SequenceSortKey sequence_sort_key; |
| 72 EXPECT_EQ(sequence, priority_queue.BeginTransaction()->PeekSequence( |
| 73 &sequence_sort_key)); |
| 74 |
| 75 // Expect to find the task in the sequence. |
| 76 EXPECT_EQ(task.posted_from, sequence->PeekTask()->posted_from); |
| 77 size_t prev_num_tasks; |
| 78 sequence->PopTask(&prev_num_tasks); |
| 79 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 80 } |
| 81 |
| 82 // Checks that when PostTaskHelper is called with a sequence that already |
| 83 // contains a task, the task is added to the sequence but the sequence is not |
| 84 // added to the priority queue. |
| 85 TEST(TaskSchedulerUtilsTest, PostTaskNowHelperSequenceWithTasks) { |
| 86 const Task task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeTicks::Now()); |
| 87 scoped_refptr<Sequence> sequence(new Sequence); |
| 88 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 89 ShutdownManager shutdown_manager; |
| 90 |
| 91 // Add an initial task in |sequence|. |
| 92 size_t prev_num_tasks; |
| 93 sequence->PushTask(make_scoped_ptr(new Task(FROM_HERE, Closure(), |
| 94 TaskTraits(), TimeTicks::Now())), |
| 95 &prev_num_tasks); |
| 96 |
| 97 // Post a task. |
| 98 PostTaskNowHelper(task, sequence, &priority_queue, &shutdown_manager); |
| 99 |
| 100 // Expect to find the priority queue empty. |
| 101 SequenceSortKey sequence_sort_key; |
| 102 EXPECT_EQ(nullptr, priority_queue.BeginTransaction() |
| 103 ->PeekSequence(&sequence_sort_key) |
| 104 .get()); |
| 105 |
| 106 // Expect to find the posted task at the end of the sequence. |
| 107 sequence->PopTask(&prev_num_tasks); |
| 108 EXPECT_EQ(task.posted_from, sequence->PeekTask()->posted_from); |
| 109 sequence->PopTask(&prev_num_tasks); |
| 110 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 111 } |
| 112 |
| 113 // Checks that when PostTaskHelper is called with a SKIP_ON_SHUTDOWN task after |
| 114 // shutdown has started, the sequence and the priority queue remain unchanged. |
| 115 TEST(TaskSchedulerUtilsTest, PostTaskNowHelperAfterShutdown) { |
| 116 const Task task( |
| 117 FROM_HERE, Bind(&DoNothing), |
| 118 TaskTraits().WithShutdownBehavior(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 119 TimeTicks::Now()); |
| 120 scoped_refptr<Sequence> sequence(new Sequence); |
| 121 PriorityQueue priority_queue(Bind(&DoNothing)); |
| 122 ShutdownManager shutdown_manager; |
| 123 |
| 124 // Post a SKIP_ON_SHUTDOWN task after shutdown. |
| 125 shutdown_manager.Shutdown(); |
| 126 PostTaskNowHelper(task, sequence, &priority_queue, &shutdown_manager); |
| 127 |
| 128 // Expect to find the priority queue empty. |
| 129 SequenceSortKey sequence_sort_key; |
| 130 EXPECT_EQ(nullptr, priority_queue.BeginTransaction() |
| 131 ->PeekSequence(&sequence_sort_key) |
| 132 .get()); |
| 133 |
| 134 // Expect to find the sequence empty. |
| 135 EXPECT_EQ(nullptr, sequence->PeekTask()); |
| 136 } |
| 137 |
| 138 } // namespace task_scheduler |
| 139 } // namespace base |
OLD | NEW |