Commit d5697a0b authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[ThreadPool]: Use mock time in priority queue unittests.

To avoid having 2 tasks with same queue_time, each PushTask is preceded by
a FastForward().

Bug: 998477
Change-Id: Ibecca4edf6d4c53420c0584676356284681d4868
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1778925
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692217}
parent 5b9afffb
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/task/thread_pool/sequence.h" #include "base/task/thread_pool/sequence.h"
#include "base/task/thread_pool/task.h" #include "base/task/thread_pool/task.h"
#include "base/test/gtest_util.h" #include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -45,6 +46,14 @@ class PriorityQueueWithSequencesTest : public testing::Test { ...@@ -45,6 +46,14 @@ class PriorityQueueWithSequencesTest : public testing::Test {
num_user_blocking); num_user_blocking);
} }
void Push(scoped_refptr<TaskSource> task_source) {
// FastForward time to ensure that queue order between task sources is well
// defined.
task_environment.FastForwardBy(TimeDelta::FromMicroseconds(1));
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource(
RegisteredTaskSource::CreateForTesting(std::move(task_source))));
}
scoped_refptr<TaskSource> sequence_a = MakeSequenceWithTraitsAndTask( scoped_refptr<TaskSource> sequence_a = MakeSequenceWithTraitsAndTask(
TaskTraits(ThreadPool(), TaskPriority::USER_VISIBLE)); TaskTraits(ThreadPool(), TaskPriority::USER_VISIBLE));
SequenceSortKey sort_key_a = sequence_a->BeginTransaction().GetSortKey(); SequenceSortKey sort_key_a = sequence_a->BeginTransaction().GetSortKey();
...@@ -62,6 +71,8 @@ class PriorityQueueWithSequencesTest : public testing::Test { ...@@ -62,6 +71,8 @@ class PriorityQueueWithSequencesTest : public testing::Test {
SequenceSortKey sort_key_d = sequence_d->BeginTransaction().GetSortKey(); SequenceSortKey sort_key_d = sequence_d->BeginTransaction().GetSortKey();
PriorityQueue pq; PriorityQueue pq;
test::TaskEnvironment task_environment{
test::ScopedTaskEnvironment::TimeSource::MOCK_TIME};
}; };
} // namespace } // namespace
...@@ -72,29 +83,25 @@ TEST_F(PriorityQueueWithSequencesTest, PushPopPeek) { ...@@ -72,29 +83,25 @@ TEST_F(PriorityQueueWithSequencesTest, PushPopPeek) {
// Push |sequence_a| in the PriorityQueue. It becomes the sequence with the // Push |sequence_a| in the PriorityQueue. It becomes the sequence with the
// highest priority. // highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_a);
RegisteredTaskSource::CreateForTesting(sequence_a)));
EXPECT_EQ(sort_key_a, pq.PeekSortKey()); EXPECT_EQ(sort_key_a, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 0U); ExpectNumSequences(0U, 1U, 0U);
// Push |sequence_b| in the PriorityQueue. It becomes the sequence with the // Push |sequence_b| in the PriorityQueue. It becomes the sequence with the
// highest priority. // highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_b);
RegisteredTaskSource::CreateForTesting(sequence_b)));
EXPECT_EQ(sort_key_b, pq.PeekSortKey()); EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 1U); ExpectNumSequences(0U, 1U, 1U);
// Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence // Push |sequence_c| in the PriorityQueue. |sequence_b| is still the sequence
// with the highest priority. // with the highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_c);
RegisteredTaskSource::CreateForTesting(sequence_c)));
EXPECT_EQ(sort_key_b, pq.PeekSortKey()); EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(0U, 1U, 2U); ExpectNumSequences(0U, 1U, 2U);
// Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence // Push |sequence_d| in the PriorityQueue. |sequence_b| is still the sequence
// with the highest priority. // with the highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_d);
RegisteredTaskSource::CreateForTesting(sequence_d)));
EXPECT_EQ(sort_key_b, pq.PeekSortKey()); EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U); ExpectNumSequences(1U, 1U, 2U);
...@@ -127,14 +134,10 @@ TEST_F(PriorityQueueWithSequencesTest, RemoveSequence) { ...@@ -127,14 +134,10 @@ TEST_F(PriorityQueueWithSequencesTest, RemoveSequence) {
// Push all test Sequences into the PriorityQueue. |sequence_b| // Push all test Sequences into the PriorityQueue. |sequence_b|
// will be the sequence with the highest priority. // will be the sequence with the highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_a);
RegisteredTaskSource::CreateForTesting(sequence_a))); Push(sequence_b);
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_c);
RegisteredTaskSource::CreateForTesting(sequence_b))); Push(sequence_d);
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource(
RegisteredTaskSource::CreateForTesting(sequence_c)));
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource(
RegisteredTaskSource::CreateForTesting(sequence_d)));
EXPECT_EQ(sort_key_b, pq.PeekSortKey()); EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U); ExpectNumSequences(1U, 1U, 2U);
...@@ -176,14 +179,10 @@ TEST_F(PriorityQueueWithSequencesTest, UpdateSortKey) { ...@@ -176,14 +179,10 @@ TEST_F(PriorityQueueWithSequencesTest, UpdateSortKey) {
// Push all test Sequences into the PriorityQueue. |sequence_b| becomes the // Push all test Sequences into the PriorityQueue. |sequence_b| becomes the
// sequence with the highest priority. // sequence with the highest priority.
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_a);
RegisteredTaskSource::CreateForTesting(sequence_a))); Push(sequence_b);
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource( Push(sequence_c);
RegisteredTaskSource::CreateForTesting(sequence_b))); Push(sequence_d);
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource(
RegisteredTaskSource::CreateForTesting(sequence_c)));
pq.Push(TransactionWithRegisteredTaskSource::FromTaskSource(
RegisteredTaskSource::CreateForTesting(sequence_d)));
EXPECT_EQ(sort_key_b, pq.PeekSortKey()); EXPECT_EQ(sort_key_b, pq.PeekSortKey());
ExpectNumSequences(1U, 1U, 2U); ExpectNumSequences(1U, 1U, 2U);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment