Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Unified Diff: base/task_scheduler/delayed_task_manager_unittest.cc

Issue 2405243003: TaskScheduler: Replace the SchedulerServiceThread with a base::Thread. (Closed)
Patch Set: CR robliao #9 Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/task_scheduler/delayed_task_manager_unittest.cc
diff --git a/base/task_scheduler/delayed_task_manager_unittest.cc b/base/task_scheduler/delayed_task_manager_unittest.cc
index c1c85ef5e8b3e847f9dc59337da37bfb4a9231e1..d79947b74f3c6e130868526b02f383a8794b0ebb 100644
--- a/base/task_scheduler/delayed_task_manager_unittest.cc
+++ b/base/task_scheduler/delayed_task_manager_unittest.cc
@@ -8,13 +8,12 @@
#include <utility>
#include "base/bind.h"
-#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
-#include "base/task_scheduler/scheduler_lock.h"
#include "base/task_scheduler/scheduler_worker_pool.h"
#include "base/task_scheduler/sequence.h"
#include "base/task_scheduler/task.h"
+#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -23,25 +22,7 @@ namespace base {
namespace internal {
namespace {
-class TestDelayedTaskManager : public DelayedTaskManager {
- public:
- TestDelayedTaskManager()
- : DelayedTaskManager(
- Bind(&TestDelayedTaskManager::OnDelayedRunTimeUpdated,
- Unretained(this))) {}
-
- void SetCurrentTime(TimeTicks now) { now_ = now; }
-
- // DelayedTaskManager:
- TimeTicks Now() const override { return now_; }
-
- MOCK_METHOD0(OnDelayedRunTimeUpdated, void());
-
- private:
- TimeTicks now_ = TimeTicks::Now();
-
- DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager);
-};
+constexpr TimeDelta kLongDelay = TimeDelta::FromHours(1);
class MockSchedulerWorkerPool : public SchedulerWorkerPool {
public:
@@ -79,166 +60,93 @@ class MockSchedulerWorkerPool : public SchedulerWorkerPool {
} // namespace
-// Verify that GetDelayedRunTime() returns a null TimeTicks when there are
-// no pending delayed tasks.
-TEST(TaskSchedulerDelayedTaskManagerTest,
- GetDelayedRunTimeNoPendingDelayedTasks) {
- TestDelayedTaskManager manager;
- EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
-}
-
-// Verify that a delayed task isn't posted before it is ripe for execution.
-TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTaskBeforeDelayedRunTime) {
- testing::StrictMock<TestDelayedTaskManager> manager;
-
- std::unique_ptr<Task> task(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
- const Task* task_raw = task.get();
- scoped_refptr<Sequence> sequence(new Sequence);
- testing::StrictMock<MockSchedulerWorkerPool> worker_pool;
-
- // Add |task| to the DelayedTaskManager.
- EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
- manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
-
- // Ask the DelayedTaskManager to post tasks that are ripe for execution. Don't
- // expect any call to the mock method of |worker_pool|.
- manager.PostReadyTasks();
-
- // The delayed run time shouldn't have changed.
- EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
-}
-
-// Verify that a delayed task is posted when PostReadyTasks() is called with the
-// current time equal to the task's delayed run time.
-TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAtDelayedRunTime) {
- testing::StrictMock<TestDelayedTaskManager> manager;
+// Verify that a delayed task isn't forwarded to its SchedulerWorkerPool before
+// it is ripe for execution.
+TEST(TaskSchedulerDelayedTaskManagerTest, DelayedTaskDoesNotRunTooEarly) {
+ scoped_refptr<TestMockTimeTaskRunner> service_thread_task_runner(
+ new TestMockTimeTaskRunner);
+ DelayedTaskManager manager(service_thread_task_runner);
std::unique_ptr<Task> task(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
- const Task* task_raw = task.get();
+ new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay));
scoped_refptr<Sequence> sequence(new Sequence);
testing::StrictMock<MockSchedulerWorkerPool> worker_pool;
- // Add |task| to the DelayedTaskManager.
- EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
+ // Send |task| to the DelayedTaskManager.
manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
-
- // Fast-forward time to |task_raw|'s delayed run time.
- manager.SetCurrentTime(task_raw->delayed_run_time);
- // Ask the DelayedTaskManager to post tasks that are ripe for execution.
- EXPECT_CALL(worker_pool,
- PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr));
- manager.PostReadyTasks();
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
+ // Run tasks that are ripe for execution on the service thread. Don't expect
gab 2016/10/17 19:09:18 "service thread" here is confusing because this te
fdoray 2016/10/18 20:10:49 Done.
+ // any call to the mock method of |worker_pool|.
+ service_thread_task_runner->RunUntilIdle();
}
-// Verify that a delayed task is posted when PostReadyTasks() is called with the
-// current time greater than the task's delayed run time.
-TEST(TaskSchedulerDelayedTaskManagerTest, PostReadyTasksAfterDelayedRunTime) {
- testing::StrictMock<TestDelayedTaskManager> manager;
+// Verify that a delayed task is forwarded to its SchedulerWorkerPool when it is
+// ripe for execution.
+TEST(TaskSchedulerDelayedTaskManagerTest, DelayedTaskRunsAfterDelay) {
+ scoped_refptr<TestMockTimeTaskRunner> service_thread_task_runner(
+ new TestMockTimeTaskRunner);
+ DelayedTaskManager manager(service_thread_task_runner);
std::unique_ptr<Task> task(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
+ new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), kLongDelay));
const Task* task_raw = task.get();
scoped_refptr<Sequence> sequence(new Sequence);
testing::StrictMock<MockSchedulerWorkerPool> worker_pool;
- // Add |task| to the DelayedTaskManager.
- EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
+ // Send |task| to the DelayedTaskManager.
manager.AddDelayedTask(std::move(task), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_raw->delayed_run_time, manager.GetDelayedRunTime());
- // Fast-forward time to |task_raw|'s delayed run time.
- manager.SetCurrentTime(task_raw->delayed_run_time +
- TimeDelta::FromSeconds(10));
-
- // Ask the DelayedTaskManager to post tasks that are ripe for execution.
+ // Fast-forward time. Expect a call to the mock method of |worker_pool|.
EXPECT_CALL(worker_pool,
PostTaskWithSequenceNowMock(task_raw, sequence.get(), nullptr));
- manager.PostReadyTasks();
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
+ service_thread_task_runner->FastForwardBy(kLongDelay);
}
-// Verify that when multiple tasks are added to a DelayedTaskManager, they are
-// posted when they become ripe for execution.
-TEST(TaskSchedulerDelayedTaskManagerTest, AddAndPostReadyTasks) {
- testing::StrictMock<TestDelayedTaskManager> manager;
+// Verify that multiple delayed task are forwarded to their SchedulerWorkerPool
+// when they are ripe for execution.
+TEST(TaskSchedulerDelayedTaskManagerTest, DelayedTasksRunAfterDelay) {
+ scoped_refptr<TestMockTimeTaskRunner> service_thread_task_runner(
+ new TestMockTimeTaskRunner);
+ DelayedTaskManager manager(service_thread_task_runner);
scoped_refptr<Sequence> sequence(new Sequence);
testing::StrictMock<MockSchedulerWorkerPool> worker_pool;
- std::unique_ptr<Task> task_a(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task_a->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2);
+ std::unique_ptr<Task> task_a(new Task(FROM_HERE, Bind(&DoNothing),
+ TaskTraits(), TimeDelta::FromHours(1)));
const Task* task_a_raw = task_a.get();
- std::unique_ptr<Task> task_b(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task_b->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(2);
+ std::unique_ptr<Task> task_b(new Task(FROM_HERE, Bind(&DoNothing),
+ TaskTraits(), TimeDelta::FromHours(2)));
const Task* task_b_raw = task_b.get();
- std::unique_ptr<Task> task_c(
- new Task(FROM_HERE, Bind(&DoNothing), TaskTraits(), TimeDelta()));
- task_c->delayed_run_time = manager.Now() + TimeDelta::FromSeconds(1);
+ std::unique_ptr<Task> task_c(new Task(FROM_HERE, Bind(&DoNothing),
+ TaskTraits(), TimeDelta::FromHours(1)));
const Task* task_c_raw = task_c.get();
- // Add |task_a| to the DelayedTaskManager. The delayed run time should be
- // updated to |task_a|'s delayed run time.
- EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
+ // Send tasks to the DelayedTaskManager.
manager.AddDelayedTask(std::move(task_a), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime());
-
- // Add |task_b| to the DelayedTaskManager. The delayed run time shouldn't
- // change.
manager.AddDelayedTask(std::move(task_b), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime());
-
- // Add |task_c| to the DelayedTaskManager. The delayed run time should be
- // updated to |task_c|'s delayed run time.
- EXPECT_CALL(manager, OnDelayedRunTimeUpdated());
manager.AddDelayedTask(std::move(task_c), sequence, nullptr, &worker_pool);
- testing::Mock::VerifyAndClear(&manager);
- EXPECT_EQ(task_c_raw->delayed_run_time, manager.GetDelayedRunTime());
- // Fast-forward time to |task_c_raw|'s delayed run time.
- manager.SetCurrentTime(task_c_raw->delayed_run_time);
+ // Run tasks that are ripe for execution on the service thread. Don't expect
+ // any call to the mock method of |worker_pool|.
+ service_thread_task_runner->RunUntilIdle();
- // Ask the DelayedTaskManager to post tasks that are ripe for execution.
- // |task_c_raw| should be posted and the delayed run time should become
- // |task_a_raw|'s delayed run time.
+ // Fast-forward time. Expect |task_a_raw| and |task_c_raw| to be forwarded to
+ // the worker pool.
+ EXPECT_CALL(worker_pool,
+ PostTaskWithSequenceNowMock(task_a_raw, sequence.get(), nullptr));
EXPECT_CALL(worker_pool,
PostTaskWithSequenceNowMock(task_c_raw, sequence.get(), nullptr));
- manager.PostReadyTasks();
+ service_thread_task_runner->FastForwardBy(TimeDelta::FromHours(1));
testing::Mock::VerifyAndClear(&worker_pool);
- EXPECT_EQ(task_a_raw->delayed_run_time, manager.GetDelayedRunTime());
-
- // Fast-forward time to |task_a_raw|'s delayed run time.
- manager.SetCurrentTime(task_a_raw->delayed_run_time);
- // Ask the DelayedTaskManager to post tasks that are ripe for execution.
- // |task_a_raw| and |task_b_raw| should be posted and the delayed run time
- // should become a null TimeTicks.
- EXPECT_CALL(worker_pool,
- PostTaskWithSequenceNowMock(task_a_raw, sequence.get(), nullptr));
+ // Fast-forward time. Expect |task_b_raw| to be forwarded to the worker pool.
EXPECT_CALL(worker_pool,
PostTaskWithSequenceNowMock(task_b_raw, sequence.get(), nullptr));
- manager.PostReadyTasks();
+ service_thread_task_runner->FastForwardBy(TimeDelta::FromHours(1));
testing::Mock::VerifyAndClear(&worker_pool);
- EXPECT_EQ(TimeTicks(), manager.GetDelayedRunTime());
}
} // namespace internal

Powered by Google App Engine
This is Rietveld 408576698