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