OLD | NEW |
---|---|
(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/memory/ptr_util.h" | |
10 #include "base/memory/weak_ptr.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 weak_ptr_factory_.GetWeakPtr())); | |
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 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().
| |
60 }; | |
61 | |
62 TEST_F(PolicySchedulerTest, Run) { | |
63 scheduler_ = base::MakeUnique<PolicyScheduler>( | |
64 base::BindRepeating(&PolicySchedulerTest::DoTask, | |
65 weak_ptr_factory_.GetWeakPtr()), | |
66 base::BindRepeating(&PolicySchedulerTest::OnTaskDone, | |
67 weak_ptr_factory_.GetWeakPtr()), | |
68 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.
| |
69 | |
70 base::RunLoop().RunUntilIdle(); | |
71 EXPECT_EQ(1, done_counter_); | |
72 } | |
73 | |
74 TEST_F(PolicySchedulerTest, Loop) { | |
75 scheduler_ = base::MakeUnique<PolicyScheduler>( | |
76 base::BindRepeating(&PolicySchedulerTest::DoTask, | |
77 weak_ptr_factory_.GetWeakPtr()), | |
78 base::BindRepeating(&PolicySchedulerTest::OnTaskDone, | |
79 weak_ptr_factory_.GetWeakPtr()), | |
80 base::TimeDelta(), base::TimeDelta()); | |
81 | |
82 base::RunLoop().RunUntilIdle(); | |
83 EXPECT_EQ(5, done_counter_); | |
84 } | |
85 | |
86 TEST_F(PolicySchedulerTest, Reschedule) { | |
87 scheduler_ = base::MakeUnique<PolicyScheduler>( | |
88 base::BindRepeating(&PolicySchedulerTest::DoTask, | |
89 weak_ptr_factory_.GetWeakPtr()), | |
90 base::BindRepeating(&PolicySchedulerTest::OnTaskDone, | |
91 weak_ptr_factory_.GetWeakPtr()), | |
92 base::TimeDelta::Max(), base::TimeDelta()); | |
93 | |
94 base::RunLoop().RunUntilIdle(); | |
95 EXPECT_EQ(1, done_counter_); | |
96 | |
97 // Delayed action is not run. | |
98 scheduler_->ScheduleTask(base::TimeDelta::Max()); | |
99 base::RunLoop().RunUntilIdle(); | |
100 EXPECT_EQ(1, done_counter_); | |
101 | |
102 // Rescheduling with 0 delay causes it to run. | |
103 scheduler_->ScheduleTask(base::TimeDelta()); | |
104 base::RunLoop().RunUntilIdle(); | |
105 EXPECT_EQ(2, done_counter_); | |
106 } | |
107 | |
108 TEST_F(PolicySchedulerTest, OverlappingTasks) { | |
109 scheduler_ = base::MakeUnique<PolicyScheduler>( | |
110 base::BindRepeating(&PolicySchedulerTest::CaptureCallbackForSlowTask, | |
111 weak_ptr_factory_.GetWeakPtr()), | |
112 base::BindRepeating(&PolicySchedulerTest::OnTaskDone, | |
113 weak_ptr_factory_.GetWeakPtr()), | |
114 base::TimeDelta::Max(), base::TimeDelta()); | |
115 | |
116 base::RunLoop().RunUntilIdle(); | |
117 EXPECT_EQ(1, do_counter_); | |
118 EXPECT_EQ(0, done_counter_); | |
119 | |
120 // Second action doesn't start while first is still pending. | |
121 scheduler_->ScheduleTask(base::TimeDelta()); | |
122 base::RunLoop().RunUntilIdle(); | |
123 EXPECT_EQ(1, do_counter_); | |
124 EXPECT_EQ(0, done_counter_); | |
125 | |
126 // After first action has finished, the second is started. | |
127 PostSlowTaskCallback(); | |
128 base::RunLoop().RunUntilIdle(); | |
129 EXPECT_EQ(2, do_counter_); | |
130 EXPECT_EQ(1, done_counter_); | |
131 | |
132 // Let the second action finish. | |
133 PostSlowTaskCallback(); | |
134 base::RunLoop().RunUntilIdle(); | |
135 EXPECT_EQ(2, do_counter_); | |
136 EXPECT_EQ(2, done_counter_); | |
137 } | |
138 | |
139 } // namespace policy | |
OLD | NEW |