OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/offline_pages/background/request_queue.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/test/test_simple_task_runner.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "components/offline_pages/background/device_conditions.h" | |
14 #include "components/offline_pages/background/offliner_policy.h" | |
15 #include "components/offline_pages/background/request_coordinator.h" | |
16 #include "components/offline_pages/background/request_coordinator_event_logger.h
" | |
17 #include "components/offline_pages/background/request_notifier.h" | |
18 #include "components/offline_pages/background/request_queue_in_memory_store.h" | |
19 #include "components/offline_pages/background/save_page_request.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace offline_pages { | |
23 | |
24 using AddRequestResult = AddRequestResult; | |
25 using GetRequestsResult = GetRequestsResult; | |
26 using UpdateRequestResult = UpdateRequestResult; | |
27 | |
28 namespace { | |
29 // Data for request 1. | |
30 const int64_t kRequestId = 42; | |
31 const GURL kUrl("http://example.com"); | |
32 const ClientId kClientId("bookmark", "1234"); | |
33 // Data for request 2. | |
34 const int64_t kRequestId2 = 77; | |
35 const GURL kUrl2("http://test.com"); | |
36 const ClientId kClientId2("bookmark", "567"); | |
37 const bool kUserRequested = true; | |
38 const int64_t kRequestId3 = 99; | |
39 const int kOneWeekInSeconds = 7 * 24 * 60 * 60; | |
40 | |
41 // Default request | |
42 const SavePageRequest kEmptyRequest(0UL, | |
43 GURL(""), | |
44 ClientId("", ""), | |
45 base::Time(), | |
46 true); | |
47 | |
48 } // namespace | |
49 | |
50 // Helper class needed by the PickRequestTask | |
51 class RequestNotifierStub : public RequestNotifier { | |
52 public: | |
53 RequestNotifierStub() | |
54 : last_expired_request_(kEmptyRequest), total_expired_requests_(0) {} | |
55 | |
56 void NotifyAdded(const SavePageRequest& request) override {} | |
57 void NotifyChanged(const SavePageRequest& request) override {} | |
58 | |
59 void NotifyCompleted(const SavePageRequest& request, | |
60 BackgroundSavePageResult status) override { | |
61 last_expired_request_ = request; | |
62 last_request_expiration_status_ = status; | |
63 total_expired_requests_++; | |
64 } | |
65 | |
66 const SavePageRequest& last_expired_request() { | |
67 return last_expired_request_; | |
68 } | |
69 | |
70 RequestCoordinator::BackgroundSavePageResult | |
71 last_request_expiration_status() { | |
72 return last_request_expiration_status_; | |
73 } | |
74 | |
75 int32_t total_expired_requests() { return total_expired_requests_; } | |
76 | |
77 private: | |
78 BackgroundSavePageResult last_request_expiration_status_; | |
79 SavePageRequest last_expired_request_; | |
80 int32_t total_expired_requests_; | |
81 }; | |
82 | |
83 // TODO(fgorski): Add tests for store failures in add/remove/get. | |
84 class RequestQueueTest : public testing::Test { | |
85 public: | |
86 RequestQueueTest(); | |
87 ~RequestQueueTest() override; | |
88 | |
89 // Test overrides. | |
90 void SetUp() override; | |
91 | |
92 void PumpLoop(); | |
93 | |
94 // Callback for adding requests. | |
95 void AddRequestDone(AddRequestResult result, const SavePageRequest& request); | |
96 // Callback for getting requests. | |
97 void GetRequestsDone(GetRequestsResult result, | |
98 std::vector<std::unique_ptr<SavePageRequest>> requests); | |
99 | |
100 void UpdateRequestDone(UpdateRequestResult result); | |
101 void UpdateRequestsDone(std::unique_ptr<UpdateRequestsResult> result); | |
102 | |
103 void ClearResults(); | |
104 | |
105 RequestQueue* queue() { return queue_.get(); } | |
106 | |
107 AddRequestResult last_add_result() const { return last_add_result_; } | |
108 SavePageRequest* last_added_request() { | |
109 return last_added_request_.get(); | |
110 } | |
111 | |
112 UpdateRequestResult last_update_result() const { return last_update_result_; } | |
113 | |
114 GetRequestsResult last_get_requests_result() const { | |
115 return last_get_requests_result_; | |
116 } | |
117 | |
118 const std::vector<std::unique_ptr<SavePageRequest>>& last_requests() const { | |
119 return last_requests_; | |
120 } | |
121 | |
122 UpdateRequestsResult* update_requests_result() const { | |
123 return update_requests_result_.get(); | |
124 } | |
125 | |
126 void RequestPickedCallback(const SavePageRequest& request) {} | |
127 void RequestNotPickedCallback(bool non_user_requested_tasks_remain) {} | |
128 void RequestCountCallback(size_t total_count, size_t available_count) {} | |
129 | |
130 private: | |
131 AddRequestResult last_add_result_; | |
132 std::unique_ptr<SavePageRequest> last_added_request_; | |
133 std::unique_ptr<UpdateRequestsResult> update_requests_result_; | |
134 UpdateRequestResult last_update_result_; | |
135 | |
136 GetRequestsResult last_get_requests_result_; | |
137 std::vector<std::unique_ptr<SavePageRequest>> last_requests_; | |
138 | |
139 std::unique_ptr<RequestQueue> queue_; | |
140 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
141 base::ThreadTaskRunnerHandle task_runner_handle_; | |
142 }; | |
143 | |
144 RequestQueueTest::RequestQueueTest() | |
145 : last_add_result_(AddRequestResult::STORE_FAILURE), | |
146 last_update_result_(UpdateRequestResult::STORE_FAILURE), | |
147 last_get_requests_result_(GetRequestsResult::STORE_FAILURE), | |
148 task_runner_(new base::TestSimpleTaskRunner), | |
149 task_runner_handle_(task_runner_) {} | |
150 | |
151 RequestQueueTest::~RequestQueueTest() {} | |
152 | |
153 void RequestQueueTest::SetUp() { | |
154 std::unique_ptr<RequestQueueInMemoryStore> store( | |
155 new RequestQueueInMemoryStore()); | |
156 queue_.reset(new RequestQueue(std::move(store))); | |
157 } | |
158 | |
159 void RequestQueueTest::PumpLoop() { | |
160 task_runner_->RunUntilIdle(); | |
161 } | |
162 | |
163 void RequestQueueTest::AddRequestDone(AddRequestResult result, | |
164 const SavePageRequest& request) { | |
165 last_add_result_ = result; | |
166 last_added_request_.reset(new SavePageRequest(request)); | |
167 } | |
168 | |
169 void RequestQueueTest::GetRequestsDone( | |
170 GetRequestsResult result, | |
171 std::vector<std::unique_ptr<SavePageRequest>> requests) { | |
172 last_get_requests_result_ = result; | |
173 last_requests_ = std::move(requests); | |
174 } | |
175 | |
176 void RequestQueueTest::UpdateRequestDone(UpdateRequestResult result) { | |
177 last_update_result_ = result; | |
178 } | |
179 | |
180 void RequestQueueTest::UpdateRequestsDone( | |
181 std::unique_ptr<UpdateRequestsResult> result) { | |
182 update_requests_result_ = std::move(result); | |
183 } | |
184 | |
185 void RequestQueueTest::ClearResults() { | |
186 last_add_result_ = AddRequestResult::STORE_FAILURE; | |
187 last_update_result_ = UpdateRequestResult::STORE_FAILURE; | |
188 last_get_requests_result_ = GetRequestsResult::STORE_FAILURE; | |
189 last_added_request_.reset(nullptr); | |
190 update_requests_result_.reset(nullptr); | |
191 last_requests_.clear(); | |
192 } | |
193 | |
194 TEST_F(RequestQueueTest, GetRequestsEmpty) { | |
195 queue()->GetRequests( | |
196 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
197 PumpLoop(); | |
198 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
199 ASSERT_EQ(0ul, last_requests().size()); | |
200 } | |
201 | |
202 TEST_F(RequestQueueTest, AddRequest) { | |
203 base::Time creation_time = base::Time::Now(); | |
204 SavePageRequest request( | |
205 kRequestId, kUrl, kClientId, creation_time, kUserRequested); | |
206 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
207 base::Unretained(this))); | |
208 PumpLoop(); | |
209 ASSERT_EQ(AddRequestResult::SUCCESS, last_add_result()); | |
210 ASSERT_TRUE(last_added_request()); | |
211 ASSERT_EQ(kRequestId, last_added_request()->request_id()); | |
212 | |
213 queue()->GetRequests( | |
214 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
215 PumpLoop(); | |
216 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
217 ASSERT_EQ(1ul, last_requests().size()); | |
218 } | |
219 | |
220 TEST_F(RequestQueueTest, RemoveRequest) { | |
221 base::Time creation_time = base::Time::Now(); | |
222 SavePageRequest request( | |
223 kRequestId, kUrl, kClientId, creation_time, kUserRequested); | |
224 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
225 base::Unretained(this))); | |
226 PumpLoop(); | |
227 ASSERT_EQ(kRequestId, last_added_request()->request_id()); | |
228 | |
229 std::vector<int64_t> remove_requests{kRequestId}; | |
230 queue()->RemoveRequests(remove_requests, | |
231 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
232 base::Unretained(this))); | |
233 PumpLoop(); | |
234 EXPECT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
235 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
236 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
237 update_requests_result()->item_statuses.at(0).second); | |
238 EXPECT_EQ(1UL, update_requests_result()->updated_items.size()); | |
239 EXPECT_EQ(request, update_requests_result()->updated_items.at(0)); | |
240 | |
241 queue()->GetRequests( | |
242 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
243 PumpLoop(); | |
244 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
245 ASSERT_EQ(0ul, last_requests().size()); | |
246 } | |
247 | |
248 TEST_F(RequestQueueTest, RemoveSeveralRequests) { | |
249 base::Time creation_time = base::Time::Now(); | |
250 SavePageRequest request(kRequestId, kUrl, kClientId, creation_time, | |
251 kUserRequested); | |
252 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
253 base::Unretained(this))); | |
254 PumpLoop(); | |
255 ASSERT_EQ(kRequestId, last_added_request()->request_id()); | |
256 | |
257 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time, | |
258 kUserRequested); | |
259 queue()->AddRequest(request2, base::Bind(&RequestQueueTest::AddRequestDone, | |
260 base::Unretained(this))); | |
261 PumpLoop(); | |
262 ASSERT_EQ(kRequestId2, last_added_request()->request_id()); | |
263 | |
264 std::vector<int64_t> remove_requests; | |
265 remove_requests.push_back(kRequestId); | |
266 remove_requests.push_back(kRequestId2); | |
267 remove_requests.push_back(kRequestId3); | |
268 queue()->RemoveRequests(remove_requests, | |
269 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
270 base::Unretained(this))); | |
271 PumpLoop(); | |
272 ASSERT_EQ(3ul, update_requests_result()->item_statuses.size()); | |
273 ASSERT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
274 ASSERT_EQ(ItemActionStatus::SUCCESS, | |
275 update_requests_result()->item_statuses.at(0).second); | |
276 ASSERT_EQ(kRequestId2, update_requests_result()->item_statuses.at(1).first); | |
277 ASSERT_EQ(ItemActionStatus::SUCCESS, | |
278 update_requests_result()->item_statuses.at(1).second); | |
279 ASSERT_EQ(kRequestId3, update_requests_result()->item_statuses.at(2).first); | |
280 ASSERT_EQ(ItemActionStatus::NOT_FOUND, | |
281 update_requests_result()->item_statuses.at(2).second); | |
282 EXPECT_EQ(2UL, update_requests_result()->updated_items.size()); | |
283 EXPECT_EQ(request, update_requests_result()->updated_items.at(0)); | |
284 EXPECT_EQ(request2, update_requests_result()->updated_items.at(1)); | |
285 | |
286 queue()->GetRequests( | |
287 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
288 PumpLoop(); | |
289 | |
290 // Verify both requests are no longer in the queue. | |
291 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
292 ASSERT_EQ(0ul, last_requests().size()); | |
293 } | |
294 | |
295 TEST_F(RequestQueueTest, PauseAndResume) { | |
296 base::Time creation_time = base::Time::Now(); | |
297 SavePageRequest request(kRequestId, kUrl, kClientId, creation_time, | |
298 kUserRequested); | |
299 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
300 base::Unretained(this))); | |
301 PumpLoop(); | |
302 ASSERT_EQ(kRequestId, last_added_request()->request_id()); | |
303 | |
304 queue()->GetRequests( | |
305 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
306 PumpLoop(); | |
307 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
308 ASSERT_EQ(1ul, last_requests().size()); | |
309 | |
310 std::vector<int64_t> request_ids; | |
311 request_ids.push_back(kRequestId); | |
312 | |
313 // Pause the request. | |
314 queue()->ChangeRequestsState(request_ids, | |
315 SavePageRequest::RequestState::PAUSED, | |
316 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
317 base::Unretained(this))); | |
318 PumpLoop(); | |
319 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
320 ASSERT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
321 ASSERT_EQ(ItemActionStatus::SUCCESS, | |
322 update_requests_result()->item_statuses.at(0).second); | |
323 ASSERT_EQ(1ul, update_requests_result()->updated_items.size()); | |
324 ASSERT_EQ(SavePageRequest::RequestState::PAUSED, | |
325 update_requests_result()->updated_items.at(0).request_state()); | |
326 | |
327 queue()->GetRequests( | |
328 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
329 PumpLoop(); | |
330 | |
331 // Verify the request is paused. | |
332 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
333 ASSERT_EQ(1ul, last_requests().size()); | |
334 ASSERT_EQ(SavePageRequest::RequestState::PAUSED, | |
335 last_requests().at(0)->request_state()); | |
336 | |
337 // Resume the request. | |
338 queue()->ChangeRequestsState(request_ids, | |
339 SavePageRequest::RequestState::AVAILABLE, | |
340 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
341 base::Unretained(this))); | |
342 PumpLoop(); | |
343 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
344 ASSERT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
345 ASSERT_EQ(ItemActionStatus::SUCCESS, | |
346 update_requests_result()->item_statuses.at(0).second); | |
347 ASSERT_EQ(1ul, update_requests_result()->updated_items.size()); | |
348 ASSERT_EQ(SavePageRequest::RequestState::AVAILABLE, | |
349 update_requests_result()->updated_items.at(0).request_state()); | |
350 | |
351 queue()->GetRequests( | |
352 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
353 PumpLoop(); | |
354 | |
355 // Verify the request is no longer paused. | |
356 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
357 ASSERT_EQ(1ul, last_requests().size()); | |
358 ASSERT_EQ(SavePageRequest::RequestState::AVAILABLE, | |
359 last_requests().at(0)->request_state()); | |
360 } | |
361 | |
362 // A longer test populating the request queue with more than one item, properly | |
363 // listing multiple items and removing the right item. | |
364 TEST_F(RequestQueueTest, MultipleRequestsAddGetRemove) { | |
365 base::Time creation_time = base::Time::Now(); | |
366 SavePageRequest request1( | |
367 kRequestId, kUrl, kClientId, creation_time, kUserRequested); | |
368 queue()->AddRequest(request1, base::Bind(&RequestQueueTest::AddRequestDone, | |
369 base::Unretained(this))); | |
370 PumpLoop(); | |
371 ASSERT_EQ(request1.request_id(), last_added_request()->request_id()); | |
372 SavePageRequest request2( | |
373 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); | |
374 queue()->AddRequest(request2, base::Bind(&RequestQueueTest::AddRequestDone, | |
375 base::Unretained(this))); | |
376 PumpLoop(); | |
377 ASSERT_EQ(request2.request_id(), last_added_request()->request_id()); | |
378 | |
379 queue()->GetRequests( | |
380 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
381 PumpLoop(); | |
382 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
383 ASSERT_EQ(2ul, last_requests().size()); | |
384 | |
385 std::vector<int64_t> remove_requests; | |
386 remove_requests.push_back(request1.request_id()); | |
387 queue()->RemoveRequests(remove_requests, | |
388 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
389 base::Unretained(this))); | |
390 PumpLoop(); | |
391 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
392 ASSERT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
393 ASSERT_EQ(ItemActionStatus::SUCCESS, | |
394 update_requests_result()->item_statuses.at(0).second); | |
395 | |
396 queue()->GetRequests( | |
397 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
398 PumpLoop(); | |
399 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
400 ASSERT_EQ(1ul, last_requests().size()); | |
401 ASSERT_EQ(request2.request_id(), last_requests().at(0)->request_id()); | |
402 } | |
403 | |
404 TEST_F(RequestQueueTest, MarkAttemptStarted) { | |
405 // First add a request. Retry count will be set to 0. | |
406 base::Time creation_time = base::Time::Now(); | |
407 SavePageRequest request(kRequestId, kUrl, kClientId, creation_time, | |
408 kUserRequested); | |
409 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
410 base::Unretained(this))); | |
411 PumpLoop(); | |
412 | |
413 base::Time before_time = base::Time::Now(); | |
414 // Update the request, ensure it succeeded. | |
415 queue()->MarkAttemptStarted(kRequestId, | |
416 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
417 base::Unretained(this))); | |
418 PumpLoop(); | |
419 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
420 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
421 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
422 update_requests_result()->item_statuses.at(0).second); | |
423 EXPECT_EQ(1UL, update_requests_result()->updated_items.size()); | |
424 EXPECT_LE(before_time, | |
425 update_requests_result()->updated_items.at(0).last_attempt_time()); | |
426 EXPECT_GE(base::Time::Now(), | |
427 update_requests_result()->updated_items.at(0).last_attempt_time()); | |
428 EXPECT_EQ( | |
429 1, update_requests_result()->updated_items.at(0).started_attempt_count()); | |
430 EXPECT_EQ(SavePageRequest::RequestState::OFFLINING, | |
431 update_requests_result()->updated_items.at(0).request_state()); | |
432 | |
433 queue()->GetRequests( | |
434 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
435 PumpLoop(); | |
436 EXPECT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | |
437 ASSERT_EQ(1ul, last_requests().size()); | |
438 EXPECT_EQ(update_requests_result()->updated_items.at(0), | |
439 *last_requests().at(0)); | |
440 } | |
441 | |
442 TEST_F(RequestQueueTest, MarkAttempStartedRequestNotPresent) { | |
443 // First add a request. Retry count will be set to 0. | |
444 base::Time creation_time = base::Time::Now(); | |
445 // This request is never put into the queue. | |
446 SavePageRequest request1(kRequestId, kUrl, kClientId, creation_time, | |
447 kUserRequested); | |
448 | |
449 queue()->MarkAttemptStarted(kRequestId, | |
450 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
451 base::Unretained(this))); | |
452 PumpLoop(); | |
453 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
454 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
455 EXPECT_EQ(ItemActionStatus::NOT_FOUND, | |
456 update_requests_result()->item_statuses.at(0).second); | |
457 EXPECT_EQ(0ul, update_requests_result()->updated_items.size()); | |
458 } | |
459 | |
460 TEST_F(RequestQueueTest, MarkAttemptAborted) { | |
461 base::Time creation_time = base::Time::Now(); | |
462 SavePageRequest request(kRequestId, kUrl, kClientId, creation_time, | |
463 kUserRequested); | |
464 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
465 base::Unretained(this))); | |
466 PumpLoop(); | |
467 | |
468 // Start request. | |
469 queue()->MarkAttemptStarted(kRequestId, | |
470 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
471 base::Unretained(this))); | |
472 PumpLoop(); | |
473 ClearResults(); | |
474 | |
475 queue()->MarkAttemptAborted(kRequestId, | |
476 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
477 base::Unretained(this))); | |
478 PumpLoop(); | |
479 | |
480 ASSERT_TRUE(update_requests_result()); | |
481 EXPECT_EQ(1UL, update_requests_result()->item_statuses.size()); | |
482 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
483 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
484 update_requests_result()->item_statuses.at(0).second); | |
485 EXPECT_EQ(1UL, update_requests_result()->updated_items.size()); | |
486 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, | |
487 update_requests_result()->updated_items.at(0).request_state()); | |
488 } | |
489 | |
490 TEST_F(RequestQueueTest, MarkAttemptAbortedRequestNotPresent) { | |
491 // First add a request. Retry count will be set to 0. | |
492 base::Time creation_time = base::Time::Now(); | |
493 // This request is never put into the queue. | |
494 SavePageRequest request1(kRequestId, kUrl, kClientId, creation_time, | |
495 kUserRequested); | |
496 | |
497 queue()->MarkAttemptAborted(kRequestId, | |
498 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
499 base::Unretained(this))); | |
500 PumpLoop(); | |
501 ASSERT_EQ(1ul, update_requests_result()->item_statuses.size()); | |
502 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
503 EXPECT_EQ(ItemActionStatus::NOT_FOUND, | |
504 update_requests_result()->item_statuses.at(0).second); | |
505 EXPECT_EQ(0ul, update_requests_result()->updated_items.size()); | |
506 } | |
507 | |
508 TEST_F(RequestQueueTest, MarkAttemptCompleted) { | |
509 base::Time creation_time = base::Time::Now(); | |
510 SavePageRequest request(kRequestId, kUrl, kClientId, creation_time, | |
511 kUserRequested); | |
512 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | |
513 base::Unretained(this))); | |
514 PumpLoop(); | |
515 | |
516 // Start request. | |
517 queue()->MarkAttemptStarted(kRequestId, | |
518 base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
519 base::Unretained(this))); | |
520 PumpLoop(); | |
521 ClearResults(); | |
522 | |
523 queue()->MarkAttemptCompleted( | |
524 kRequestId, base::Bind(&RequestQueueTest::UpdateRequestsDone, | |
525 base::Unretained(this))); | |
526 PumpLoop(); | |
527 | |
528 ASSERT_TRUE(update_requests_result()); | |
529 EXPECT_EQ(1UL, update_requests_result()->item_statuses.size()); | |
530 EXPECT_EQ(kRequestId, update_requests_result()->item_statuses.at(0).first); | |
531 EXPECT_EQ(ItemActionStatus::SUCCESS, | |
532 update_requests_result()->item_statuses.at(0).second); | |
533 EXPECT_EQ(1UL, update_requests_result()->updated_items.size()); | |
534 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, | |
535 update_requests_result()->updated_items.at(0).request_state()); | |
536 } | |
537 | |
538 // Request expiration is detected during the call to pick a request, which | |
539 // is why this test calls PickNextRequest(). | |
540 TEST_F(RequestQueueTest, CleanStaleRequests) { | |
541 // Create a request that is already expired. | |
542 base::Time creation_time = | |
543 base::Time::Now() - base::TimeDelta::FromSeconds(2 * kOneWeekInSeconds); | |
544 | |
545 SavePageRequest original_request(kRequestId, kUrl, kClientId, creation_time, | |
546 kUserRequested); | |
547 queue()->AddRequest( | |
548 original_request, | |
549 base::Bind(&RequestQueueTest::AddRequestDone, base::Unretained(this))); | |
550 this->PumpLoop(); | |
551 this->ClearResults(); | |
552 | |
553 // Set up a picker factory pointing to our fake notifier. | |
554 OfflinerPolicy policy; | |
555 RequestNotifierStub notifier; | |
556 RequestCoordinatorEventLogger event_logger; | |
557 std::unique_ptr<PickRequestTaskFactory> picker_factory( | |
558 new PickRequestTaskFactory(&policy, ¬ifier, &event_logger)); | |
559 queue()->SetPickerFactory(std::move(picker_factory)); | |
560 | |
561 // Do a pick and clean operation, which will remove stale entries. | |
562 DeviceConditions conditions; | |
563 std::set<int64_t> disabled_list; | |
564 queue()->PickNextRequest( | |
565 base::Bind(&RequestQueueTest::RequestPickedCallback, | |
566 base::Unretained(this)), | |
567 base::Bind(&RequestQueueTest::RequestNotPickedCallback, | |
568 base::Unretained(this)), | |
569 base::Bind(&RequestQueueTest::RequestCountCallback, | |
570 base::Unretained(this)), | |
571 conditions, disabled_list); | |
572 | |
573 this->PumpLoop(); | |
574 | |
575 // Notifier should have been notified that the request was removed. | |
576 ASSERT_EQ(notifier.last_expired_request().request_id(), kRequestId); | |
577 ASSERT_EQ(notifier.last_request_expiration_status(), | |
578 RequestNotifier::BackgroundSavePageResult::EXPIRED); | |
579 | |
580 // Doing a get should show no entries left in the queue since the expired | |
581 // request has been removed. | |
582 queue()->GetRequests( | |
583 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | |
584 this->PumpLoop(); | |
585 ASSERT_EQ(GetRequestsResult::SUCCESS, this->last_get_requests_result()); | |
586 ASSERT_TRUE(this->last_requests().empty()); | |
587 } | |
588 | |
589 } // namespace offline_pages | |
OLD | NEW |