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"
13 class TaskQueueSetsTest
: public testing::Test
{
15 void SetUp() override
{ task_queue_sets_
.reset(new TaskQueueSets(kNumSets
)); }
19 kNumSets
= 5 // An arbitary choice.
22 TaskQueueImpl
* NewTaskQueue(const char* queue_name
) {
23 scoped_refptr
<internal::TaskQueueImpl
> queue
=
24 make_scoped_refptr(new internal::TaskQueueImpl(
25 nullptr, TaskQueue::Spec(queue_name
), "test", "test"));
26 task_queues_
.push_back(queue
);
30 TaskQueueImpl::Task
FakeTaskWithEnqueueOrder(int enqueue_order
) {
31 TaskQueueImpl::Task
fake_task(FROM_HERE
, base::Closure(), 0, true);
32 fake_task
.set_enqueue_order(enqueue_order
);
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("queue");
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("queue");
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("queue");
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(FakeTaskWithEnqueueOrder(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("queue");
74 queue
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(10));
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("queue1");
85 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue2");
86 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue2");
87 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(6));
88 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(5));
89 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(4));
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("queue1");
102 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue2");
103 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue3");
104 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(6));
105 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(3));
106 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(1));
107 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(4));
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("queue");
126 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue");
127 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue");
128 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(6));
129 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(5));
130 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(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("queue1");
150 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue2");
151 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue3");
152 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(0x7ffffff1));
153 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(0x7ffffff0));
154 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(-0x7ffffff1));
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("queue1");
167 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue2");
168 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue3");
169 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(6));
170 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(5));
171 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(4));
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("queue1");
185 internal::TaskQueueImpl
* queue2
= NewTaskQueue("queue2");
186 internal::TaskQueueImpl
* queue3
= NewTaskQueue("queue3");
187 internal::TaskQueueImpl
* queue4
= NewTaskQueue("queue4");
188 queue1
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(6));
189 queue2
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(5));
190 queue3
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(4));
191 queue4
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(3));
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 TEST_F(TaskQueueSetsTest
, IsSetEmpty_NoWork
) {
217 EXPECT_TRUE(task_queue_sets_
->IsSetEmpty(set
));
219 internal::TaskQueueImpl
* queue
= NewTaskQueue("queue");
220 task_queue_sets_
->AssignQueueToSet(queue
, set
);
221 EXPECT_TRUE(task_queue_sets_
->IsSetEmpty(set
));
224 TEST_F(TaskQueueSetsTest
, IsSetEmpty_Work
) {
226 EXPECT_TRUE(task_queue_sets_
->IsSetEmpty(set
));
228 internal::TaskQueueImpl
* queue
= NewTaskQueue("queue");
229 queue
->PushTaskOntoWorkQueueForTest(FakeTaskWithEnqueueOrder(1));
230 task_queue_sets_
->AssignQueueToSet(queue
, set
);
231 EXPECT_FALSE(task_queue_sets_
->IsSetEmpty(set
));
233 queue
->PopTaskFromWorkQueueForTest();
234 task_queue_sets_
->OnPopQueue(queue
);
235 EXPECT_TRUE(task_queue_sets_
->IsSetEmpty(set
));
238 } // namespace internal
239 } // namespace scheduler