Chromium Code Reviews| Index: components/policy/core/common/policy_scheduler_unittest.cc |
| diff --git a/components/policy/core/common/policy_scheduler_unittest.cc b/components/policy/core/common/policy_scheduler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a60e6f18e1174769e1ce5ae4a43da47a330c287 |
| --- /dev/null |
| +++ b/components/policy/core/common/policy_scheduler_unittest.cc |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/policy/core/common/policy_scheduler.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "base/test/scoped_task_environment.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace policy { |
| + |
| +class PolicySchedulerTest : public testing::Test { |
| + public: |
| + void DoTask(PolicyScheduler::TaskCallback callback) { |
| + do_counter_++; |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::BindOnce(std::move(callback), true)); |
| + } |
| + |
| + void OnTaskDone(bool success) { |
| + done_counter_++; |
| + |
| + // Terminate PolicyScheduler after 5 iterations. |
| + if (done_counter_ >= 5) { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::BindOnce(&PolicySchedulerTest::Terminate, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + } |
| + |
| + // To simulate a slow task the callback is captured instead of running it. |
| + void CaptureCallbackForSlowTask(PolicyScheduler::TaskCallback callback) { |
| + do_counter_++; |
| + slow_callback_ = std::move(callback); |
| + } |
| + |
| + // Runs the captured callback to simulate the end of the slow task. |
| + void PostSlowTaskCallback() { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::BindOnce(std::move(slow_callback_), true)); |
| + } |
| + |
| + void Terminate() { scheduler_.reset(); } |
| + |
| + protected: |
| + int do_counter_ = 0; |
| + int done_counter_ = 0; |
| + std::unique_ptr<PolicyScheduler> scheduler_; |
| + |
| + PolicyScheduler::TaskCallback slow_callback_; |
| + |
| + base::test::ScopedTaskEnvironment scoped_task_environment_; |
| + base::WeakPtrFactory<PolicySchedulerTest> weak_ptr_factory_{this}; |
|
pmarko
2017/06/19 10:14:37
Technically, WeakPtr is not necessary here because
Thiemo Nagel
2017/06/19 22:29:51
You're right. Changed to base::Unretained().
|
| +}; |
| + |
| +TEST_F(PolicySchedulerTest, Run) { |
| + scheduler_ = base::MakeUnique<PolicyScheduler>( |
| + base::BindRepeating(&PolicySchedulerTest::DoTask, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::BindRepeating(&PolicySchedulerTest::OnTaskDone, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::TimeDelta::Max(), base::TimeDelta()); |
|
pmarko
2017/06/19 10:14:37
Please consider adding /* interval */ and /* backo
Thiemo Nagel
2017/06/19 22:29:51
Good point. Done.
|
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, done_counter_); |
| +} |
| + |
| +TEST_F(PolicySchedulerTest, Loop) { |
| + scheduler_ = base::MakeUnique<PolicyScheduler>( |
| + base::BindRepeating(&PolicySchedulerTest::DoTask, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::BindRepeating(&PolicySchedulerTest::OnTaskDone, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::TimeDelta(), base::TimeDelta()); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(5, done_counter_); |
| +} |
| + |
| +TEST_F(PolicySchedulerTest, Reschedule) { |
| + scheduler_ = base::MakeUnique<PolicyScheduler>( |
| + base::BindRepeating(&PolicySchedulerTest::DoTask, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::BindRepeating(&PolicySchedulerTest::OnTaskDone, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::TimeDelta::Max(), base::TimeDelta()); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, done_counter_); |
| + |
| + // Delayed action is not run. |
| + scheduler_->ScheduleTask(base::TimeDelta::Max()); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, done_counter_); |
| + |
| + // Rescheduling with 0 delay causes it to run. |
| + scheduler_->ScheduleTask(base::TimeDelta()); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(2, done_counter_); |
| +} |
| + |
| +TEST_F(PolicySchedulerTest, OverlappingTasks) { |
| + scheduler_ = base::MakeUnique<PolicyScheduler>( |
| + base::BindRepeating(&PolicySchedulerTest::CaptureCallbackForSlowTask, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::BindRepeating(&PolicySchedulerTest::OnTaskDone, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::TimeDelta::Max(), base::TimeDelta()); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, do_counter_); |
| + EXPECT_EQ(0, done_counter_); |
| + |
| + // Second action doesn't start while first is still pending. |
| + scheduler_->ScheduleTask(base::TimeDelta()); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, do_counter_); |
| + EXPECT_EQ(0, done_counter_); |
| + |
| + // After first action has finished, the second is started. |
| + PostSlowTaskCallback(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(2, do_counter_); |
| + EXPECT_EQ(1, done_counter_); |
| + |
| + // Let the second action finish. |
| + PostSlowTaskCallback(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(2, do_counter_); |
| + EXPECT_EQ(2, done_counter_); |
| +} |
| + |
| +} // namespace policy |