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
|
// 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/delayed_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 "testing/gtest/include/gtest/gtest.h"
namespace base::internal {
namespace {
class DelayedPriorityQueueWithSequencesTest : public testing::Test {
protected:
scoped_refptr<Sequence> MakeSequenceWithDelayedTask(
TimeDelta delayed_run_time) {
// FastForward time to ensure that queue order between task sources is well
// defined.
task_environment.FastForwardBy(Microseconds(1));
scoped_refptr<Sequence> sequence = MakeRefCounted<Sequence>(
TaskTraits(), nullptr, TaskSourceExecutionMode::kParallel);
sequence->BeginTransaction().PushDelayedTask(
Task(FROM_HERE, DoNothing(), TimeTicks::Now(), delayed_run_time));
return sequence;
}
void Push(scoped_refptr<Sequence> task_source) {
auto delayed_sort_key = task_source->GetDelayedSortKey();
pq.Push(std::move(task_source), delayed_sort_key);
}
test::TaskEnvironment task_environment{
test::TaskEnvironment::TimeSource::MOCK_TIME};
scoped_refptr<Sequence> sequence_a =
MakeSequenceWithDelayedTask(Milliseconds(90));
TimeTicks sort_key_a = sequence_a->GetDelayedSortKey();
scoped_refptr<Sequence> sequence_b =
MakeSequenceWithDelayedTask(Milliseconds(70));
TimeTicks sort_key_b = sequence_b->GetDelayedSortKey();
scoped_refptr<Sequence> sequence_c =
MakeSequenceWithDelayedTask(Milliseconds(80));
TimeTicks sort_key_c = sequence_c->GetDelayedSortKey();
scoped_refptr<Sequence> sequence_d =
MakeSequenceWithDelayedTask(Milliseconds(100));
TimeTicks sort_key_d = sequence_d->GetDelayedSortKey();
DelayedPriorityQueue pq;
};
} // namespace
TEST_F(DelayedPriorityQueueWithSequencesTest, PushPopPeek) {
EXPECT_TRUE(pq.IsEmpty());
// Push |sequence_a| in the DelayedPriorityQueue. It becomes the sequence with
// the earliest delayed runtime.
Push(sequence_a);
EXPECT_EQ(sort_key_a, pq.PeekDelayedSortKey());
// Push |sequence_b| in the DelayedPriorityQueue. It becomes the sequence with
// the earliest delayed runtime.
Push(sequence_b);
EXPECT_EQ(sort_key_b, pq.PeekDelayedSortKey());
// Push |sequence_c| in the DelayedPriorityQueue. |sequence_b| is still the
// sequence with the earliest delayed runtime.
Push(sequence_c);
EXPECT_EQ(sort_key_b, pq.PeekDelayedSortKey());
// Push |sequence_d| in the DelayedPriorityQueue. |sequence_b| is still the
// sequence with the earliest delayed runtime.
Push(sequence_d);
EXPECT_EQ(sort_key_b, pq.PeekDelayedSortKey());
// Pop |sequence_b| from the DelayedPriorityQueue. |sequence_c| becomes the
// sequence with the earliest delayed runtime.
EXPECT_EQ(sequence_b, pq.PopTaskSource());
EXPECT_EQ(sort_key_c, pq.PeekDelayedSortKey());
// Pop |sequence_c| from the DelayedPriorityQueue. |sequence_a| becomes the
// sequence with the earliest delayed runtime.
EXPECT_EQ(sequence_c, pq.PopTaskSource());
EXPECT_EQ(sort_key_a, pq.PeekDelayedSortKey());
// Pop |sequence_a| from the DelayedPriorityQueue. |sequence_d| becomes the
// sequence with the earliest delayed runtime.
EXPECT_EQ(sequence_a, pq.PopTaskSource());
EXPECT_EQ(sort_key_d, pq.PeekDelayedSortKey());
// Pop |sequence_d| from the DelayedPriorityQueue. It is now empty.
EXPECT_EQ(sequence_d, pq.PopTaskSource());
EXPECT_TRUE(pq.IsEmpty());
}
TEST_F(DelayedPriorityQueueWithSequencesTest, 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.PeekDelayedSortKey());
// Remove |sequence_a| from the PriorityQueue. |sequence_b| is still the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(sequence_a));
EXPECT_EQ(sort_key_b, pq.PeekDelayedSortKey());
// RemoveTaskSource() should return false if called on a sequence not in the
// PriorityQueue.
EXPECT_FALSE(pq.RemoveTaskSource(sequence_a));
// Remove |sequence_b| from the PriorityQueue. |sequence_c| becomes the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(sequence_b));
EXPECT_EQ(sort_key_c, pq.PeekDelayedSortKey());
// Remove |sequence_d| from the PriorityQueue. |sequence_c| is still the
// sequence with the highest priority.
EXPECT_TRUE(pq.RemoveTaskSource(sequence_d));
EXPECT_EQ(sort_key_c, pq.PeekDelayedSortKey());
// Remove |sequence_c| from the PriorityQueue, making it empty.
EXPECT_TRUE(pq.RemoveTaskSource(sequence_c));
EXPECT_TRUE(pq.IsEmpty());
// Return false if RemoveTaskSource() is called on an empty PriorityQueue.
EXPECT_FALSE(pq.RemoveTaskSource(sequence_c));
}
// Test that when the top of a task source changes, the delayed queue is
// appropriately rearranged
TEST_F(DelayedPriorityQueueWithSequencesTest, UpdateDelayedSortKey) {
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.PeekDelayedSortKey());
{
// Push a new delayed task earlier than pq's top into sequence_c.
// |sequence_c| becomes the sequence on top of the delayed priority queue.
sequence_c->BeginTransaction().PushDelayedTask(
Task(FROM_HERE, DoNothing(), TimeTicks::Now(), Milliseconds(60)));
sort_key_c = sequence_c->GetDelayedSortKey();
pq.UpdateDelayedSortKey(sequence_c);
EXPECT_EQ(sort_key_c, pq.PeekDelayedSortKey());
}
{
// Push a new delayed task earlier than pq's top into sequence_d.
// |sequence_d| becomes the sequence on top of the delayed priority queue.
sequence_d->BeginTransaction().PushDelayedTask(
Task(FROM_HERE, DoNothing(), TimeTicks::Now(), Milliseconds(50)));
sort_key_d = sequence_d->GetDelayedSortKey();
pq.UpdateDelayedSortKey(sequence_d);
EXPECT_EQ(sort_key_d, pq.PeekDelayedSortKey());
// Pop top task source and verify that it is |sequence_d|
EXPECT_EQ(sequence_d, pq.PopTaskSource());
// New top should be |sequence_c|
EXPECT_EQ(sort_key_c, pq.PeekDelayedSortKey());
}
{
EXPECT_EQ(sequence_c, pq.PopTaskSource());
EXPECT_EQ(sequence_b, pq.PopTaskSource());
EXPECT_EQ(sequence_a, pq.PopTaskSource());
}
{
// No-op if UpdateSortKey() is called on an empty PriorityQueue.
pq.UpdateDelayedSortKey(sequence_b);
EXPECT_TRUE(pq.IsEmpty());
}
}
} // namespace base::internal
|