Commit 4a5340df authored by Greg Kraynov's avatar Greg Kraynov Committed by Commit Bot

Prune cancelled pending tasks in TestMockTimeTaskRunner.

Change-Id: Id9829fbc88f7df9171625886c0d94ef7ae97b60a
Reviewed-on: https://chromium-review.googlesource.com/1042291Reviewed-by: default avatarBalazs Engedy <engedy@chromium.org>
Reviewed-by: default avatarDerek Cheng <imcheng@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Greg Kraynov <kraynov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556898}
parent db64731f
......@@ -251,27 +251,45 @@ TestMockTimeTaskRunner::TakePendingTasks() {
while (!tasks_.empty()) {
// It's safe to remove const and consume |task| here, since |task| is not
// used for ordering the item.
if (!tasks_.top().task.IsCancelled()) {
tasks.push_back(
std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())));
}
tasks_.pop();
}
return tasks;
}
bool TestMockTimeTaskRunner::HasPendingTask() const {
bool TestMockTimeTaskRunner::HasPendingTask() {
DCHECK(thread_checker_.CalledOnValidThread());
AutoLock scoped_lock(tasks_lock_);
while (!tasks_.empty() && tasks_.top().task.IsCancelled())
tasks_.pop();
return !tasks_.empty();
}
size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
size_t TestMockTimeTaskRunner::GetPendingTaskCount() {
DCHECK(thread_checker_.CalledOnValidThread());
AutoLock scoped_lock(tasks_lock_);
TaskPriorityQueue preserved_tasks;
while (!tasks_.empty()) {
if (!tasks_.top().task.IsCancelled()) {
preserved_tasks.push(
std::move(const_cast<TestOrderedPendingTask&>(tasks_.top())));
}
tasks_.pop();
}
tasks_.swap(preserved_tasks);
return tasks_.size();
}
TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() {
DCHECK(thread_checker_.CalledOnValidThread());
AutoLock scoped_lock(tasks_lock_);
while (!tasks_.empty() && tasks_.top().task.IsCancelled())
tasks_.pop();
return tasks_.empty() ? TimeDelta::Max()
: tasks_.top().GetTimeToRun() - NowTicks();
: tasks_.top().GetTimeToRun() - now_ticks_;
}
// TODO(gab): Combine |thread_checker_| with a SequenceToken to differentiate
......@@ -329,6 +347,8 @@ void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
TestPendingTask task_info;
if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
break;
if (task_info.task.IsCancelled())
continue;
// If tasks were posted with a negative delay, task_info.GetTimeToRun() will
// be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
// moving the clock backwards in this case.
......
......@@ -171,10 +171,11 @@ class TestMockTimeTaskRunner : public SingleThreadTaskRunner,
std::unique_ptr<TickClock> DeprecatedGetMockTickClock() const;
const TickClock* GetMockTickClock() const;
// Cancelled pending tasks get pruned automatically.
base::circular_deque<TestPendingTask> TakePendingTasks();
bool HasPendingTask() const;
size_t GetPendingTaskCount() const;
TimeDelta NextPendingTaskDelay() const;
bool HasPendingTask();
size_t GetPendingTaskCount();
TimeDelta NextPendingTaskDelay();
// SingleThreadTaskRunner:
bool RunsTasksInCurrentSequence() const override;
......
......@@ -4,6 +4,7 @@
#include "base/test/test_mock_time_task_runner.h"
#include "base/cancelable_callback.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
......@@ -196,4 +197,54 @@ TEST(TestMockTimeTaskRunnerTest, RunLoopQuitFromIdle) {
run_loop.Run();
}
TEST(TestMockTimeTaskRunnerTest, TakePendingTasks) {
auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
task_runner->PostTask(FROM_HERE, Bind([]() {}));
EXPECT_TRUE(task_runner->HasPendingTask());
EXPECT_EQ(1u, task_runner->TakePendingTasks().size());
EXPECT_FALSE(task_runner->HasPendingTask());
}
TEST(TestMockTimeTaskRunnerTest, CancelPendingTask) {
auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
CancelableClosure task1(Bind([]() {}));
task_runner->PostDelayedTask(FROM_HERE, task1.callback(),
TimeDelta::FromSeconds(1));
EXPECT_TRUE(task_runner->HasPendingTask());
EXPECT_EQ(1u, task_runner->GetPendingTaskCount());
EXPECT_EQ(TimeDelta::FromSeconds(1), task_runner->NextPendingTaskDelay());
task1.Cancel();
EXPECT_FALSE(task_runner->HasPendingTask());
CancelableClosure task2(Bind([]() {}));
task_runner->PostDelayedTask(FROM_HERE, task2.callback(),
TimeDelta::FromSeconds(1));
task2.Cancel();
EXPECT_EQ(0u, task_runner->GetPendingTaskCount());
CancelableClosure task3(Bind([]() {}));
task_runner->PostDelayedTask(FROM_HERE, task3.callback(),
TimeDelta::FromSeconds(1));
task3.Cancel();
EXPECT_EQ(TimeDelta::Max(), task_runner->NextPendingTaskDelay());
CancelableClosure task4(Bind([]() {}));
task_runner->PostDelayedTask(FROM_HERE, task4.callback(),
TimeDelta::FromSeconds(1));
task4.Cancel();
EXPECT_TRUE(task_runner->TakePendingTasks().empty());
}
TEST(TestMockTimeTaskRunnerTest, NoFastForwardToCancelledTask) {
auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
TimeTicks start_time = task_runner->NowTicks();
CancelableClosure task(Bind([]() {}));
task_runner->PostDelayedTask(FROM_HERE, task.callback(),
TimeDelta::FromSeconds(1));
EXPECT_EQ(TimeDelta::FromSeconds(1), task_runner->NextPendingTaskDelay());
task.Cancel();
task_runner->FastForwardUntilNoTasksRemain();
EXPECT_EQ(start_time, task_runner->NowTicks());
}
} // namespace base
......@@ -137,13 +137,12 @@ TEST_F(IssueManagerTest, IssueAutoDismissNoopsIfAlreadyCleared) {
ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(&observer));
EXPECT_CALL(observer, OnIssuesCleared()).Times(1);
EXPECT_TRUE(task_runner_->HasPendingTask());
manager_.ClearIssue(issue1.id());
EXPECT_CALL(observer, OnIssuesCleared()).Times(0);
base::TimeDelta timeout = IssueManager::GetAutoDismissTimeout(issue_info1);
EXPECT_FALSE(timeout.is_zero());
EXPECT_TRUE(task_runner_->HasPendingTask());
task_runner_->FastForwardBy(timeout);
EXPECT_FALSE(task_runner_->HasPendingTask());
}
......
......@@ -185,6 +185,11 @@ class AffiliatedMatchHelperTest : public testing::Test {
AffiliatedMatchHelper::kInitializationDelayOnStartup);
}
void ExpectNoDeferredTasks() {
mock_time_task_runner_->RunUntilIdle();
ASSERT_FALSE(mock_time_task_runner_->HasPendingTask());
}
void RunUntilIdle() {
// TODO(gab): Add support for base::RunLoop().RunUntilIdle() in scope of
// ScopedMockTimeMessageLoopTaskRunner and use it instead of this helper
......@@ -666,7 +671,7 @@ TEST_F(AffiliatedMatchHelperTest, DestroyBeforeDeferredInitialization) {
match_helper()->Initialize();
RunUntilIdle();
DestroyMatchHelper();
ASSERT_NO_FATAL_FAILURE(RunDeferredInitialization());
ASSERT_NO_FATAL_FAILURE(ExpectNoDeferredTasks());
}
} // namespace password_manager
......@@ -411,7 +411,8 @@ TEST_F(AffiliationFetchThrottlerTest, InstanceDestroyedWhileInBackoff) {
throttler->SignalNetworkRequestNeeded();
throttler.reset();
EXPECT_EQ(1u, GetPendingTaskCount());
// We expect the task to be cancelled.
EXPECT_EQ(0u, GetPendingTaskCount());
AssertNoReleaseUntilNoTasksRemain();
}
......
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