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

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

Issue 2178723002: Add more unit tests for RequestPicker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename helper function, improve comment Created 4 years, 4 months 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
« no previous file with comments | « components/offline_pages/background/offliner_policy.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "components/offline_pages/background/device_conditions.h" 11 #include "components/offline_pages/background/device_conditions.h"
12 #include "components/offline_pages/background/request_queue.h" 12 #include "components/offline_pages/background/request_queue.h"
13 #include "components/offline_pages/background/request_queue_in_memory_store.h" 13 #include "components/offline_pages/background/request_queue_in_memory_store.h"
14 #include "components/offline_pages/background/save_page_request.h" 14 #include "components/offline_pages/background/save_page_request.h"
15 #include "components/offline_pages/offline_page_item.h" 15 #include "components/offline_pages/offline_page_item.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace offline_pages { 18 namespace offline_pages {
19 19
20 namespace { 20 namespace {
21 // Data for request 1. 21 // Data for request 1.
22 const int64_t kRequestId1 = 17; 22 const int64_t kRequestId1 = 17;
23 const GURL kUrl1("https://google.com"); 23 const GURL kUrl1("https://google.com");
24 const ClientId kClientId1("bookmark", "1234"); 24 const ClientId kClientId1("bookmark", "1234");
25 // Data for request 2. 25 // Data for request 2.
26 const int64_t kRequestId2 = 42; 26 const int64_t kRequestId2 = 42;
27 const GURL kUrl2("http://nytimes.com"); 27 const GURL kUrl2("http://nytimes.com");
28 const ClientId kClientId2("bookmark", "5678"); 28 const ClientId kClientId2("bookmark", "5678");
29 const bool kUserRequested = true; 29 const bool kUserRequested = true;
30 const int kAttemptCount = 1;
31
32 // Constants for policy values - These settings represent the default values.
33 const bool kPreferUntried = false;
34 const bool kPreferEarlier = true;
35 const bool kPreferRetryCount = true;
30 } // namespace 36 } // namespace
31 37
32 class RequestPickerTest : public testing::Test { 38 class RequestPickerTest : public testing::Test {
33 public: 39 public:
34 RequestPickerTest(); 40 RequestPickerTest();
35 41
36 ~RequestPickerTest() override; 42 ~RequestPickerTest() override;
37 43
38 void SetUp() override; 44 void SetUp() override;
39 45
40 void PumpLoop(); 46 void PumpLoop();
41 47
42 void AddRequestDone(RequestQueue::AddRequestResult result, 48 void AddRequestDone(RequestQueue::AddRequestResult result,
43 const SavePageRequest& request); 49 const SavePageRequest& request);
44 50
45 void RequestPicked(const SavePageRequest& request); 51 void RequestPicked(const SavePageRequest& request);
46 52
47 void RequestQueueEmpty(); 53 void RequestQueueEmpty();
48 54
55 void QueueRequestsAndChooseOne(const SavePageRequest& request1,
56 const SavePageRequest& request2);
57
49 protected: 58 protected:
50 // The request queue is simple enough we will use a real queue with a memory 59 // The request queue is simple enough we will use a real queue with a memory
51 // store instead of a stub. 60 // store instead of a stub.
52 std::unique_ptr<RequestQueue> queue_; 61 std::unique_ptr<RequestQueue> queue_;
53 std::unique_ptr<RequestPicker> picker_; 62 std::unique_ptr<RequestPicker> picker_;
54 std::unique_ptr<SavePageRequest> last_picked_; 63 std::unique_ptr<SavePageRequest> last_picked_;
55 std::unique_ptr<OfflinerPolicy> policy_; 64 std::unique_ptr<OfflinerPolicy> policy_;
56 bool request_queue_empty_called_; 65 bool request_queue_empty_called_;
57 66
58 private: 67 private:
(...skipping 24 matching lines...) Expand all
83 const SavePageRequest& request) {} 92 const SavePageRequest& request) {}
84 93
85 void RequestPickerTest::RequestPicked(const SavePageRequest& request) { 94 void RequestPickerTest::RequestPicked(const SavePageRequest& request) {
86 last_picked_.reset(new SavePageRequest(request)); 95 last_picked_.reset(new SavePageRequest(request));
87 } 96 }
88 97
89 void RequestPickerTest::RequestQueueEmpty() { 98 void RequestPickerTest::RequestQueueEmpty() {
90 request_queue_empty_called_ = true; 99 request_queue_empty_called_ = true;
91 } 100 }
92 101
93 TEST_F(RequestPickerTest, ChooseNextRequest) { 102 // Test helper to queue the two given requests and then pick one of them per
94 base::Time creation_time = base::Time::Now(); 103 // configured policy.
104 void RequestPickerTest::QueueRequestsAndChooseOne(
105 const SavePageRequest& request1, const SavePageRequest& request2) {
95 DeviceConditions conditions; 106 DeviceConditions conditions;
96 SavePageRequest request1( 107 // Add test requests on the Queue.
97 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested);
98 SavePageRequest request2(
99 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested);
100 // Put some test requests on the Queue.
101 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone, 108 queue_->AddRequest(request1, base::Bind(&RequestPickerTest::AddRequestDone,
102 base::Unretained(this))); 109 base::Unretained(this)));
103 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone, 110 queue_->AddRequest(request2, base::Bind(&RequestPickerTest::AddRequestDone,
104 base::Unretained(this))); 111 base::Unretained(this)));
105 112
106 // Pump the loop to give the async queue the opportunity to do the adds. 113 // Pump the loop to give the async queue the opportunity to do the adds.
107 PumpLoop(); 114 PumpLoop();
108 115
116 // Call the method under test.
109 picker_->ChooseNextRequest( 117 picker_->ChooseNextRequest(
110 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), 118 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)),
111 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), 119 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)),
112 &conditions); 120 &conditions);
113 121
114 // Pump the loop again to give the async queue the opportunity to return 122 // Pump the loop again to give the async queue the opportunity to return
115 // results from the Get operation, and for the picker to call the "picked" 123 // results from the Get operation, and for the picker to call the "picked"
116 // callback. 124 // callback.
117 PumpLoop(); 125 PumpLoop();
118
119 EXPECT_EQ(kRequestId2, last_picked_->request_id());
120 EXPECT_FALSE(request_queue_empty_called_);
121 } 126 }
122 127
123 TEST_F(RequestPickerTest, PickFromEmptyQueue) { 128 TEST_F(RequestPickerTest, PickFromEmptyQueue) {
124 DeviceConditions conditions; 129 DeviceConditions conditions;
125 picker_->ChooseNextRequest( 130 picker_->ChooseNextRequest(
126 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)), 131 base::Bind(&RequestPickerTest::RequestPicked, base::Unretained(this)),
127 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)), 132 base::Bind(&RequestPickerTest::RequestQueueEmpty, base::Unretained(this)),
128 &conditions); 133 &conditions);
129 134
130 // Pump the loop again to give the async queue the opportunity to return 135 // Pump the loop again to give the async queue the opportunity to return
131 // results from the Get operation, and for the picker to call the "QueueEmpty" 136 // results from the Get operation, and for the picker to call the "QueueEmpty"
132 // callback. 137 // callback.
133 PumpLoop(); 138 PumpLoop();
134 139
135 EXPECT_TRUE(request_queue_empty_called_); 140 EXPECT_TRUE(request_queue_empty_called_);
136 } 141 }
137 142
143 TEST_F(RequestPickerTest, ChooseRequestWithHigherRetryCount) {
144 base::Time creation_time = base::Time::Now();
145 SavePageRequest request1(
146 kRequestId1, kUrl1, kClientId1, creation_time, kUserRequested);
147 SavePageRequest request2(
148 kRequestId2, kUrl2, kClientId2, creation_time, kUserRequested);
149 request2.set_attempt_count(kAttemptCount);
150
151 QueueRequestsAndChooseOne(request1, request2);
152
153 EXPECT_EQ(kRequestId2, last_picked_->request_id());
154 EXPECT_FALSE(request_queue_empty_called_);
155 }
156
157 TEST_F(RequestPickerTest, ChooseRequestWithSameRetryCountButEarlier) {
158 base::Time creation_time1 =
159 base::Time::Now() - base::TimeDelta::FromSeconds(10);
160 base::Time creation_time2 = base::Time::Now();
161 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
162 kUserRequested);
163 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
164 kUserRequested);
165
166 QueueRequestsAndChooseOne(request1, request2);
167
168 EXPECT_EQ(kRequestId1, last_picked_->request_id());
169 EXPECT_FALSE(request_queue_empty_called_);
170 }
171
172 TEST_F(RequestPickerTest, ChooseEarlierRequest) {
173 // We need a custom policy object prefering recency to retry count.
174 policy_.reset(
175 new OfflinerPolicy(kPreferUntried, kPreferEarlier, !kPreferRetryCount));
176 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
177
178 base::Time creation_time1 =
179 base::Time::Now() - base::TimeDelta::FromSeconds(10);
180 base::Time creation_time2 = base::Time::Now();
181 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
182 kUserRequested);
183 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
184 kUserRequested);
185 request2.set_attempt_count(kAttemptCount);
186
187 QueueRequestsAndChooseOne(request1, request2);
188
189 EXPECT_EQ(kRequestId1, last_picked_->request_id());
190 EXPECT_FALSE(request_queue_empty_called_);
191 }
192
193 TEST_F(RequestPickerTest, ChooseSameTimeRequestWithHigherRetryCount) {
194 // We need a custom policy object preferring recency to retry count.
195 policy_.reset(
196 new OfflinerPolicy(kPreferUntried, kPreferEarlier, !kPreferRetryCount));
197 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
198
199 base::Time creation_time = base::Time::Now();
200 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
201 kUserRequested);
202 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
203 kUserRequested);
204 request2.set_attempt_count(kAttemptCount);
205
206 QueueRequestsAndChooseOne(request1, request2);
207
208 EXPECT_EQ(kRequestId2, last_picked_->request_id());
209 EXPECT_FALSE(request_queue_empty_called_);
210 }
211
212 TEST_F(RequestPickerTest, ChooseRequestWithLowerRetryCount) {
213 // We need a custom policy object preferring lower retry count.
214 policy_.reset(
215 new OfflinerPolicy(!kPreferUntried, kPreferEarlier, kPreferRetryCount));
216 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
217
218 base::Time creation_time = base::Time::Now();
219 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time,
220 kUserRequested);
221 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time,
222 kUserRequested);
223 request2.set_attempt_count(kAttemptCount);
224
225 QueueRequestsAndChooseOne(request1, request2);
226
227 EXPECT_EQ(kRequestId1, last_picked_->request_id());
228 EXPECT_FALSE(request_queue_empty_called_);
229 }
230
231 TEST_F(RequestPickerTest, ChooseLaterRequest) {
232 // We need a custom policy preferring recency over retry, and later requests.
233 policy_.reset(
234 new OfflinerPolicy(kPreferUntried, !kPreferEarlier, !kPreferRetryCount));
235 picker_.reset(new RequestPicker(queue_.get(), policy_.get()));
236
237 base::Time creation_time1 =
238 base::Time::Now() - base::TimeDelta::FromSeconds(10);
239 base::Time creation_time2 = base::Time::Now();
240 SavePageRequest request1(kRequestId1, kUrl1, kClientId1, creation_time1,
241 kUserRequested);
242 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time2,
243 kUserRequested);
244
245 QueueRequestsAndChooseOne(request1, request2);
246
247 EXPECT_EQ(kRequestId2, last_picked_->request_id());
248 EXPECT_FALSE(request_queue_empty_called_);
249 }
250
138 } // namespace offline_pages 251 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/offliner_policy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698