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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/thread_pool/priority_queue.h"
#include <memory>
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool/sequence.h"
#include "base/task/thread_pool/task.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base::internal {
namespace {
class PriorityQueueWithSequencesTest : public testing::Test {
protected:
void ExpectNumSequences(size_t num_best_effort,
size_t num_user_visible,
size_t num_user_blocking) {
EXPECT_EQ(pq.GetNumTaskSourcesWithPriority(TaskPriority::BEST_EFFORT),
num_best_effort);
EXPECT_EQ(pq.GetNumTaskSourcesWithPriority(TaskPriority::USER_VISIBLE),
num_user_visible);
EXPECT_EQ(pq.GetNumTaskSourcesWithPriority(TaskPriority::USER_BLOCKING),
num_user_blocking);
}
scoped_refptr<TaskSource> MakeSequenceWithTraitsAndTask(
const TaskTraits& traits) {
// FastForward time to ensure that queue order between task sources is well
// defined.
task_environment.FastForwardBy(Microseconds(1));
scoped_refptr<Sequence> sequence = MakeRefCounted<Sequence>(
traits, nullptr, TaskSourceExecutionMode::kParallel);
auto transaction = sequence->BeginTransaction();
transaction.WillPushImmediateTask();
transaction.PushImmediateTask(
Task(FROM_HERE, DoNothing(), TimeTicks::Now(), TimeDelta()));
return sequence;
}
void Push(scoped_refptr<TaskSource> task_source) {
auto sort_key = task_source->GetSortKey();
pq.Push(RegisteredTaskSource::CreateForTesting(std::move(task_source)),
sort_key);
}
test::TaskEnvironment task_environment{
test::TaskEnvironment::TimeSource::MOCK_TIME};
scoped_refptr<TaskSource> sequence_a =
MakeSequenceWithTraitsAndTask(TaskTraits(TaskPriority::USER_VISIBLE));
TaskSourceSortKey sort_key_a = sequence_a->GetSortKey();
scoped_refptr<TaskSource> sequence_b =
MakeSequenceWithTraitsAndTask(TaskTraits(TaskPriority::USER_BLOCKING));
TaskSourceSortKey sort_key_b = sequence_b->GetSortKey();
scoped_refptr<TaskSource> sequence_c =
MakeSequenceWithTraitsAndTask(TaskTraits(TaskPriority::USER_BLOCKING));
TaskSourceSortKey sort_key_c = sequence_c->GetSortKey();
scoped_refptr<TaskSource> sequence_d =
MakeSequenceWithTraitsAndTask(TaskTraits(TaskPriority::BEST_EFFORT));
TaskSourceSortKey sort_key_d = sequence_d->GetSortKey();
PriorityQueue pq;
};
} // namespace
TEST_F(PriorityQueueWithSequencesTest, PushPopPeek) {
EXPECT_TRUE(pq.IsEmpty());
ExpectNumSequences(0U, 0U, 0U);
// Push |sequence_a| in the PriorityQueue. It becomes the sequence with the
// highest priority.
Push(sequence_a);
EXPECT_EQ(sort_key_a, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 0U);
// Push |sequence_b| in the PriorityQueue. It becomes the sequence with the
// highest priority.
Push(sequence_b);
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 1U);
// Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence
// with the highest priority.
Push(sequence_c);
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 2U);
// Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence
// with the highest priority.
Push(sequence_d);
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U);
// Pop |sequence_b| from the PriorityQueue. |sequence_c| becomes the sequence
// with the highest priority.
EXPECT_EQ(sequence_b, pq.PopTaskSource().Unregister());
EXPECT_EQ(sort_key_c, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 1U);
// Pop |sequence_c| from the PriorityQueue. |sequence_a| becomes the sequence
// with the highest priority.
EXPECT_EQ(sequence_c, pq.PopTaskSource().Unregister());
EXPECT_EQ(sort_key_a, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 0U);
// Pop |sequence_a| from the PriorityQueue. |sequence_d| becomes the sequence
// with the highest priority.
EXPECT_EQ(sequence_a, pq.PopTaskSource().Unregister());
EXPECT_EQ(sort_key_d, pq.PeekSortKey());
ExpectNumSequences(1U, 0U, 0U);
// Pop |sequence_d| from the PriorityQueue. It is now empty.
EXPECT_EQ(sequence_d, pq.PopTaskSource().Unregister());
EXPECT_TRUE(pq.IsEmpty());
ExpectNumSequences(0U, 0U, 0U);
}
TEST_F(PriorityQueueWithSequencesTest, RemoveSequence) {
EXPECT_TRUE(pq.IsEmpty());
// Push all test Sequences into the PriorityQueue. |sequence_b|
// will be the sequence with the highest priority.
Push(sequence_a);
Push(sequence_b);
Push(sequence_c);
Push(sequence_d);
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U);
// Remove |sequence_a| from the PriorityQueue. |sequence_b| is still the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(*sequence_a).Unregister());
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 0U, 2U);
// RemoveTaskSource() should return false if called on a sequence not in the
// PriorityQueue.
EXPECT_FALSE(pq.RemoveTaskSource(*sequence_a).Unregister());
ExpectNumSequences(1U, 0U, 2U);
// Remove |sequence_b| from the PriorityQueue. |sequence_c| becomes the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(*sequence_b).Unregister());
EXPECT_EQ(sort_key_c, pq.PeekSortKey());
ExpectNumSequences(1U, 0U, 1U);
// Remove |sequence_d| from the PriorityQueue. |sequence_c| is still the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(*sequence_d).Unregister());
EXPECT_EQ(sort_key_c, pq.PeekSortKey());
ExpectNumSequences(0U, 0U, 1U);
// Remove |sequence_c| from the PriorityQueue, making it empty.
EXPECT_TRUE(pq.RemoveTaskSource(*sequence_c).Unregister());
EXPECT_TRUE(pq.IsEmpty());
ExpectNumSequences(0U, 0U, 0U);
// Return false if RemoveTaskSource() is called on an empty PriorityQueue.
EXPECT_FALSE(pq.RemoveTaskSource(*sequence_c).Unregister());
ExpectNumSequences(0U, 0U, 0U);
}
TEST_F(PriorityQueueWithSequencesTest, UpdateSortKey) {
EXPECT_TRUE(pq.IsEmpty());
// Push all test Sequences into the PriorityQueue. |sequence_b| becomes the
// sequence with the highest priority.
Push(sequence_a);
Push(sequence_b);
Push(sequence_c);
Push(sequence_d);
EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U);
{
// Downgrade |sequence_b| from USER_BLOCKING to BEST_EFFORT. |sequence_c|
// (USER_BLOCKING priority) becomes the sequence with the highest priority.
auto sequence_b_transaction = sequence_b->BeginTransaction();
sequence_b_transaction.UpdatePriority(TaskPriority::BEST_EFFORT);
pq.UpdateSortKey(*sequence_b, sequence_b->GetSortKey());
EXPECT_EQ(sort_key_c, pq.PeekSortKey());
ExpectNumSequences(2U, 1U, 1U);
}
{
// Update |sequence_c|'s sort key to one with the same priority.
// |sequence_c| (USER_BLOCKING priority) is still the sequence with the
// highest priority.
auto sequence_c_transaction = sequence_c->BeginTransaction();
sequence_c_transaction.UpdatePriority(TaskPriority::USER_BLOCKING);
pq.UpdateSortKey(*sequence_c, sequence_c->GetSortKey());
ExpectNumSequences(2U, 1U, 1U);
// Note: |sequence_c| is popped for comparison as |sort_key_c| becomes
// obsolete. |sequence_a| (USER_VISIBLE priority) becomes the sequence with
// the highest priority.
EXPECT_EQ(sequence_c, pq.PopTaskSource().Unregister());
EXPECT_EQ(sort_key_a, pq.PeekSortKey());
ExpectNumSequences(2U, 1U, 0U);
}
{
// Upgrade |sequence_d| from BEST_EFFORT to USER_BLOCKING. |sequence_d|
// becomes the sequence with the highest priority.
auto sequence_d_and_transaction = sequence_d->BeginTransaction();
sequence_d_and_transaction.UpdatePriority(TaskPriority::USER_BLOCKING);
pq.UpdateSortKey(*sequence_d, sequence_d->GetSortKey());
ExpectNumSequences(1U, 1U, 1U);
// Note: |sequence_d| is popped for comparison as |sort_key_d| becomes
// obsolete.
EXPECT_EQ(sequence_d, pq.PopTaskSource().Unregister());
// No-op if UpdateSortKey() is called on a Sequence not in the
// PriorityQueue.
EXPECT_EQ(sort_key_a, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 0U);
}
{
pq.UpdateSortKey(*sequence_d, sequence_d->GetSortKey());
ExpectNumSequences(1U, 1U, 0U);
EXPECT_EQ(sequence_a, pq.PopTaskSource().Unregister());
ExpectNumSequences(1U, 0U, 0U);
EXPECT_EQ(sequence_b, pq.PopTaskSource().Unregister());
ExpectNumSequences(0U, 0U, 0U);
}
{
// No-op if UpdateSortKey() is called on an empty PriorityQueue.
pq.UpdateSortKey(*sequence_b, sequence_b->GetSortKey());
EXPECT_TRUE(pq.IsEmpty());
ExpectNumSequences(0U, 0U, 0U);
}
}
} // namespace base::internal
|