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

Side by Side Diff: components/offline_pages/background/pick_request_task_unittest.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: more rebase Created 4 years 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
OLDNEW
(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/pick_request_task.h"
6
7 #include <memory>
8 #include <set>
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/request_queue_store.h"
20 #include "components/offline_pages/background/save_page_request.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace offline_pages {
24
25 namespace {
26 // Data for request 1.
27 const int64_t kRequestId1 = 17;
28 const GURL kUrl1("https://google.com");
29 const ClientId kClientId1("bookmark", "1234");
30 // Data for request 2.
31 const int64_t kRequestId2 = 42;
32 const GURL kUrl2("http://nytimes.com");
33 const ClientId kClientId2("bookmark", "5678");
34 const bool kUserRequested = true;
35 const int kAttemptCount = 1;
36 const int kMaxStartedTries = 5;
37 const int kMaxCompletedTries = 1;
38
39 // Constants for policy values - These settings represent the default values.
40 const bool kPreferUntried = false;
41 const bool kPreferEarlier = true;
42 const bool kPreferRetryCount = true;
43 const int kBackgroundProcessingTimeBudgetSeconds = 170;
44
45 // Default request
46 const SavePageRequest kEmptyRequest(0UL,
47 GURL(""),
48 ClientId("", ""),
49 base::Time(),
50 true);
51 } // namespace
52
53 // Helper class needed by the PickRequestTask
54 class RequestNotifierStub : public RequestNotifier {
55 public:
56 RequestNotifierStub()
57 : last_expired_request_(kEmptyRequest), total_expired_requests_(0) {}
58
59 void NotifyAdded(const SavePageRequest& request) override {}
60 void NotifyChanged(const SavePageRequest& request) override {}
61
62 void NotifyCompleted(const SavePageRequest& request,
63 BackgroundSavePageResult status) override {
64 last_expired_request_ = request;
65 last_request_expiration_status_ = status;
66 total_expired_requests_++;
67 }
68
69 const SavePageRequest& last_expired_request() {
70 return last_expired_request_;
71 }
72
73 RequestCoordinator::BackgroundSavePageResult
74 last_request_expiration_status() {
75 return last_request_expiration_status_;
76 }
77
78 int32_t total_expired_requests() { return total_expired_requests_; }
79
80 private:
81 BackgroundSavePageResult last_request_expiration_status_;
82 SavePageRequest last_expired_request_;
83 int32_t total_expired_requests_;
84 };
85
86 class PickRequestTaskTest : public testing::Test {
87 public:
88 PickRequestTaskTest();
89
90 ~PickRequestTaskTest() override;
91
92 void SetUp() override;
93
94 void PumpLoop();
95
96 void AddRequestDone(ItemActionStatus status);
97
98 void RequestPicked(const SavePageRequest& request);
99
100 void RequestNotPicked(const bool non_user_requested_tasks_remaining);
101
102 void RequestCountCallback(size_t total_count, size_t available_count);
103
104 void QueueRequests(const SavePageRequest& request1,
105 const SavePageRequest& request2);
106
107 // Reset the factory and the task using the current policy.
108 void MakeFactoryAndTask();
109
110 RequestNotifierStub* GetNotifier() { return notifier_.get(); }
111
112 PickRequestTask* task() { return task_.get(); }
113
114 void TaskCompletionCallback(Task* completed_task);
115
116 protected:
117 std::unique_ptr<RequestQueueStore> store_;
118 std::unique_ptr<RequestNotifierStub> notifier_;
119 std::unique_ptr<SavePageRequest> last_picked_;
120 std::unique_ptr<OfflinerPolicy> policy_;
121 RequestCoordinatorEventLogger event_logger_;
122 std::set<int64_t> disabled_requests_;
123 std::unique_ptr<PickRequestTaskFactory> factory_;
124 std::unique_ptr<PickRequestTask> task_;
125 bool request_queue_not_picked_called_;
126 size_t total_request_count_;
127 size_t available_request_count_;
128 bool task_complete_called_;
129
130 private:
131 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
132 base::ThreadTaskRunnerHandle task_runner_handle_;
133 };
134
135 PickRequestTaskTest::PickRequestTaskTest()
136 : task_runner_(new base::TestSimpleTaskRunner),
137 task_runner_handle_(task_runner_) {}
138
139 PickRequestTaskTest::~PickRequestTaskTest() {}
140
141 void PickRequestTaskTest::SetUp() {
142 DeviceConditions conditions;
143 store_.reset(new RequestQueueInMemoryStore());
144 policy_.reset(new OfflinerPolicy());
145 notifier_.reset(new RequestNotifierStub());
146 MakeFactoryAndTask();
147 request_queue_not_picked_called_ = false;
148 total_request_count_ = 9999;
149 available_request_count_ = 9999;
150 task_complete_called_ = false;
151 last_picked_.reset();
152 }
153
154 void PickRequestTaskTest::PumpLoop() {
155 task_runner_->RunUntilIdle();
156 }
157
158 void PickRequestTaskTest::TaskCompletionCallback(Task* completed_task) {
159 task_complete_called_ = true;
160 }
161
162 void PickRequestTaskTest::AddRequestDone(ItemActionStatus status) {}
163
164 void PickRequestTaskTest::RequestPicked(const SavePageRequest& request) {
165 last_picked_.reset(new SavePageRequest(request));
166 }
167
168 void PickRequestTaskTest::RequestNotPicked(
169 const bool non_user_requested_tasks_remaining) {
170 request_queue_not_picked_called_ = true;
171 }
172
173 void PickRequestTaskTest::RequestCountCallback(size_t total_count,
174 size_t available_count) {
175 total_request_count_ = total_count;
176 available_request_count_ = available_count;
177 }
178
179 // Test helper to queue the two given requests.
180 void PickRequestTaskTest::QueueRequests(const SavePageRequest& request1,
181 const SavePageRequest& request2) {
182 DeviceConditions conditions;
183 std::set<int64_t> disabled_requests;
184 // Add test requests on the Queue.
185 store_->AddRequest(request1, base::Bind(&PickRequestTaskTest::AddRequestDone,
186 base::Unretained(this)));
187 store_->AddRequest(request2, base::Bind(&PickRequestTaskTest::AddRequestDone,
188 base::Unretained(this)));
189
190 // Pump the loop to give the async queue the opportunity to do the adds.
191 PumpLoop();
192 }
193
194 void PickRequestTaskTest::MakeFactoryAndTask() {
195 factory_.reset(new PickRequestTaskFactory(policy_.get(), notifier_.get(),
196 &event_logger_));
197 DeviceConditions conditions;
198 task_ = factory_->CreatePickerTask(
199 store_.get(),
200 base::Bind(&PickRequestTaskTest::RequestPicked, base::Unretained(this)),
201 base::Bind(&PickRequestTaskTest::RequestNotPicked,
202 base::Unretained(this)),
203 base::Bind(&PickRequestTaskTest::RequestCountCallback,
204 base::Unretained(this)),
205 conditions, disabled_requests_);
206 task_->SetTaskCompletionCallbackForTesting(
207 task_runner_.get(),
208 base::Bind(&PickRequestTaskTest::TaskCompletionCallback,
209 base::Unretained(this)));
210 }
211
212 TEST_F(PickRequestTaskTest, PickFromEmptyQueue) {
213 task()->Run();
214 PumpLoop();
215
216 // Pump the loop again to give the async queue the opportunity to return
217 // results from the Get operation, and for the picker to call the "QueueEmpty"
218 // callback.
219 PumpLoop();
220
221 EXPECT_TRUE(request_queue_not_picked_called_);
222 EXPECT_EQ((size_t) 0, total_request_count_);
223 EXPECT_EQ((size_t) 0, available_request_count_);
224 EXPECT_TRUE(task_complete_called_);
225 }
226
227 TEST_F(PickRequestTaskTest, ChooseRequestWithHigherRetryCount) {
228 // Set up policy to prefer higher retry count.
229 policy_.reset(new OfflinerPolicy(
230 kPreferUntried, kPreferEarlier, kPreferRetryCount, kMaxStartedTries,
231 kMaxCompletedTries + 1, kBackgroundProcessingTimeBudgetSeconds));
232 MakeFactoryAndTask();
233
234 base::Time creation_time = base::Time::Now();
235 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
236 kUserRequested);
237 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
238 kUserRequested);
239 request2.set_completed_attempt_count(kAttemptCount);
240
241 QueueRequests(request1, request2);
242
243 task()->Run();
244 PumpLoop();
245
246 EXPECT_EQ(kRequestId2, last_picked_->request_id());
247 EXPECT_FALSE(request_queue_not_picked_called_);
248 EXPECT_EQ((size_t) 2, total_request_count_);
249 EXPECT_EQ((size_t) 2, available_request_count_);
250 EXPECT_TRUE(task_complete_called_);
251 }
252
253 TEST_F(PickRequestTaskTest, ChooseRequestWithSameRetryCountButEarlier) {
254 base::Time creation_time1 =
255 base::Time::Now() - base::TimeDelta::FromSeconds(10);
256 base::Time creation_time2 = base::Time::Now();
257 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
258 kUserRequested);
259 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
260 kUserRequested);
261
262 QueueRequests(request1, request2);
263
264 task()->Run();
265 PumpLoop();
266
267 EXPECT_EQ(kRequestId1, last_picked_->request_id());
268 EXPECT_FALSE(request_queue_not_picked_called_);
269 EXPECT_TRUE(task_complete_called_);
270 }
271
272 TEST_F(PickRequestTaskTest, ChooseEarlierRequest) {
273 // We need a custom policy object prefering recency to retry count.
274 policy_.reset(new OfflinerPolicy(
275 kPreferUntried, kPreferEarlier, !kPreferRetryCount, kMaxStartedTries,
276 kMaxCompletedTries, kBackgroundProcessingTimeBudgetSeconds));
277 MakeFactoryAndTask();
278
279 base::Time creation_time1 =
280 base::Time::Now() - base::TimeDelta::FromSeconds(10);
281 base::Time creation_time2 = base::Time::Now();
282 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
283 kUserRequested);
284 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
285 kUserRequested);
286 request2.set_completed_attempt_count(kAttemptCount);
287
288 QueueRequests(request1, request2);
289
290 task()->Run();
291 PumpLoop();
292
293 EXPECT_EQ(kRequestId1, last_picked_->request_id());
294 EXPECT_FALSE(request_queue_not_picked_called_);
295 EXPECT_TRUE(task_complete_called_);
296 }
297
298 TEST_F(PickRequestTaskTest, ChooseSameTimeRequestWithHigherRetryCount) {
299 // We need a custom policy object preferring recency to retry count.
300 policy_.reset(new OfflinerPolicy(
301 kPreferUntried, kPreferEarlier, !kPreferRetryCount, kMaxStartedTries,
302 kMaxCompletedTries + 1, kBackgroundProcessingTimeBudgetSeconds));
303 MakeFactoryAndTask();
304
305 base::Time creation_time = base::Time::Now();
306 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
307 kUserRequested);
308 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
309 kUserRequested);
310 request2.set_completed_attempt_count(kAttemptCount);
311
312 QueueRequests(request1, request2);
313
314 task()->Run();
315 PumpLoop();
316
317 EXPECT_EQ(kRequestId2, last_picked_->request_id());
318 EXPECT_FALSE(request_queue_not_picked_called_);
319 EXPECT_TRUE(task_complete_called_);
320 }
321
322 TEST_F(PickRequestTaskTest, ChooseRequestWithLowerRetryCount) {
323 // We need a custom policy object preferring lower retry count.
324 policy_.reset(new OfflinerPolicy(
325 !kPreferUntried, kPreferEarlier, kPreferRetryCount, kMaxStartedTries,
326 kMaxCompletedTries + 1, kBackgroundProcessingTimeBudgetSeconds));
327 MakeFactoryAndTask();
328
329 base::Time creation_time = base::Time::Now();
330 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
331 kUserRequested);
332 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
333 kUserRequested);
334 request2.set_completed_attempt_count(kAttemptCount);
335
336 QueueRequests(request1, request2);
337
338 task()->Run();
339 PumpLoop();
340
341 EXPECT_EQ(kRequestId1, last_picked_->request_id());
342 EXPECT_FALSE(request_queue_not_picked_called_);
343 EXPECT_TRUE(task_complete_called_);
344 }
345
346 TEST_F(PickRequestTaskTest, ChooseLaterRequest) {
347 // We need a custom policy preferring recency over retry, and later requests.
348 policy_.reset(new OfflinerPolicy(
349 kPreferUntried, !kPreferEarlier, !kPreferRetryCount, kMaxStartedTries,
350 kMaxCompletedTries, kBackgroundProcessingTimeBudgetSeconds));
351 MakeFactoryAndTask();
352
353 base::Time creation_time1 =
354 base::Time::Now() - base::TimeDelta::FromSeconds(10);
355 base::Time creation_time2 = base::Time::Now();
356 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
357 kUserRequested);
358 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
359 kUserRequested);
360
361 QueueRequests(request1, request2);
362
363 task()->Run();
364 PumpLoop();
365
366 EXPECT_EQ(kRequestId2, last_picked_->request_id());
367 EXPECT_FALSE(request_queue_not_picked_called_);
368 EXPECT_TRUE(task_complete_called_);
369 }
370
371 TEST_F(PickRequestTaskTest, ChooseNonExpiredRequest) {
372 base::Time creation_time = base::Time::Now();
373 base::Time expired_time =
374 creation_time - base::TimeDelta::FromSeconds(
375 policy_->GetRequestExpirationTimeInSeconds() + 60);
376 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
377 kUserRequested);
378 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, expired_time,
379 kUserRequested);
380
381 QueueRequests(request1, request2);
382
383 task()->Run();
384 PumpLoop();
385
386 EXPECT_EQ(kRequestId1, last_picked_->request_id());
387 EXPECT_FALSE(request_queue_not_picked_called_);
388 EXPECT_EQ(kRequestId2, GetNotifier()->last_expired_request().request_id());
389 EXPECT_EQ(RequestNotifier::BackgroundSavePageResult::EXPIRED,
390 GetNotifier()->last_request_expiration_status());
391 EXPECT_EQ(1, GetNotifier()->total_expired_requests());
392 EXPECT_EQ((size_t) 1, total_request_count_);
393 EXPECT_EQ((size_t) 1, available_request_count_);
394 EXPECT_TRUE(task_complete_called_);
395 }
396
397 TEST_F(PickRequestTaskTest, ChooseRequestThatHasNotExceededStartLimit) {
398 base::Time creation_time1 =
399 base::Time::Now() - base::TimeDelta::FromSeconds(1);
400 base::Time creation_time2 = base::Time::Now();
401 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
402 kUserRequested);
403 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
404 kUserRequested);
405
406 // With default policy settings, we should choose the earlier request.
407 // However, we will make the earlier reqeust exceed the limit.
408 request1.set_started_attempt_count(policy_->GetMaxStartedTries());
409
410 QueueRequests(request1, request2);
411
412 task()->Run();
413 PumpLoop();
414
415 EXPECT_EQ(kRequestId2, last_picked_->request_id());
416 EXPECT_FALSE(request_queue_not_picked_called_);
417 // TODO(dougarnett): Counts should be 1 here once requests exceeding start
418 // count get cleaned up from the queue.
419 EXPECT_EQ((size_t) 2, total_request_count_);
420 EXPECT_EQ((size_t) 2, available_request_count_);
421 EXPECT_TRUE(task_complete_called_);
422 }
423
424 TEST_F(PickRequestTaskTest, ChooseRequestThatHasNotExceededCompletionLimit) {
425 base::Time creation_time1 =
426 base::Time::Now() - base::TimeDelta::FromSeconds(1);
427 base::Time creation_time2 = base::Time::Now();
428 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
429 kUserRequested);
430 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
431 kUserRequested);
432
433 // With default policy settings, we should choose the earlier request.
434 // However, we will make the earlier reqeust exceed the limit.
435 request1.set_completed_attempt_count(policy_->GetMaxCompletedTries());
436
437 QueueRequests(request1, request2);
438
439 task()->Run();
440 PumpLoop();
441
442 EXPECT_EQ(kRequestId2, last_picked_->request_id());
443 EXPECT_FALSE(request_queue_not_picked_called_);
444 EXPECT_TRUE(task_complete_called_);
445 }
446
447 TEST_F(PickRequestTaskTest, ChooseRequestThatIsNotDisabled) {
448 policy_.reset(new OfflinerPolicy(
449 kPreferUntried, kPreferEarlier, kPreferRetryCount, kMaxStartedTries,
450 kMaxCompletedTries + 1, kBackgroundProcessingTimeBudgetSeconds));
451
452 // put request 2 on disabled list, ensure request1 picked instead,
453 // even though policy would prefer 2.
454 disabled_requests_.insert(kRequestId2);
455 MakeFactoryAndTask();
456
457 base::Time creation_time = base::Time::Now();
458 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
459 kUserRequested);
460 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
461 kUserRequested);
462 request2.set_completed_attempt_count(kAttemptCount);
463
464 // Add test requests on the Queue.
465 QueueRequests(request1, request2);
466
467 task()->Run();
468 PumpLoop();
469
470 // Pump the loop again to give the async queue the opportunity to return
471 // results from the Get operation, and for the picker to call the "picked"
472 // callback.
473 PumpLoop();
474
475 EXPECT_EQ(kRequestId1, last_picked_->request_id());
476 EXPECT_FALSE(request_queue_not_picked_called_);
477 EXPECT_EQ((size_t) 2, total_request_count_);
478 EXPECT_EQ((size_t) 1, available_request_count_);
479 EXPECT_TRUE(task_complete_called_);
480 }
481
482 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698