1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
base / task / sequence_manager / task_queue_selector_unittest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "base/task/sequence_manager/task_queue_selector.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/pending_task.h"
#include "base/task/sequence_manager/enqueue_order_generator.h"
#include "base/task/sequence_manager/task_queue_impl.h"
#include "base/task/sequence_manager/test/mock_time_domain.h"
#include "base/task/sequence_manager/work_queue.h"
#include "base/task/sequence_manager/work_queue_sets.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::ElementsAre;
using testing::NotNull;
namespace base {
namespace sequence_manager {
namespace internal {
// To avoid symbol collisions in jumbo builds.
namespace task_queue_selector_unittest {
namespace {
const TaskQueue::QueuePriority kHighestPriority = 0;
const TaskQueue::QueuePriority kHighPriority = 4;
const TaskQueue::QueuePriority kDefaultPriority = 5;
const TaskQueue::QueuePriority kPriorityCount = 10;
const size_t kTaskQueueCount = kPriorityCount;
// Tests assume high priority is higher than default priority, but nothing about
// their values.
static_assert(kHighPriority < kDefaultPriority);
} // namespace
class MockObserver : public TaskQueueSelector::Observer {
public:
MockObserver() = default;
MockObserver(const MockObserver&) = delete;
MockObserver& operator=(const MockObserver&) = delete;
~MockObserver() override = default;
MOCK_METHOD1(OnTaskQueueEnabled, void(internal::TaskQueueImpl*));
MOCK_METHOD0(OnWorkAvailable, void());
};
class TaskQueueSelectorForTest : public TaskQueueSelector {
public:
using TaskQueueSelector::ActivePriorityTracker;
using TaskQueueSelector::ChooseWithPriority;
using TaskQueueSelector::delayed_work_queue_sets;
using TaskQueueSelector::immediate_work_queue_sets;
using TaskQueueSelector::SetImmediateStarvationCountForTest;
using TaskQueueSelector::SetOperationOldest;
explicit TaskQueueSelectorForTest(
scoped_refptr<AssociatedThreadId> associated_thread)
: TaskQueueSelector(
associated_thread,
SequenceManager::Settings::Builder()
.SetPrioritySettings(
SequenceManager::PrioritySettings(kPriorityCount,
kDefaultPriority))
.Build()) {}
};
class TaskQueueSelectorTest : public testing::Test {
public:
TaskQueueSelectorTest()
: test_closure_(BindRepeating(&TaskQueueSelectorTest::TestFunction)),
associated_thread_(AssociatedThreadId::CreateBound()),
selector_(associated_thread_) {}
~TaskQueueSelectorTest() override = default;
void PushTasks(const size_t queue_indices[], size_t num_tasks) {
EnqueueOrderGenerator enqueue_order_generator;
for (size_t i = 0; i < num_tasks; i++) {
task_queues_[queue_indices[i]]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
enqueue_order_generator.GenerateNext()));
}
}
void PushTasksWithEnqueueOrder(const size_t queue_indices[],
const size_t enqueue_orders[],
size_t num_tasks) {
for (size_t i = 0; i < num_tasks; i++) {
task_queues_[queue_indices[i]]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(enqueue_orders[i])));
}
}
void PushTask(const size_t queue_index, const size_t enqueue_order) {
task_queues_[queue_index]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(enqueue_order)));
}
std::vector<size_t> PopTasksAndReturnQueueIndices() {
std::vector<size_t> order;
while (WorkQueue* chosen_work_queue =
selector_.SelectWorkQueueToService()) {
size_t chosen_queue_index =
queue_to_index_map_.find(chosen_work_queue->task_queue())->second;
order.push_back(chosen_queue_index);
chosen_work_queue->PopTaskForTesting();
chosen_work_queue->work_queue_sets()->OnPopMinQueueInSet(
chosen_work_queue);
}
return order;
}
static void TestFunction() {}
protected:
void SetUp() final {
for (size_t i = 0; i < kTaskQueueCount; i++) {
std::unique_ptr<TaskQueueImpl> task_queue =
std::make_unique<TaskQueueImpl>(nullptr, nullptr,
TaskQueue::Spec(QueueName::TEST_TQ));
selector_.AddQueue(task_queue.get(), kDefaultPriority);
task_queues_.push_back(std::move(task_queue));
}
for (size_t i = 0; i < kTaskQueueCount; i++) {
EXPECT_EQ(kDefaultPriority, task_queues_[i]->GetQueuePriority()) << i;
queue_to_index_map_.insert(std::make_pair(task_queues_[i].get(), i));
}
}
void TearDown() final {
for (std::unique_ptr<TaskQueueImpl>& task_queue : task_queues_) {
// Note since this test doesn't have a SequenceManager we need to
// manually remove |task_queue| from the |selector_|. Normally
// UnregisterTaskQueue would do that.
selector_.RemoveQueue(task_queue.get());
task_queue->UnregisterTaskQueue();
}
}
std::unique_ptr<TaskQueueImpl> NewTaskQueueWithBlockReporting() {
return std::make_unique<TaskQueueImpl>(nullptr, nullptr,
TaskQueue::Spec(QueueName::TEST_TQ));
}
RepeatingClosure test_closure_;
scoped_refptr<AssociatedThreadId> associated_thread_;
TaskQueueSelectorForTest selector_;
std::vector<std::unique_ptr<TaskQueueImpl>> task_queues_;
std::map<TaskQueueImpl*, size_t> queue_to_index_map_;
};
TEST_F(TaskQueueSelectorTest, TestDefaultPriority) {
size_t queue_order[] = {4, 3, 2, 1, 0};
PushTasks(queue_order, 5);
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(4, 3, 2, 1, 0));
}
TEST_F(TaskQueueSelectorTest, TestPriorities) {
for (size_t priority = 0; priority < kDefaultPriority; ++priority) {
size_t queue_order[] = {0, 1, 2, 3, 4};
PushTasks(queue_order, 5);
if (priority != kDefaultPriority) {
selector_.SetQueuePriority(task_queues_[2].get(), priority);
}
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(2, 0, 1, 3, 4));
}
}
TEST_F(TaskQueueSelectorTest, TestMultiplePriorities) {
size_t reverse_priority_order[kPriorityCount];
for (size_t priority = 0; priority < kPriorityCount; ++priority) {
reverse_priority_order[(kPriorityCount - 1) - priority] = priority;
}
PushTasks(reverse_priority_order, kPriorityCount);
for (size_t i = 0; i < kPriorityCount; ++i) {
TaskQueue::QueuePriority priority = reverse_priority_order[i];
if (priority != kDefaultPriority) {
selector_.SetQueuePriority(task_queues_[i].get(), priority);
}
EXPECT_EQ(task_queues_[i]->GetQueuePriority(), priority);
}
EXPECT_THAT(PopTasksAndReturnQueueIndices(),
ElementsAre(9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
}
TEST_F(TaskQueueSelectorTest, TestObserverWorkAvailableOnPushTask) {
testing::StrictMock<MockObserver> mock_observer;
selector_.SetTaskQueueSelectorObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnWorkAvailable());
PushTask(/* queue_index=*/0, /* enqueue_order=*/4);
}
TEST_F(TaskQueueSelectorTest, TestObserverWithEnabledQueue) {
task_queues_[1]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[1].get());
{
testing::StrictMock<MockObserver> mock_observer;
selector_.SetTaskQueueSelectorObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_)).Times(1);
task_queues_[1]->SetQueueEnabled(true);
selector_.EnableQueue(task_queues_[1].get());
// Clear observer before it goes out of scope.
selector_.SetTaskQueueSelectorObserver(nullptr);
}
}
TEST_F(TaskQueueSelectorTest,
TestObserverWithSetQueuePriorityAndQueueAlreadyEnabled) {
selector_.SetQueuePriority(task_queues_[1].get(), kHighPriority);
{
testing::StrictMock<MockObserver> mock_observer;
selector_.SetTaskQueueSelectorObserver(&mock_observer);
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_)).Times(0);
selector_.SetQueuePriority(task_queues_[1].get(), kDefaultPriority);
// Clear observer before it goes out of scope.
selector_.SetTaskQueueSelectorObserver(nullptr);
}
}
TEST_F(TaskQueueSelectorTest, TestDisableEnable) {
testing::StrictMock<MockObserver> mock_observer;
selector_.SetTaskQueueSelectorObserver(&mock_observer);
size_t queue_order[] = {0, 1, 2, 3, 4};
{
// Work is available when a task is pushed to an enabled queue (all queues
// were previously empty).
EXPECT_CALL(mock_observer, OnWorkAvailable());
PushTasks(queue_order, 5);
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
task_queues_[2]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[2].get());
task_queues_[4]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[4].get());
// Disabling a queue should not affect its priority.
EXPECT_EQ(kDefaultPriority, task_queues_[2]->GetQueuePriority());
EXPECT_EQ(kDefaultPriority, task_queues_[4]->GetQueuePriority());
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(0, 1, 3));
task_queues_[2]->SetQueueEnabled(true);
{
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_));
// Work is available when a non-empty queue is enabled (all queues were
// previously disabled).
EXPECT_CALL(mock_observer, OnWorkAvailable());
selector_.EnableQueue(task_queues_[2].get());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
selector_.SetQueuePriority(task_queues_[2].get(), kPriorityCount - 1);
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(2));
task_queues_[4]->SetQueueEnabled(true);
{
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_));
// Work is available when a non-empty queue is enabled (all queues were
// previously disabled).
EXPECT_CALL(mock_observer, OnWorkAvailable());
selector_.EnableQueue(task_queues_[4].get());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(4));
// Clear observer before it goes out of scope.
selector_.SetTaskQueueSelectorObserver(nullptr);
}
TEST_F(TaskQueueSelectorTest, TestDisableChangePriorityThenEnable) {
EXPECT_TRUE(task_queues_[2]->delayed_work_queue()->Empty());
EXPECT_TRUE(task_queues_[2]->immediate_work_queue()->Empty());
task_queues_[2]->SetQueueEnabled(false);
selector_.SetQueuePriority(task_queues_[2].get(), kHighPriority);
size_t queue_order[] = {0, 1, 2, 3, 4};
PushTasks(queue_order, 5);
EXPECT_TRUE(task_queues_[2]->delayed_work_queue()->Empty());
EXPECT_FALSE(task_queues_[2]->immediate_work_queue()->Empty());
task_queues_[2]->SetQueueEnabled(true);
EXPECT_EQ(kHighPriority, task_queues_[2]->GetQueuePriority());
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(2, 0, 1, 3, 4));
}
TEST_F(TaskQueueSelectorTest, TestEmptyQueues) {
EXPECT_EQ(nullptr, selector_.SelectWorkQueueToService());
// Test only disabled queues.
size_t queue_order[] = {0};
PushTasks(queue_order, 1);
task_queues_[0]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[0].get());
EXPECT_EQ(nullptr, selector_.SelectWorkQueueToService());
// These tests are unusual since there's no TQM. To avoid a later DCHECK when
// deleting the task queue, we re-enable the queue here so the selector
// doesn't get out of sync.
task_queues_[0]->SetQueueEnabled(true);
selector_.EnableQueue(task_queues_[0].get());
}
TEST_F(TaskQueueSelectorTest, TestAge) {
size_t enqueue_order[] = {10, 1, 2, 9, 4};
size_t queue_order[] = {0, 1, 2, 3, 4};
PushTasksWithEnqueueOrder(queue_order, enqueue_order, 5);
EXPECT_THAT(PopTasksAndReturnQueueIndices(), ElementsAre(1, 2, 4, 3, 0));
}
class TaskQueueSelectorStarvationTest : public TaskQueueSelectorTest {
public:
TaskQueueSelectorStarvationTest() = default;
protected:
void TestPriorityOrder(const size_t queue_order[], size_t num_tasks) {
for (size_t i = 0; i < kTaskQueueCount; i++) {
// Setting the queue priority to its current value causes a check to fail.
if (task_queues_[i]->GetQueuePriority() !=
static_cast<TaskQueue::QueuePriority>(i)) {
selector_.SetQueuePriority(task_queues_[i].get(),
static_cast<TaskQueue::QueuePriority>(i));
}
}
ASSERT_EQ(num_tasks, kTaskQueueCount);
PushTasks(queue_order, kTaskQueueCount);
for (size_t priority = 0; priority < kTaskQueueCount; priority++) {
for (int i = 0; i < 100; i++) {
WorkQueue* chosen_work_queue = selector_.SelectWorkQueueToService();
ASSERT_THAT(chosen_work_queue, NotNull());
EXPECT_EQ(task_queues_[priority].get(),
chosen_work_queue->task_queue());
// Don't remove task from queue to simulate all queues still being full.
}
// Simulate the highest priority queue becoming empty.
WorkQueue* chosen_work_queue = selector_.SelectWorkQueueToService();
chosen_work_queue->PopTaskForTesting();
chosen_work_queue->work_queue_sets()->OnPopMinQueueInSet(
chosen_work_queue);
}
}
};
TEST_F(TaskQueueSelectorStarvationTest,
HigherPriorityWorkStarvesLowerPriorityWork) {
size_t queue_order[kTaskQueueCount];
for (size_t i = 0; i < kTaskQueueCount; i++)
queue_order[i] = i;
TestPriorityOrder(queue_order, kTaskQueueCount);
}
TEST_F(TaskQueueSelectorStarvationTest,
NewHigherPriorityTasksStarveOldLowerPriorityTasks) {
// Enqueue tasks in order from lowest to highest priority, and check that they
// still run in order from highest to lowest priority.
size_t queue_order[kTaskQueueCount];
for (size_t i = 0; i < kTaskQueueCount; i++)
queue_order[i] = (kTaskQueueCount - i) - 1;
TestPriorityOrder(queue_order, kTaskQueueCount);
}
TEST_F(TaskQueueSelectorTest, GetHighestPendingPriority) {
EXPECT_FALSE(selector_.GetHighestPendingPriority().has_value());
size_t queue_order[] = {0, 1};
PushTasks(queue_order, 2);
selector_.SetQueuePriority(task_queues_[1].get(), kHighPriority);
EXPECT_EQ(kHighPriority, *selector_.GetHighestPendingPriority());
PopTasksAndReturnQueueIndices();
EXPECT_FALSE(selector_.GetHighestPendingPriority().has_value());
PushTasks(queue_order, 1);
EXPECT_EQ(kDefaultPriority, *selector_.GetHighestPendingPriority());
PopTasksAndReturnQueueIndices();
EXPECT_FALSE(selector_.GetHighestPendingPriority().has_value());
}
TEST_F(TaskQueueSelectorTest, ChooseWithPriority_Empty) {
EXPECT_EQ(
nullptr,
selector_
.ChooseWithPriority<TaskQueueSelectorForTest::SetOperationOldest>(
kDefaultPriority));
}
TEST_F(TaskQueueSelectorTest, ChooseWithPriority_OnlyDelayed) {
task_queues_[0]->delayed_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2)));
EXPECT_EQ(
task_queues_[0]->delayed_work_queue(),
selector_
.ChooseWithPriority<TaskQueueSelectorForTest::SetOperationOldest>(
kDefaultPriority));
}
TEST_F(TaskQueueSelectorTest, ChooseWithPriority_OnlyImmediate) {
task_queues_[0]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2)));
EXPECT_EQ(
task_queues_[0]->immediate_work_queue(),
selector_
.ChooseWithPriority<TaskQueueSelectorForTest::SetOperationOldest>(
kDefaultPriority));
}
TEST_F(TaskQueueSelectorTest,
SelectWorkQueueToServiceImmediateOnlyWithoutImmediateTask) {
task_queues_[0]->delayed_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2)));
EXPECT_EQ(nullptr,
selector_.SelectWorkQueueToService(
TaskQueueSelector::SelectTaskOption::kSkipDelayedTask));
EXPECT_EQ(task_queues_[0]->delayed_work_queue(),
selector_.SelectWorkQueueToService());
}
TEST_F(TaskQueueSelectorTest,
SelectWorkQueueToServiceImmediateOnlyWithDelayedTasks) {
task_queues_[0]->delayed_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(1)));
task_queues_[0]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2)));
EXPECT_EQ(task_queues_[0]->immediate_work_queue(),
selector_.SelectWorkQueueToService(
TaskQueueSelector::SelectTaskOption::kSkipDelayedTask));
EXPECT_EQ(task_queues_[0]->delayed_work_queue(),
selector_.SelectWorkQueueToService());
}
TEST_F(TaskQueueSelectorTest,
SelectWorkQueueToServiceImmediateOnlyWithDisabledQueues) {
task_queues_[0]->delayed_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(1)));
task_queues_[0]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2)));
task_queues_[1]->delayed_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(3)));
task_queues_[2]->immediate_work_queue()->Push(
Task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(4)));
EXPECT_EQ(task_queues_[0]->delayed_work_queue(),
selector_.SelectWorkQueueToService());
EXPECT_EQ(task_queues_[0]->immediate_work_queue(),
selector_.SelectWorkQueueToService(
TaskQueueSelector::SelectTaskOption::kSkipDelayedTask));
task_queues_[0]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[0].get());
EXPECT_EQ(task_queues_[1]->delayed_work_queue(),
selector_.SelectWorkQueueToService());
EXPECT_EQ(task_queues_[2]->immediate_work_queue(),
selector_.SelectWorkQueueToService(
TaskQueueSelector::SelectTaskOption::kSkipDelayedTask));
task_queues_[1]->SetQueueEnabled(false);
selector_.DisableQueue(task_queues_[1].get());
EXPECT_EQ(task_queues_[2]->immediate_work_queue(),
selector_.SelectWorkQueueToService(
TaskQueueSelector::SelectTaskOption::kSkipDelayedTask));
EXPECT_EQ(task_queues_[2]->immediate_work_queue(),
selector_.SelectWorkQueueToService());
}
TEST_F(TaskQueueSelectorTest, TestObserverWithOneBlockedQueue) {
testing::StrictMock<MockObserver> mock_observer; // Must outlive `selector`
TaskQueueSelectorForTest selector(associated_thread_);
selector.SetTaskQueueSelectorObserver(&mock_observer);
std::unique_ptr<TaskQueueImpl> task_queue(NewTaskQueueWithBlockReporting());
selector.AddQueue(task_queue.get(), kDefaultPriority);
task_queue->SetQueueEnabled(false);
selector.DisableQueue(task_queue.get());
Task task(PostedTask(nullptr, test_closure_, FROM_HERE), EnqueueOrder(),
EnqueueOrder::FromIntForTesting(2));
task_queue->immediate_work_queue()->Push(std::move(task));
EXPECT_EQ(nullptr, selector.SelectWorkQueueToService());
task_queue->SetQueueEnabled(true);
{
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_));
EXPECT_CALL(mock_observer, OnWorkAvailable());
selector.EnableQueue(task_queue.get());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
selector.RemoveQueue(task_queue.get());
task_queue->UnregisterTaskQueue();
}
TEST_F(TaskQueueSelectorTest, TestObserverWithTwoBlockedQueues) {
testing::StrictMock<MockObserver> mock_observer; // Must outlive `selector`
TaskQueueSelectorForTest selector(associated_thread_);
selector.SetTaskQueueSelectorObserver(&mock_observer);
std::unique_ptr<TaskQueueImpl> task_queue(NewTaskQueueWithBlockReporting());
std::unique_ptr<TaskQueueImpl> task_queue2(NewTaskQueueWithBlockReporting());
selector.AddQueue(task_queue.get(), kDefaultPriority);
selector.AddQueue(task_queue2.get(), kDefaultPriority);
task_queue->SetQueueEnabled(false);
task_queue2->SetQueueEnabled(false);
selector.DisableQueue(task_queue.get());
selector.DisableQueue(task_queue2.get());
selector.SetQueuePriority(task_queue2.get(), kHighestPriority);
Task task1(PostedTask(nullptr, test_closure_, FROM_HERE),
EnqueueOrder::FromIntForTesting(2),
EnqueueOrder::FromIntForTesting(2));
Task task2(PostedTask(nullptr, test_closure_, FROM_HERE),
EnqueueOrder::FromIntForTesting(3),
EnqueueOrder::FromIntForTesting(3));
task_queue->immediate_work_queue()->Push(std::move(task1));
task_queue2->immediate_work_queue()->Push(std::move(task2));
EXPECT_EQ(nullptr, selector.SelectWorkQueueToService());
{
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_));
EXPECT_CALL(mock_observer, OnWorkAvailable());
task_queue->SetQueueEnabled(true);
selector.EnableQueue(task_queue.get());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
selector.RemoveQueue(task_queue.get());
task_queue->UnregisterTaskQueue();
EXPECT_EQ(nullptr, selector.SelectWorkQueueToService());
{
EXPECT_CALL(mock_observer, OnTaskQueueEnabled(_));
EXPECT_CALL(mock_observer, OnWorkAvailable());
task_queue2->SetQueueEnabled(true);
selector.EnableQueue(task_queue2.get());
testing::Mock::VerifyAndClearExpectations(&mock_observer);
}
selector.RemoveQueue(task_queue2.get());
task_queue2->UnregisterTaskQueue();
}
TEST_F(TaskQueueSelectorTest, CollectSkippedOverLowerPriorityTasks) {
size_t queue_order[] = {0, 1, 2, 3, 2, 1, 0};
PushTasks(queue_order, 7);
selector_.SetQueuePriority(task_queues_[3].get(), kHighPriority);
std::vector<const Task*> result;
selector_.CollectSkippedOverLowerPriorityTasks(
task_queues_[3]->immediate_work_queue(), &result);
ASSERT_EQ(3u, result.size());
EXPECT_EQ(2u, result[0]->enqueue_order()); // The order here isn't important.
EXPECT_EQ(3u, result[1]->enqueue_order());
EXPECT_EQ(4u, result[2]->enqueue_order());
}
struct ChooseWithPriorityTestParam {
int delayed_task_enqueue_order;
int immediate_task_enqueue_order;
int immediate_starvation_count;
const char* expected_work_queue_name;
};
static const ChooseWithPriorityTestParam kChooseWithPriorityTestCases[] = {
{1, 2, 0, "delayed"}, {1, 2, 1, "delayed"}, {1, 2, 2, "delayed"},
{1, 2, 3, "immediate"}, {1, 2, 4, "immediate"}, {2, 1, 4, "immediate"},
{2, 1, 4, "immediate"},
};
class ChooseWithPriorityTest
: public TaskQueueSelectorTest,
public testing::WithParamInterface<ChooseWithPriorityTestParam> {};
TEST_P(ChooseWithPriorityTest, RoundRobinTest) {
task_queues_[0]->immediate_work_queue()->Push(Task(
PostedTask(nullptr, test_closure_, FROM_HERE),
EnqueueOrder::FromIntForTesting(GetParam().immediate_task_enqueue_order),
EnqueueOrder::FromIntForTesting(
GetParam().immediate_task_enqueue_order)));
task_queues_[0]->delayed_work_queue()->Push(Task(
PostedTask(nullptr, test_closure_, FROM_HERE),
EnqueueOrder::FromIntForTesting(GetParam().delayed_task_enqueue_order),
EnqueueOrder::FromIntForTesting(GetParam().delayed_task_enqueue_order)));
selector_.SetImmediateStarvationCountForTest(
GetParam().immediate_starvation_count);
WorkQueue* chosen_work_queue =
selector_
.ChooseWithPriority<TaskQueueSelectorForTest::SetOperationOldest>(
kDefaultPriority);
EXPECT_EQ(chosen_work_queue->task_queue(), task_queues_[0].get());
EXPECT_STREQ(chosen_work_queue->name(), GetParam().expected_work_queue_name);
}
INSTANTIATE_TEST_SUITE_P(ChooseWithPriorityTest,
ChooseWithPriorityTest,
testing::ValuesIn(kChooseWithPriorityTestCases));
class ActivePriorityTrackerTest : public testing::Test {
protected:
TaskQueueSelectorForTest::ActivePriorityTracker active_priority_tracker_;
};
TEST_F(ActivePriorityTrackerTest, SetPriorityActiveAndInactive) {
EXPECT_FALSE(active_priority_tracker_.HasActivePriority());
EXPECT_FALSE(active_priority_tracker_.IsActive(kDefaultPriority));
active_priority_tracker_.SetActive(kDefaultPriority, true);
EXPECT_TRUE(active_priority_tracker_.HasActivePriority());
EXPECT_TRUE(active_priority_tracker_.IsActive(kDefaultPriority));
active_priority_tracker_.SetActive(kDefaultPriority, false);
EXPECT_FALSE(active_priority_tracker_.HasActivePriority());
EXPECT_FALSE(active_priority_tracker_.IsActive(kDefaultPriority));
}
TEST_F(ActivePriorityTrackerTest, HighestActivePriority) {
EXPECT_FALSE(active_priority_tracker_.HasActivePriority());
for (size_t i = 0; i < SequenceManager::PrioritySettings::kMaxPriorities;
i++) {
TaskQueue::QueuePriority priority =
static_cast<TaskQueue::QueuePriority>(i);
EXPECT_FALSE(active_priority_tracker_.IsActive(priority));
active_priority_tracker_.SetActive(priority, true);
EXPECT_TRUE(active_priority_tracker_.IsActive(priority));
}
for (size_t i = 0; i < SequenceManager::PrioritySettings::kMaxPriorities;
i++) {
EXPECT_TRUE(active_priority_tracker_.HasActivePriority());
TaskQueue::QueuePriority priority =
static_cast<TaskQueue::QueuePriority>(i);
EXPECT_EQ(active_priority_tracker_.HighestActivePriority(), priority);
active_priority_tracker_.SetActive(priority, false);
}
EXPECT_FALSE(active_priority_tracker_.HasActivePriority());
}
} // namespace task_queue_selector_unittest
} // namespace internal
} // namespace sequence_manager
} // namespace base