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

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager_unittest.cc

Issue 2470923003: Handle timeout for update requests. (Closed)
Patch Set: Added a histogram for counting update timeouts Created 4 years, 1 month 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
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/safe_browsing_db/v4_update_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 157
158 // Initial state. No errors. 158 // Initial state. No errors.
159 EXPECT_EQ(0ul, pm->update_error_count_); 159 EXPECT_EQ(0ul, pm->update_error_count_);
160 EXPECT_EQ(1ul, pm->update_back_off_mult_); 160 EXPECT_EQ(1ul, pm->update_back_off_mult_);
161 expect_callback_to_be_called_ = false; 161 expect_callback_to_be_called_ = false;
162 pm->store_state_map_ = std::move(store_state_map_); 162 pm->store_state_map_ = std::move(store_state_map_);
163 pm->IssueUpdateRequest(); 163 pm->IssueUpdateRequest();
164 164
165 EXPECT_FALSE(pm->IsUpdateScheduled()); 165 EXPECT_FALSE(pm->IsUpdateScheduled());
166 166
167 runner->RunPendingTasks();
168
169 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 167 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
170 DCHECK(fetcher); 168 DCHECK(fetcher);
171 // Failed request status should result in error. 169 // Failed request status should result in error.
172 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 170 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
173 net::ERR_CONNECTION_RESET)); 171 net::ERR_CONNECTION_RESET));
174 fetcher->delegate()->OnURLFetchComplete(fetcher); 172 fetcher->delegate()->OnURLFetchComplete(fetcher);
175 173
176 // Should have recorded one error, but back off multiplier is unchanged. 174 // Should have recorded one error, but back off multiplier is unchanged.
177 EXPECT_EQ(1ul, pm->update_error_count_); 175 EXPECT_EQ(1ul, pm->update_error_count_);
178 EXPECT_EQ(1ul, pm->update_back_off_mult_); 176 EXPECT_EQ(1ul, pm->update_back_off_mult_);
(...skipping 12 matching lines...) Expand all
191 189
192 // Initial state. No errors. 190 // Initial state. No errors.
193 EXPECT_EQ(0ul, pm->update_error_count_); 191 EXPECT_EQ(0ul, pm->update_error_count_);
194 EXPECT_EQ(1ul, pm->update_back_off_mult_); 192 EXPECT_EQ(1ul, pm->update_back_off_mult_);
195 expect_callback_to_be_called_ = false; 193 expect_callback_to_be_called_ = false;
196 pm->store_state_map_ = std::move(store_state_map_); 194 pm->store_state_map_ = std::move(store_state_map_);
197 pm->IssueUpdateRequest(); 195 pm->IssueUpdateRequest();
198 196
199 EXPECT_FALSE(pm->IsUpdateScheduled()); 197 EXPECT_FALSE(pm->IsUpdateScheduled());
200 198
201 runner->RunPendingTasks();
202
203 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 199 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
204 DCHECK(fetcher); 200 DCHECK(fetcher);
205 fetcher->set_status(net::URLRequestStatus()); 201 fetcher->set_status(net::URLRequestStatus());
206 // Response code of anything other than 200 should result in error. 202 // Response code of anything other than 200 should result in error.
207 fetcher->set_response_code(net::HTTP_NO_CONTENT); 203 fetcher->set_response_code(net::HTTP_NO_CONTENT);
208 fetcher->SetResponseString(""); 204 fetcher->SetResponseString("");
209 fetcher->delegate()->OnURLFetchComplete(fetcher); 205 fetcher->delegate()->OnURLFetchComplete(fetcher);
210 206
211 // Should have recorded one error, but back off multiplier is unchanged. 207 // Should have recorded one error, but back off multiplier is unchanged.
212 EXPECT_EQ(1ul, pm->update_error_count_); 208 EXPECT_EQ(1ul, pm->update_error_count_);
(...skipping 13 matching lines...) Expand all
226 runner->ClearPendingTasks(); 222 runner->ClearPendingTasks();
227 223
228 // Initial state. No errors. 224 // Initial state. No errors.
229 EXPECT_EQ(0ul, pm->update_error_count_); 225 EXPECT_EQ(0ul, pm->update_error_count_);
230 EXPECT_EQ(1ul, pm->update_back_off_mult_); 226 EXPECT_EQ(1ul, pm->update_back_off_mult_);
231 expect_callback_to_be_called_ = true; 227 expect_callback_to_be_called_ = true;
232 pm->store_state_map_ = std::move(store_state_map_); 228 pm->store_state_map_ = std::move(store_state_map_);
233 pm->IssueUpdateRequest(); 229 pm->IssueUpdateRequest();
234 230
235 EXPECT_FALSE(pm->IsUpdateScheduled()); 231 EXPECT_FALSE(pm->IsUpdateScheduled());
236 232
Nathan Parker 2016/11/08 23:39:29 I'm not clear on why you need to remove these RunP
vakh (use Gerrit instead) 2016/11/09 00:08:51 IssueUpdateRequest queues a network request right
237 runner->RunPendingTasks();
238
239 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 233 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
240 DCHECK(fetcher); 234 DCHECK(fetcher);
241 fetcher->set_status(net::URLRequestStatus()); 235 fetcher->set_status(net::URLRequestStatus());
242 fetcher->set_response_code(net::HTTP_OK); 236 fetcher->set_response_code(net::HTTP_OK);
243 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 237 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
244 fetcher->delegate()->OnURLFetchComplete(fetcher); 238 fetcher->delegate()->OnURLFetchComplete(fetcher);
245 239
246 // No error, back off multiplier is unchanged. 240 // No error, back off multiplier is unchanged.
247 EXPECT_EQ(0ul, pm->update_error_count_); 241 EXPECT_EQ(0ul, pm->update_error_count_);
248 EXPECT_EQ(1ul, pm->update_back_off_mult_); 242 EXPECT_EQ(1ul, pm->update_back_off_mult_);
(...skipping 13 matching lines...) Expand all
262 256
263 // Initial state. No errors. 257 // Initial state. No errors.
264 EXPECT_EQ(0ul, pm->update_error_count_); 258 EXPECT_EQ(0ul, pm->update_error_count_);
265 EXPECT_EQ(1ul, pm->update_back_off_mult_); 259 EXPECT_EQ(1ul, pm->update_back_off_mult_);
266 expect_callback_to_be_called_ = false; 260 expect_callback_to_be_called_ = false;
267 pm->store_state_map_ = std::move(store_state_map_); 261 pm->store_state_map_ = std::move(store_state_map_);
268 pm->IssueUpdateRequest(); 262 pm->IssueUpdateRequest();
269 263
270 EXPECT_FALSE(pm->IsUpdateScheduled()); 264 EXPECT_FALSE(pm->IsUpdateScheduled());
271 265
272 runner->RunPendingTasks();
273
274 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 266 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
275 DCHECK(fetcher); 267 DCHECK(fetcher);
276 fetcher->set_status(net::URLRequestStatus()); 268 fetcher->set_status(net::URLRequestStatus());
277 // Response code of anything other than 200 should result in error. 269 // Response code of anything other than 200 should result in error.
278 fetcher->set_response_code(net::HTTP_NO_CONTENT); 270 fetcher->set_response_code(net::HTTP_NO_CONTENT);
279 fetcher->SetResponseString(""); 271 fetcher->SetResponseString("");
280 fetcher->delegate()->OnURLFetchComplete(fetcher); 272 fetcher->delegate()->OnURLFetchComplete(fetcher);
281 273
282 // Should have recorded one error, but back off multiplier is unchanged. 274 // Should have recorded one error, but back off multiplier is unchanged.
283 EXPECT_EQ(1ul, pm->update_error_count_); 275 EXPECT_EQ(1ul, pm->update_error_count_);
284 EXPECT_EQ(1ul, pm->update_back_off_mult_); 276 EXPECT_EQ(1ul, pm->update_back_off_mult_);
285 EXPECT_TRUE(pm->IsUpdateScheduled()); 277 EXPECT_TRUE(pm->IsUpdateScheduled());
286 278
287 // Retry, now no backoff. 279 // Retry, now no backoff.
288 expect_callback_to_be_called_ = true; 280 expect_callback_to_be_called_ = true;
281 // Call RunPendingTasks to ensure that the request is sent after backoff.
289 runner->RunPendingTasks(); 282 runner->RunPendingTasks();
290 283
291 fetcher = factory.GetFetcherByID(1); 284 fetcher = factory.GetFetcherByID(1);
292 DCHECK(fetcher); 285 DCHECK(fetcher);
293 fetcher->set_status(net::URLRequestStatus()); 286 fetcher->set_status(net::URLRequestStatus());
294 fetcher->set_response_code(net::HTTP_OK); 287 fetcher->set_response_code(net::HTTP_OK);
295 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 288 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
296 fetcher->delegate()->OnURLFetchComplete(fetcher); 289 fetcher->delegate()->OnURLFetchComplete(fetcher);
297 290
298 // No error, back off multiplier is unchanged. 291 // No error, back off multiplier is unchanged.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 pm->ScheduleNextUpdate(std::move(store_state_map_)); 326 pm->ScheduleNextUpdate(std::move(store_state_map_));
334 EXPECT_FALSE(pm->IsUpdateScheduled()); 327 EXPECT_FALSE(pm->IsUpdateScheduled());
335 328
336 runner->RunPendingTasks(); 329 runner->RunPendingTasks();
337 EXPECT_FALSE(pm->IsUpdateScheduled()); 330 EXPECT_FALSE(pm->IsUpdateScheduled());
338 331
339 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 332 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
340 DCHECK(!fetcher); 333 DCHECK(!fetcher);
341 } 334 }
342 335
336 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesHasTimeout) {
337 scoped_refptr<base::TestSimpleTaskRunner> runner(
338 new base::TestSimpleTaskRunner());
339 base::ThreadTaskRunnerHandle runner_handler(runner);
340 net::TestURLFetcherFactory factory;
341 std::vector<ListUpdateResponse> expected_lurs;
342 SetupExpectedListUpdateResponse(&expected_lurs);
343 std::unique_ptr<V4UpdateProtocolManager> pm(
344 CreateProtocolManager(expected_lurs));
345 runner->ClearPendingTasks();
346
347 // Initial state. No errors.
348 EXPECT_EQ(0ul, pm->update_error_count_);
349 EXPECT_EQ(1ul, pm->update_back_off_mult_);
350 expect_callback_to_be_called_ = true;
351 pm->store_state_map_ = std::move(store_state_map_);
352 pm->IssueUpdateRequest();
353
354 net::TestURLFetcher* timeout_fetcher = factory.GetFetcherByID(0);
355 DCHECK(timeout_fetcher);
356 // Don't set anything on the fetcher. Let it time out.
357 runner->RunPendingTasks();
358 // Now wait for the next request to be scheduled.
Nathan Parker 2016/11/08 23:39:29 Is there some state you can test between these to
vakh (use Gerrit instead) 2016/11/09 00:08:51 Done. Not much else to test.
359 runner->RunPendingTasks();
360
361 // There should be another fetcher now.
362 net::TestURLFetcher* fetcher = factory.GetFetcherByID(1);
363 DCHECK(fetcher);
364 fetcher->set_status(net::URLRequestStatus());
365 fetcher->set_response_code(net::HTTP_OK);
366 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
367 fetcher->delegate()->OnURLFetchComplete(fetcher);
368
369 // No error, back off multiplier is unchanged.
370 EXPECT_EQ(0ul, pm->update_error_count_);
371 EXPECT_EQ(1ul, pm->update_back_off_mult_);
372 }
373
343 } // namespace safe_browsing 374 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698