Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / scheduler / child / task_queue_sets_unittest.cc
blobcf7959891a82baa2715671b9f084f42fc1ae7bb5
1 // Copyright 2015 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.
5 #include "components/scheduler/child/task_queue_sets.h"
7 #include "components/scheduler/child/task_queue_impl.h"
8 #include "testing/gmock/include/gmock/gmock.h"
10 namespace scheduler {
11 namespace internal {
13 class TaskQueueSetsTest : public testing::Test {
14 public:
15 void SetUp() override { task_queue_sets_.reset(new TaskQueueSets(kNumSets)); }
17 protected:
18 enum {
19 kNumSets = 5 // An arbitary choice.
22 internal::TaskQueueImpl* NewTaskQueue() {
23 scoped_refptr<internal::TaskQueueImpl> queue =
24 make_scoped_refptr(new internal::TaskQueueImpl(
25 nullptr, TaskQueue::Spec("test queue"), "test", "test"));
26 task_queues_.push_back(queue);
27 return queue.get();
30 base::PendingTask FakeTaskWithSequenceNum(int sequence_num) {
31 base::PendingTask fake_task(FROM_HERE, base::Closure());
32 fake_task.sequence_num = sequence_num;
33 return fake_task;
36 std::vector<scoped_refptr<internal::TaskQueueImpl>> task_queues_;
37 scoped_ptr<TaskQueueSets> task_queue_sets_;
40 TEST_F(TaskQueueSetsTest, AssignQueueToSet) {
41 internal::TaskQueueImpl* queue = NewTaskQueue();
42 size_t set = TaskQueue::NORMAL_PRIORITY;
43 task_queue_sets_->AssignQueueToSet(queue, set);
45 EXPECT_EQ(set, queue->get_task_queue_set_index());
48 TEST_F(TaskQueueSetsTest, GetOldestQueueInSet_QueueEmpty) {
49 internal::TaskQueueImpl* queue = NewTaskQueue();
50 size_t set = TaskQueue::NORMAL_PRIORITY;
51 task_queue_sets_->AssignQueueToSet(queue, set);
53 internal::TaskQueueImpl* selected_queue;
54 EXPECT_FALSE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
57 TEST_F(TaskQueueSetsTest, OnPushQueue) {
58 internal::TaskQueueImpl* queue = NewTaskQueue();
59 size_t set = TaskQueue::NORMAL_PRIORITY;
60 task_queue_sets_->AssignQueueToSet(queue, set);
62 internal::TaskQueueImpl* selected_queue;
63 EXPECT_FALSE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
65 queue->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(10));
66 task_queue_sets_->OnPushQueue(queue);
68 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
69 EXPECT_EQ(queue, selected_queue);
72 TEST_F(TaskQueueSetsTest, GetOldestQueueInSet_SingleTaskInSet) {
73 internal::TaskQueueImpl* queue = NewTaskQueue();
74 queue->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(10));
75 size_t set = 1;
76 task_queue_sets_->AssignQueueToSet(queue, set);
78 internal::TaskQueueImpl* selected_queue;
79 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
80 EXPECT_EQ(queue, selected_queue);
83 TEST_F(TaskQueueSetsTest, GetOldestQueueInSet_MultipleAgesInSet) {
84 internal::TaskQueueImpl* queue1 = NewTaskQueue();
85 internal::TaskQueueImpl* queue2 = NewTaskQueue();
86 internal::TaskQueueImpl* queue3 = NewTaskQueue();
87 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(6));
88 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(5));
89 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(4));
90 size_t set = 2;
91 task_queue_sets_->AssignQueueToSet(queue1, set);
92 task_queue_sets_->AssignQueueToSet(queue2, set);
93 task_queue_sets_->AssignQueueToSet(queue3, set);
95 internal::TaskQueueImpl* selected_queue;
96 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
97 EXPECT_EQ(queue3, selected_queue);
100 TEST_F(TaskQueueSetsTest, OnPopQueue) {
101 internal::TaskQueueImpl* queue1 = NewTaskQueue();
102 internal::TaskQueueImpl* queue2 = NewTaskQueue();
103 internal::TaskQueueImpl* queue3 = NewTaskQueue();
104 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(6));
105 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(3));
106 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(1));
107 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(4));
108 size_t set = 3;
109 task_queue_sets_->AssignQueueToSet(queue1, set);
110 task_queue_sets_->AssignQueueToSet(queue2, set);
111 task_queue_sets_->AssignQueueToSet(queue3, set);
113 internal::TaskQueueImpl* selected_queue;
114 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
115 EXPECT_EQ(queue2, selected_queue);
117 queue2->PopTaskFromWorkQueueForTest();
118 task_queue_sets_->OnPopQueue(queue2);
120 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
121 EXPECT_EQ(queue2, selected_queue);
124 TEST_F(TaskQueueSetsTest, OnPopQueue_QueueBecomesEmpty) {
125 internal::TaskQueueImpl* queue1 = NewTaskQueue();
126 internal::TaskQueueImpl* queue2 = NewTaskQueue();
127 internal::TaskQueueImpl* queue3 = NewTaskQueue();
128 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(6));
129 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(5));
130 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(4));
131 size_t set = 4;
132 task_queue_sets_->AssignQueueToSet(queue1, set);
133 task_queue_sets_->AssignQueueToSet(queue2, set);
134 task_queue_sets_->AssignQueueToSet(queue3, set);
136 internal::TaskQueueImpl* selected_queue;
137 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
138 EXPECT_EQ(queue3, selected_queue);
140 queue3->PopTaskFromWorkQueueForTest();
141 task_queue_sets_->OnPopQueue(queue3);
143 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
144 EXPECT_EQ(queue2, selected_queue);
147 TEST_F(TaskQueueSetsTest,
148 GetOldestQueueInSet_MultipleAgesInSetIntegerRollover) {
149 internal::TaskQueueImpl* queue1 = NewTaskQueue();
150 internal::TaskQueueImpl* queue2 = NewTaskQueue();
151 internal::TaskQueueImpl* queue3 = NewTaskQueue();
152 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(0x7ffffff1));
153 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(0x7ffffff0));
154 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(-0x7ffffff1));
155 size_t set = 0;
156 task_queue_sets_->AssignQueueToSet(queue1, set);
157 task_queue_sets_->AssignQueueToSet(queue2, set);
158 task_queue_sets_->AssignQueueToSet(queue3, set);
160 internal::TaskQueueImpl* selected_queue;
161 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
162 EXPECT_EQ(queue2, selected_queue);
165 TEST_F(TaskQueueSetsTest, GetOldestQueueInSet_MultipleAgesInSet_RemoveQueue) {
166 internal::TaskQueueImpl* queue1 = NewTaskQueue();
167 internal::TaskQueueImpl* queue2 = NewTaskQueue();
168 internal::TaskQueueImpl* queue3 = NewTaskQueue();
169 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(6));
170 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(5));
171 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(4));
172 size_t set = 1;
173 task_queue_sets_->AssignQueueToSet(queue1, set);
174 task_queue_sets_->AssignQueueToSet(queue2, set);
175 task_queue_sets_->AssignQueueToSet(queue3, set);
176 task_queue_sets_->RemoveQueue(queue3);
178 internal::TaskQueueImpl* selected_queue;
179 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set, &selected_queue));
180 EXPECT_EQ(queue2, selected_queue);
183 TEST_F(TaskQueueSetsTest, AssignQueueToSet_Complex) {
184 internal::TaskQueueImpl* queue1 = NewTaskQueue();
185 internal::TaskQueueImpl* queue2 = NewTaskQueue();
186 internal::TaskQueueImpl* queue3 = NewTaskQueue();
187 internal::TaskQueueImpl* queue4 = NewTaskQueue();
188 queue1->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(6));
189 queue2->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(5));
190 queue3->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(4));
191 queue4->PushTaskOntoWorkQueueForTest(FakeTaskWithSequenceNum(3));
192 size_t set1 = 1;
193 size_t set2 = 2;
194 task_queue_sets_->AssignQueueToSet(queue1, set1);
195 task_queue_sets_->AssignQueueToSet(queue2, set1);
196 task_queue_sets_->AssignQueueToSet(queue3, set2);
197 task_queue_sets_->AssignQueueToSet(queue4, set2);
199 internal::TaskQueueImpl* selected_queue;
200 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set1, &selected_queue));
201 EXPECT_EQ(queue2, selected_queue);
203 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set2, &selected_queue));
204 EXPECT_EQ(queue4, selected_queue);
206 task_queue_sets_->AssignQueueToSet(queue4, set1);
208 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set1, &selected_queue));
209 EXPECT_EQ(queue4, selected_queue);
211 EXPECT_TRUE(task_queue_sets_->GetOldestQueueInSet(set2, &selected_queue));
212 EXPECT_EQ(queue3, selected_queue);
215 } // namespace internal
216 } // namespace scheduler