OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "components/policy/core/common/remote_commands/remote_commands_queue.h" |
| 6 |
5 #include <string> | 7 #include <string> |
| 8 #include <utility> |
6 | 9 |
7 #include "base/macros.h" | 10 #include "base/macros.h" |
8 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
9 #include "base/test/test_mock_time_task_runner.h" | 12 #include "base/test/test_mock_time_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
11 #include "base/time/tick_clock.h" | 14 #include "base/time/tick_clock.h" |
12 #include "base/time/time.h" | 15 #include "base/time/time.h" |
13 #include "components/policy/core/common/remote_commands/remote_command_job.h" | 16 #include "components/policy/core/common/remote_commands/remote_command_job.h" |
14 #include "components/policy/core/common/remote_commands/remote_commands_queue.h" | |
15 #include "components/policy/core/common/remote_commands/test_remote_command_job.
h" | 17 #include "components/policy/core/common/remote_commands/test_remote_command_job.
h" |
16 #include "policy/proto/device_management_backend.pb.h" | 18 #include "policy/proto/device_management_backend.pb.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace policy { | 22 namespace policy { |
21 | 23 |
22 namespace em = enterprise_management; | 24 namespace em = enterprise_management; |
23 | 25 |
24 namespace { | 26 namespace { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 : task_runner_(new base::TestMockTimeTaskRunner()), | 104 : task_runner_(new base::TestMockTimeTaskRunner()), |
103 clock_(nullptr), | 105 clock_(nullptr), |
104 runner_handle_(task_runner_) { | 106 runner_handle_(task_runner_) { |
105 } | 107 } |
106 | 108 |
107 void RemoteCommandsQueueTest::SetUp() { | 109 void RemoteCommandsQueueTest::SetUp() { |
108 scoped_ptr<base::TickClock> clock(task_runner_->GetMockTickClock()); | 110 scoped_ptr<base::TickClock> clock(task_runner_->GetMockTickClock()); |
109 test_start_time_ = clock->NowTicks(); | 111 test_start_time_ = clock->NowTicks(); |
110 | 112 |
111 clock_ = clock.get(); | 113 clock_ = clock.get(); |
112 queue_.SetClockForTesting(clock.Pass()); | 114 queue_.SetClockForTesting(std::move(clock)); |
113 queue_.AddObserver(&observer_); | 115 queue_.AddObserver(&observer_); |
114 } | 116 } |
115 | 117 |
116 void RemoteCommandsQueueTest::TearDown() { | 118 void RemoteCommandsQueueTest::TearDown() { |
117 queue_.RemoveObserver(&observer_); | 119 queue_.RemoveObserver(&observer_); |
118 } | 120 } |
119 | 121 |
120 void RemoteCommandsQueueTest::InitializeJob( | 122 void RemoteCommandsQueueTest::InitializeJob( |
121 RemoteCommandJob* job, | 123 RemoteCommandJob* job, |
122 RemoteCommandJob::UniqueIDType unique_id, | 124 RemoteCommandJob::UniqueIDType unique_id, |
(...skipping 26 matching lines...) Expand all Loading... |
149 Mock::VerifyAndClearExpectations(&observer_); | 151 Mock::VerifyAndClearExpectations(&observer_); |
150 | 152 |
151 const base::TimeTicks now = clock_->NowTicks(); | 153 const base::TimeTicks now = clock_->NowTicks(); |
152 | 154 |
153 // Add the job to the queue. It should start executing immediately. | 155 // Add the job to the queue. It should start executing immediately. |
154 EXPECT_CALL( | 156 EXPECT_CALL( |
155 observer_, | 157 observer_, |
156 OnJobStarted( | 158 OnJobStarted( |
157 AllOf(Property(&RemoteCommandJob::status, RemoteCommandJob::RUNNING), | 159 AllOf(Property(&RemoteCommandJob::status, RemoteCommandJob::RUNNING), |
158 Property(&RemoteCommandJob::execution_started_time, now)))); | 160 Property(&RemoteCommandJob::execution_started_time, now)))); |
159 queue_.AddJob(job.Pass()); | 161 queue_.AddJob(std::move(job)); |
160 Mock::VerifyAndClearExpectations(&observer_); | 162 Mock::VerifyAndClearExpectations(&observer_); |
161 | 163 |
162 // After |delta|, the job should still be running. | 164 // After |delta|, the job should still be running. |
163 task_runner_->FastForwardBy(delta); | 165 task_runner_->FastForwardBy(delta); |
164 Mock::VerifyAndClearExpectations(&observer_); | 166 Mock::VerifyAndClearExpectations(&observer_); |
165 } | 167 } |
166 | 168 |
167 void RemoteCommandsQueueTest::VerifyCommandIssuedTime( | 169 void RemoteCommandsQueueTest::VerifyCommandIssuedTime( |
168 RemoteCommandJob* job, | 170 RemoteCommandJob* job, |
169 base::TimeTicks expected_issued_time) { | 171 base::TimeTicks expected_issued_time) { |
170 // Maximum possible error can be 1 millisecond due to truncating. | 172 // Maximum possible error can be 1 millisecond due to truncating. |
171 EXPECT_GE(expected_issued_time, job->issued_time()); | 173 EXPECT_GE(expected_issued_time, job->issued_time()); |
172 EXPECT_LE(expected_issued_time - base::TimeDelta::FromMilliseconds(1), | 174 EXPECT_LE(expected_issued_time - base::TimeDelta::FromMilliseconds(1), |
173 job->issued_time()); | 175 job->issued_time()); |
174 } | 176 } |
175 | 177 |
176 TEST_F(RemoteCommandsQueueTest, SingleSucceedCommand) { | 178 TEST_F(RemoteCommandsQueueTest, SingleSucceedCommand) { |
177 // Initialize a job expected to succeed after 5 seconds, from a protobuf with | 179 // Initialize a job expected to succeed after 5 seconds, from a protobuf with |
178 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. | 180 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. |
179 scoped_ptr<RemoteCommandJob> job( | 181 scoped_ptr<RemoteCommandJob> job( |
180 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); | 182 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); |
181 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); | 183 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); |
182 | 184 |
183 AddJobAndVerifyRunningAfter(job.Pass(), base::TimeDelta::FromSeconds(4)); | 185 AddJobAndVerifyRunningAfter(std::move(job), base::TimeDelta::FromSeconds(4)); |
184 | 186 |
185 // After 6 seconds, the job is expected to be finished. | 187 // After 6 seconds, the job is expected to be finished. |
186 EXPECT_CALL(observer_, | 188 EXPECT_CALL(observer_, |
187 OnJobFinished(AllOf(Property(&RemoteCommandJob::status, | 189 OnJobFinished(AllOf(Property(&RemoteCommandJob::status, |
188 RemoteCommandJob::SUCCEEDED), | 190 RemoteCommandJob::SUCCEEDED), |
189 Property(&RemoteCommandJob::GetResultPayload, | 191 Property(&RemoteCommandJob::GetResultPayload, |
190 Pointee(StrEq(kPayload)))))); | 192 Pointee(StrEq(kPayload)))))); |
191 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | 193 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
192 Mock::VerifyAndClearExpectations(&observer_); | 194 Mock::VerifyAndClearExpectations(&observer_); |
193 | 195 |
194 task_runner_->FastForwardUntilNoTasksRemain(); | 196 task_runner_->FastForwardUntilNoTasksRemain(); |
195 } | 197 } |
196 | 198 |
197 TEST_F(RemoteCommandsQueueTest, SingleFailedCommand) { | 199 TEST_F(RemoteCommandsQueueTest, SingleFailedCommand) { |
198 // Initialize a job expected to fail after 10 seconds, from a protobuf with | 200 // Initialize a job expected to fail after 10 seconds, from a protobuf with |
199 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. | 201 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. |
200 scoped_ptr<RemoteCommandJob> job( | 202 scoped_ptr<RemoteCommandJob> job( |
201 new TestRemoteCommandJob(false, base::TimeDelta::FromSeconds(10))); | 203 new TestRemoteCommandJob(false, base::TimeDelta::FromSeconds(10))); |
202 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); | 204 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); |
203 | 205 |
204 AddJobAndVerifyRunningAfter(job.Pass(), base::TimeDelta::FromSeconds(9)); | 206 AddJobAndVerifyRunningAfter(std::move(job), base::TimeDelta::FromSeconds(9)); |
205 | 207 |
206 // After 11 seconds, the job is expected to be finished. | 208 // After 11 seconds, the job is expected to be finished. |
207 EXPECT_CALL(observer_, | 209 EXPECT_CALL(observer_, |
208 OnJobFinished(AllOf( | 210 OnJobFinished(AllOf( |
209 Property(&RemoteCommandJob::status, RemoteCommandJob::FAILED), | 211 Property(&RemoteCommandJob::status, RemoteCommandJob::FAILED), |
210 Property(&RemoteCommandJob::GetResultPayload, | 212 Property(&RemoteCommandJob::GetResultPayload, |
211 Pointee(StrEq(kPayload)))))); | 213 Pointee(StrEq(kPayload)))))); |
212 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | 214 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
213 Mock::VerifyAndClearExpectations(&observer_); | 215 Mock::VerifyAndClearExpectations(&observer_); |
214 | 216 |
215 task_runner_->FastForwardUntilNoTasksRemain(); | 217 task_runner_->FastForwardUntilNoTasksRemain(); |
216 } | 218 } |
217 | 219 |
218 TEST_F(RemoteCommandsQueueTest, SingleTerminatedCommand) { | 220 TEST_F(RemoteCommandsQueueTest, SingleTerminatedCommand) { |
219 // Initialize a job expected to fail after 200 seconds, from a protobuf with | 221 // Initialize a job expected to fail after 200 seconds, from a protobuf with |
220 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. | 222 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. |
221 scoped_ptr<RemoteCommandJob> job( | 223 scoped_ptr<RemoteCommandJob> job( |
222 new TestRemoteCommandJob(false, base::TimeDelta::FromSeconds(200))); | 224 new TestRemoteCommandJob(false, base::TimeDelta::FromSeconds(200))); |
223 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); | 225 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); |
224 | 226 |
225 AddJobAndVerifyRunningAfter(job.Pass(), base::TimeDelta::FromSeconds(179)); | 227 AddJobAndVerifyRunningAfter(std::move(job), |
| 228 base::TimeDelta::FromSeconds(179)); |
226 | 229 |
227 // After 181 seconds, the job is expected to be terminated (3 minutes is the | 230 // After 181 seconds, the job is expected to be terminated (3 minutes is the |
228 // timeout duration). | 231 // timeout duration). |
229 EXPECT_CALL(observer_, OnJobFinished(Property(&RemoteCommandJob::status, | 232 EXPECT_CALL(observer_, OnJobFinished(Property(&RemoteCommandJob::status, |
230 RemoteCommandJob::TERMINATED))); | 233 RemoteCommandJob::TERMINATED))); |
231 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | 234 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
232 Mock::VerifyAndClearExpectations(&observer_); | 235 Mock::VerifyAndClearExpectations(&observer_); |
233 | 236 |
234 task_runner_->FastForwardUntilNoTasksRemain(); | 237 task_runner_->FastForwardUntilNoTasksRemain(); |
235 } | 238 } |
(...skipping 13 matching lines...) Expand all Loading... |
249 // |kUniqueID| and |test_start_time_ - 4 hours|. | 252 // |kUniqueID| and |test_start_time_ - 4 hours|. |
250 scoped_ptr<RemoteCommandJob> job( | 253 scoped_ptr<RemoteCommandJob> job( |
251 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(10))); | 254 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(10))); |
252 InitializeJob(job.get(), kUniqueID, | 255 InitializeJob(job.get(), kUniqueID, |
253 test_start_time_ - base::TimeDelta::FromHours(4), | 256 test_start_time_ - base::TimeDelta::FromHours(4), |
254 std::string()); | 257 std::string()); |
255 | 258 |
256 // Add the job to the queue. It should not be started. | 259 // Add the job to the queue. It should not be started. |
257 EXPECT_CALL(observer_, OnJobFinished(Property(&RemoteCommandJob::status, | 260 EXPECT_CALL(observer_, OnJobFinished(Property(&RemoteCommandJob::status, |
258 RemoteCommandJob::EXPIRED))); | 261 RemoteCommandJob::EXPIRED))); |
259 queue_.AddJob(job.Pass()); | 262 queue_.AddJob(std::move(job)); |
260 Mock::VerifyAndClearExpectations(&observer_); | 263 Mock::VerifyAndClearExpectations(&observer_); |
261 | 264 |
262 task_runner_->FastForwardUntilNoTasksRemain(); | 265 task_runner_->FastForwardUntilNoTasksRemain(); |
263 } | 266 } |
264 | 267 |
265 TEST_F(RemoteCommandsQueueTest, TwoCommands) { | 268 TEST_F(RemoteCommandsQueueTest, TwoCommands) { |
266 InSequence sequence; | 269 InSequence sequence; |
267 | 270 |
268 // Initialize a job expected to succeed after 5 seconds, from a protobuf with | 271 // Initialize a job expected to succeed after 5 seconds, from a protobuf with |
269 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. | 272 // |kUniqueID|, |kPayload| and |test_start_time_| as command issued time. |
270 scoped_ptr<RemoteCommandJob> job( | 273 scoped_ptr<RemoteCommandJob> job( |
271 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); | 274 new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); |
272 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); | 275 InitializeJob(job.get(), kUniqueID, test_start_time_, kPayload); |
273 | 276 |
274 // Add the job to the queue, should start executing immediately. Pass the | 277 // Add the job to the queue, should start executing immediately. Pass the |
275 // ownership of |job| as well. | 278 // ownership of |job| as well. |
276 EXPECT_CALL( | 279 EXPECT_CALL( |
277 observer_, | 280 observer_, |
278 OnJobStarted(AllOf( | 281 OnJobStarted(AllOf( |
279 Property(&RemoteCommandJob::unique_id, kUniqueID), | 282 Property(&RemoteCommandJob::unique_id, kUniqueID), |
280 Property(&RemoteCommandJob::status, RemoteCommandJob::RUNNING)))); | 283 Property(&RemoteCommandJob::status, RemoteCommandJob::RUNNING)))); |
281 queue_.AddJob(job.Pass()); | 284 queue_.AddJob(std::move(job)); |
282 Mock::VerifyAndClearExpectations(&observer_); | 285 Mock::VerifyAndClearExpectations(&observer_); |
283 | 286 |
284 // Initialize another job expected to succeed after 5 seconds, from a protobuf | 287 // Initialize another job expected to succeed after 5 seconds, from a protobuf |
285 // with |kUniqueID2|, |kPayload2| and |test_start_time_ + 1s| as command | 288 // with |kUniqueID2|, |kPayload2| and |test_start_time_ + 1s| as command |
286 // issued time. | 289 // issued time. |
287 job.reset(new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); | 290 job.reset(new TestRemoteCommandJob(true, base::TimeDelta::FromSeconds(5))); |
288 InitializeJob(job.get(), kUniqueID2, | 291 InitializeJob(job.get(), kUniqueID2, |
289 test_start_time_ + base::TimeDelta::FromSeconds(1), kPayload2); | 292 test_start_time_ + base::TimeDelta::FromSeconds(1), kPayload2); |
290 | 293 |
291 // After 2 seconds, add the second job. It should be queued and not start | 294 // After 2 seconds, add the second job. It should be queued and not start |
292 // running immediately. | 295 // running immediately. |
293 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | 296 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
294 queue_.AddJob(job.Pass()); | 297 queue_.AddJob(std::move(job)); |
295 | 298 |
296 // After 4 seconds, nothing happens. | 299 // After 4 seconds, nothing happens. |
297 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | 300 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
298 Mock::VerifyAndClearExpectations(&observer_); | 301 Mock::VerifyAndClearExpectations(&observer_); |
299 | 302 |
300 // After 6 seconds, the first job should finish running and the second one | 303 // After 6 seconds, the first job should finish running and the second one |
301 // start immediately after that. | 304 // start immediately after that. |
302 EXPECT_CALL( | 305 EXPECT_CALL( |
303 observer_, | 306 observer_, |
304 OnJobFinished(AllOf( | 307 OnJobFinished(AllOf( |
(...skipping 17 matching lines...) Expand all Loading... |
322 Property(&RemoteCommandJob::status, RemoteCommandJob::SUCCEEDED), | 325 Property(&RemoteCommandJob::status, RemoteCommandJob::SUCCEEDED), |
323 Property(&RemoteCommandJob::GetResultPayload, | 326 Property(&RemoteCommandJob::GetResultPayload, |
324 Pointee(StrEq(kPayload2)))))); | 327 Pointee(StrEq(kPayload2)))))); |
325 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(5)); | 328 task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(5)); |
326 Mock::VerifyAndClearExpectations(&observer_); | 329 Mock::VerifyAndClearExpectations(&observer_); |
327 | 330 |
328 task_runner_->FastForwardUntilNoTasksRemain(); | 331 task_runner_->FastForwardUntilNoTasksRemain(); |
329 } | 332 } |
330 | 333 |
331 } // namespace policy | 334 } // namespace policy |
OLD | NEW |