| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/offline_pages/background/request_picker.h" | 5 #include "components/offline_pages/background/request_picker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 void RequestPickerTest::RequestNotPicked( | 148 void RequestPickerTest::RequestNotPicked( |
| 149 const bool non_user_requested_tasks_remaining) { | 149 const bool non_user_requested_tasks_remaining) { |
| 150 request_queue_not_picked_called_ = true; | 150 request_queue_not_picked_called_ = true; |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Test helper to queue the two given requests and then pick one of them per | 153 // Test helper to queue the two given requests and then pick one of them per |
| 154 // configured policy. | 154 // configured policy. |
| 155 void RequestPickerTest::QueueRequestsAndChooseOne( | 155 void RequestPickerTest::QueueRequestsAndChooseOne( |
| 156 const SavePageRequest& request1, const SavePageRequest& request2) { | 156 const SavePageRequest& request1, const SavePageRequest& request2) { |
| 157 DeviceConditions conditions; | 157 DeviceConditions conditions; |
| 158 std::set<int64_t> disabled_requests; |
| 158 // Add test requests on the Queue. | 159 // Add test requests on the Queue. |
| 159 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, | 160 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, |
| 160 base::Unretained(this))); | 161 base::Unretained(this))); |
| 161 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, | 162 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, |
| 162 base::Unretained(this))); | 163 base::Unretained(this))); |
| 163 | 164 |
| 164 // Pump the loop to give the async queue the opportunity to do the adds. | 165 // Pump the loop to give the async queue the opportunity to do the adds. |
| 165 PumpLoop(); | 166 PumpLoop(); |
| 166 | 167 |
| 167 // Call the method under test. | 168 // Call the method under test. |
| 168 picker_->ChooseNextRequest( | 169 picker_->ChooseNextRequest( |
| 169 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | 170 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), |
| 170 base::Bind(&RequestPickerTest::RequestNotPicked, base::Unretained(this)), | 171 base::Bind(&RequestPickerTest::RequestNotPicked, base::Unretained(this)), |
| 171 &conditions); | 172 &conditions, disabled_requests); |
| 172 | 173 |
| 173 // Pump the loop again to give the async queue the opportunity to return | 174 // Pump the loop again to give the async queue the opportunity to return |
| 174 // results from the Get operation, and for the picker to call the "picked" | 175 // results from the Get operation, and for the picker to call the "picked" |
| 175 // callback. | 176 // callback. |
| 176 PumpLoop(); | 177 PumpLoop(); |
| 177 } | 178 } |
| 178 | 179 |
| 179 TEST_F(RequestPickerTest, PickFromEmptyQueue) { | 180 TEST_F(RequestPickerTest, PickFromEmptyQueue) { |
| 180 DeviceConditions conditions; | 181 DeviceConditions conditions; |
| 182 std::set<int64_t> disabled_requests; |
| 181 picker_->ChooseNextRequest( | 183 picker_->ChooseNextRequest( |
| 182 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), | 184 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), |
| 183 base::Bind(&RequestPickerTest::RequestNotPicked, base::Unretained(this)), | 185 base::Bind(&RequestPickerTest::RequestNotPicked, base::Unretained(this)), |
| 184 &conditions); | 186 &conditions, disabled_requests); |
| 185 | 187 |
| 186 // Pump the loop again to give the async queue the opportunity to return | 188 // Pump the loop again to give the async queue the opportunity to return |
| 187 // results from the Get operation, and for the picker to call the "QueueEmpty" | 189 // results from the Get operation, and for the picker to call the "QueueEmpty" |
| 188 // callback. | 190 // callback. |
| 189 PumpLoop(); | 191 PumpLoop(); |
| 190 | 192 |
| 191 EXPECT_TRUE(request_queue_not_picked_called_); | 193 EXPECT_TRUE(request_queue_not_picked_called_); |
| 192 } | 194 } |
| 193 | 195 |
| 194 TEST_F(RequestPickerTest, ChooseRequestWithHigherRetryCount) { | 196 TEST_F(RequestPickerTest, ChooseRequestWithHigherRetryCount) { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 367 |
| 366 // With default policy settings, we should choose the earlier request. | 368 // With default policy settings, we should choose the earlier request. |
| 367 // However, we will make the earlier reqeust exceed the limit. | 369 // However, we will make the earlier reqeust exceed the limit. |
| 368 request1.set_completed_attempt_count(policy_->GetMaxCompletedTries()); | 370 request1.set_completed_attempt_count(policy_->GetMaxCompletedTries()); |
| 369 | 371 |
| 370 QueueRequestsAndChooseOne(request1, request2); | 372 QueueRequestsAndChooseOne(request1, request2); |
| 371 | 373 |
| 372 EXPECT_EQ(kRequestId2, last_picked_->request_id()); | 374 EXPECT_EQ(kRequestId2, last_picked_->request_id()); |
| 373 EXPECT_FALSE(request_queue_not_picked_called_); | 375 EXPECT_FALSE(request_queue_not_picked_called_); |
| 374 } | 376 } |
| 377 |
| 378 |
| 379 TEST_F(RequestPickerTest, ChooseRequestThatIsNotDisabled) { |
| 380 policy_.reset(new OfflinerPolicy(kPreferUntried, kPreferEarlier, |
| 381 kPreferRetryCount, kMaxStartedTries, |
| 382 kMaxCompletedTries + 1)); |
| 383 picker_.reset(new RequestPicker(queue_.get(), policy_.get(), notifier_.get(), |
| 384 &event_logger_)); |
| 385 |
| 386 base::Time creation_time = base::Time::Now(); |
| 387 SavePageRequest request1( |
| 388 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested); |
| 389 SavePageRequest request2( |
| 390 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested); |
| 391 request2.set_completed_attempt_count(kAttemptCount); |
| 392 |
| 393 // put request 2 on disabled list, ensure request1 picked instead, |
| 394 // even though policy would prefer 2. |
| 395 std::set<int64_t> disabled_requests {kRequestId2}; |
| 396 DeviceConditions conditions; |
| 397 |
| 398 // Add test requests on the Queue. |
| 399 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, |
| 400 base::Unretained(this))); |
| 401 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, |
| 402 base::Unretained(this))); |
| 403 |
| 404 // Pump the loop to give the async queue the opportunity to do the adds. |
| 405 PumpLoop(); |
| 406 |
| 407 // Call the method under test. |
| 408 picker_->ChooseNextRequest( |
| 409 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), |
| 410 base::Bind(&RequestPickerTest::RequestNotPicked, base::Unretained(this)), |
| 411 &conditions, disabled_requests); |
| 412 |
| 413 // Pump the loop again to give the async queue the opportunity to return |
| 414 // results from the Get operation, and for the picker to call the "picked" |
| 415 // callback. |
| 416 PumpLoop(); |
| 417 |
| 418 EXPECT_EQ(kRequestId1, last_picked_->request_id()); |
| 419 EXPECT_FALSE(request_queue_not_picked_called_); |
| 420 } |
| 375 } // namespace offline_pages | 421 } // namespace offline_pages |
| OLD | NEW |