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

Side by Side Diff: components/policy/core/common/policy_scheduler_unittest.cc

Issue 2942373002: Extract AD policy scheduler into separate class (Closed)
Patch Set: Simplify public interface (and implementation) Created 3 years, 6 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 unified diff | Download patch
« no previous file with comments | « components/policy/core/common/policy_scheduler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/policy/core/common/policy_scheduler.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h"
12 #include "base/test/scoped_task_environment.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace policy {
17
18 class PolicySchedulerTest : public testing::Test {
19 public:
20 void DoTask(PolicyScheduler::TaskCallback callback) {
21 do_counter_++;
22 base::ThreadTaskRunnerHandle::Get()->PostTask(
23 FROM_HERE, base::BindOnce(std::move(callback), true));
24 }
25
26 void OnTaskDone(bool success) {
27 done_counter_++;
28
29 // Terminate PolicyScheduler after 5 iterations.
30 if (done_counter_ >= 5) {
31 base::ThreadTaskRunnerHandle::Get()->PostTask(
32 FROM_HERE, base::BindOnce(&PolicySchedulerTest::Terminate,
33 base::Unretained(this)));
34 }
35 }
36
37 // To simulate a slow task the callback is captured instead of running it.
38 void CaptureCallbackForSlowTask(PolicyScheduler::TaskCallback callback) {
39 do_counter_++;
40 slow_callback_ = std::move(callback);
41 }
42
43 // Runs the captured callback to simulate the end of the slow task.
44 void PostSlowTaskCallback() {
45 base::ThreadTaskRunnerHandle::Get()->PostTask(
46 FROM_HERE, base::BindOnce(std::move(slow_callback_), true));
47 }
48
49 void Terminate() { scheduler_.reset(); }
50
51 protected:
52 int do_counter_ = 0;
53 int done_counter_ = 0;
54 std::unique_ptr<PolicyScheduler> scheduler_;
55
56 PolicyScheduler::TaskCallback slow_callback_;
57
58 base::test::ScopedTaskEnvironment scoped_task_environment_;
59 };
60
61 TEST_F(PolicySchedulerTest, Run) {
62 scheduler_ = base::MakeUnique<PolicyScheduler>(
63 base::BindRepeating(&PolicySchedulerTest::DoTask, base::Unretained(this)),
64 base::BindRepeating(&PolicySchedulerTest::OnTaskDone,
65 base::Unretained(this)),
66 base::TimeDelta::Max());
67
68 base::RunLoop().RunUntilIdle();
69 EXPECT_EQ(1, done_counter_);
70 }
71
72 TEST_F(PolicySchedulerTest, Loop) {
73 scheduler_ = base::MakeUnique<PolicyScheduler>(
74 base::BindRepeating(&PolicySchedulerTest::DoTask, base::Unretained(this)),
75 base::BindRepeating(&PolicySchedulerTest::OnTaskDone,
76 base::Unretained(this)),
77 base::TimeDelta());
78
79 base::RunLoop().RunUntilIdle();
80 EXPECT_EQ(5, done_counter_);
81 }
82
83 TEST_F(PolicySchedulerTest, Reschedule) {
84 scheduler_ = base::MakeUnique<PolicyScheduler>(
85 base::BindRepeating(&PolicySchedulerTest::DoTask, base::Unretained(this)),
86 base::BindRepeating(&PolicySchedulerTest::OnTaskDone,
87 base::Unretained(this)),
88 base::TimeDelta::Max());
89
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(1, done_counter_);
92
93 // Delayed action is not run.
94 base::RunLoop().RunUntilIdle();
95 EXPECT_EQ(1, done_counter_);
96
97 // Rescheduling with 0 delay causes it to run.
98 scheduler_->ScheduleTaskNow();
99 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(2, done_counter_);
101 }
102
103 TEST_F(PolicySchedulerTest, OverlappingTasks) {
104 scheduler_ = base::MakeUnique<PolicyScheduler>(
105 base::BindRepeating(&PolicySchedulerTest::CaptureCallbackForSlowTask,
106 base::Unretained(this)),
107 base::BindRepeating(&PolicySchedulerTest::OnTaskDone,
108 base::Unretained(this)),
109 base::TimeDelta::Max());
110
111 base::RunLoop().RunUntilIdle();
112 EXPECT_EQ(1, do_counter_);
113 EXPECT_EQ(0, done_counter_);
114
115 // Second action doesn't start while first is still pending.
116 scheduler_->ScheduleTaskNow();
117 base::RunLoop().RunUntilIdle();
118 EXPECT_EQ(1, do_counter_);
119 EXPECT_EQ(0, done_counter_);
120
121 // After first action has finished, the second is started.
122 PostSlowTaskCallback();
123 base::RunLoop().RunUntilIdle();
124 EXPECT_EQ(2, do_counter_);
125 EXPECT_EQ(1, done_counter_);
126
127 // Let the second action finish.
128 PostSlowTaskCallback();
129 base::RunLoop().RunUntilIdle();
130 EXPECT_EQ(2, do_counter_);
131 EXPECT_EQ(2, done_counter_);
132 }
133
134 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/policy_scheduler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698