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

Side by Side Diff: sync/internal_api/attachments/attachment_service_impl_unittest.cc

Issue 569463002: Make AttachmentServiceImpl clear backoff when reconnected to network. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge with master. Created 6 years, 3 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "sync/internal_api/public/attachments/attachment_service_impl.h" 5 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/weak_ptr.h" 8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/timer/mock_timer.h"
11 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" 12 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h"
12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" 13 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h"
13 #include "testing/gmock/include/gmock/gmock-matchers.h" 14 #include "testing/gmock/include/gmock/gmock-matchers.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace syncer { 17 namespace syncer {
17 18
18 class MockAttachmentStore : public AttachmentStore, 19 class MockAttachmentStore : public AttachmentStore,
19 public base::SupportsWeakPtr<MockAttachmentStore> { 20 public base::SupportsWeakPtr<MockAttachmentStore> {
20 public: 21 public:
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 149
149 DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader); 150 DISALLOW_COPY_AND_ASSIGN(MockAttachmentUploader);
150 }; 151 };
151 152
152 class AttachmentServiceImplTest : public testing::Test, 153 class AttachmentServiceImplTest : public testing::Test,
153 public AttachmentService::Delegate { 154 public AttachmentService::Delegate {
154 protected: 155 protected:
155 AttachmentServiceImplTest() {} 156 AttachmentServiceImplTest() {}
156 157
157 virtual void SetUp() OVERRIDE { 158 virtual void SetUp() OVERRIDE {
159 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
158 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), 160 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()),
159 make_scoped_ptr(new MockAttachmentDownloader()), 161 make_scoped_ptr(new MockAttachmentDownloader()),
160 this); 162 this);
161 } 163 }
162 164
163 virtual void TearDown() OVERRIDE { 165 virtual void TearDown() OVERRIDE {
164 attachment_service_.reset(); 166 attachment_service_.reset();
165 ASSERT_FALSE(attachment_store_); 167 ASSERT_FALSE(attachment_store_);
166 ASSERT_FALSE(attachment_uploader_); 168 ASSERT_FALSE(attachment_uploader_);
167 ASSERT_FALSE(attachment_downloader_); 169 ASSERT_FALSE(attachment_downloader_);
(...skipping 17 matching lines...) Expand all
185 attachment_uploader_ = uploader->AsWeakPtr(); 187 attachment_uploader_ = uploader->AsWeakPtr();
186 } 188 }
187 if (downloader.get()) { 189 if (downloader.get()) {
188 attachment_downloader_ = downloader->AsWeakPtr(); 190 attachment_downloader_ = downloader->AsWeakPtr();
189 } 191 }
190 attachment_service_.reset( 192 attachment_service_.reset(
191 new AttachmentServiceImpl(attachment_store, 193 new AttachmentServiceImpl(attachment_store,
192 uploader.PassAs<AttachmentUploader>(), 194 uploader.PassAs<AttachmentUploader>(),
193 downloader.PassAs<AttachmentDownloader>(), 195 downloader.PassAs<AttachmentDownloader>(),
194 delegate, 196 delegate,
195 base::TimeDelta(), 197 base::TimeDelta::FromMinutes(1),
196 base::TimeDelta())); 198 base::TimeDelta::FromMinutes(8)));
199
200 scoped_ptr<base::MockTimer> timer_to_pass(
201 new base::MockTimer(false, false));
202 mock_timer_ = timer_to_pass.get();
203 attachment_service_->SetTimerForTest(timer_to_pass.PassAs<base::Timer>());
197 } 204 }
198 205
199 AttachmentService* attachment_service() { return attachment_service_.get(); } 206 AttachmentService* attachment_service() { return attachment_service_.get(); }
200 207
208 base::MockTimer* mock_timer() { return mock_timer_; }
209
201 AttachmentService::GetOrDownloadCallback download_callback() { 210 AttachmentService::GetOrDownloadCallback download_callback() {
202 return base::Bind(&AttachmentServiceImplTest::DownloadDone, 211 return base::Bind(&AttachmentServiceImplTest::DownloadDone,
203 base::Unretained(this)); 212 base::Unretained(this));
204 } 213 }
205 214
206 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, 215 void DownloadDone(const AttachmentService::GetOrDownloadResult& result,
207 scoped_ptr<AttachmentMap> attachments) { 216 scoped_ptr<AttachmentMap> attachments) {
208 download_results_.push_back(result); 217 download_results_.push_back(result);
209 last_download_attachments_ = attachments.Pass(); 218 last_download_attachments_ = attachments.Pass();
210 } 219 }
211 220
212 void RunLoop() { 221 void RunLoop() {
213 base::RunLoop run_loop; 222 base::RunLoop run_loop;
214 run_loop.RunUntilIdle(); 223 run_loop.RunUntilIdle();
215 } 224 }
216 225
226 void RunLoopAndFireTimer() {
227 RunLoop();
228 if (mock_timer()->IsRunning()) {
229 mock_timer()->Fire();
230 }
231 RunLoop();
232 }
233
217 const std::vector<AttachmentService::GetOrDownloadResult>& 234 const std::vector<AttachmentService::GetOrDownloadResult>&
218 download_results() const { 235 download_results() const {
219 return download_results_; 236 return download_results_;
220 } 237 }
221 238
222 const AttachmentMap& last_download_attachments() const { 239 const AttachmentMap& last_download_attachments() const {
223 return *last_download_attachments_.get(); 240 return *last_download_attachments_.get();
224 } 241 }
225 242
243 net::NetworkChangeNotifier* network_change_notifier() {
244 return network_change_notifier_.get();
245 }
246
226 MockAttachmentStore* store() { return attachment_store_.get(); } 247 MockAttachmentStore* store() { return attachment_store_.get(); }
227 248
228 MockAttachmentDownloader* downloader() { 249 MockAttachmentDownloader* downloader() {
229 return attachment_downloader_.get(); 250 return attachment_downloader_.get();
230 } 251 }
231 252
232 MockAttachmentUploader* uploader() { 253 MockAttachmentUploader* uploader() {
233 return attachment_uploader_.get(); 254 return attachment_uploader_.get();
234 } 255 }
235 256
236 const std::vector<AttachmentId>& on_attachment_uploaded_list() const { 257 const std::vector<AttachmentId>& on_attachment_uploaded_list() const {
237 return on_attachment_uploaded_list_; 258 return on_attachment_uploaded_list_;
238 } 259 }
239 260
240 private: 261 private:
241 base::MessageLoop message_loop_; 262 base::MessageLoop message_loop_;
263 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
242 base::WeakPtr<MockAttachmentStore> attachment_store_; 264 base::WeakPtr<MockAttachmentStore> attachment_store_;
243 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; 265 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_;
244 base::WeakPtr<MockAttachmentUploader> attachment_uploader_; 266 base::WeakPtr<MockAttachmentUploader> attachment_uploader_;
245 scoped_ptr<AttachmentService> attachment_service_; 267 scoped_ptr<AttachmentServiceImpl> attachment_service_;
268 base::MockTimer* mock_timer_; // not owned
246 269
247 std::vector<AttachmentService::GetOrDownloadResult> download_results_; 270 std::vector<AttachmentService::GetOrDownloadResult> download_results_;
248 scoped_ptr<AttachmentMap> last_download_attachments_; 271 scoped_ptr<AttachmentMap> last_download_attachments_;
249 std::vector<AttachmentId> on_attachment_uploaded_list_; 272 std::vector<AttachmentId> on_attachment_uploaded_list_;
250 }; 273 };
251 274
252 TEST_F(AttachmentServiceImplTest, GetStore) { 275 TEST_F(AttachmentServiceImplTest, GetStore) {
253 EXPECT_EQ(store(), attachment_service()->GetStore()); 276 EXPECT_EQ(store(), attachment_service()->GetStore());
254 } 277 }
255 278
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 369
347 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { 370 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) {
348 AttachmentIdSet attachment_ids; 371 AttachmentIdSet attachment_ids;
349 const unsigned num_attachments = 3; 372 const unsigned num_attachments = 3;
350 for (unsigned i = 0; i < num_attachments; ++i) { 373 for (unsigned i = 0; i < num_attachments; ++i) {
351 attachment_ids.insert(AttachmentId::Create()); 374 attachment_ids.insert(AttachmentId::Create());
352 } 375 }
353 attachment_service()->UploadAttachments(attachment_ids); 376 attachment_service()->UploadAttachments(attachment_ids);
354 377
355 for (unsigned i = 0; i < num_attachments; ++i) { 378 for (unsigned i = 0; i < num_attachments; ++i) {
356 RunLoop(); 379 RunLoopAndFireTimer();
357 // See that the service has issued a read for at least one of the 380 // See that the service has issued a read for at least one of the
358 // attachments. 381 // attachments.
359 ASSERT_GE(1U, store()->read_ids.size()); 382 ASSERT_GE(store()->read_ids.size(), 1U);
360 store()->RespondToRead(attachment_ids); 383 store()->RespondToRead(attachment_ids);
361 RunLoop(); 384 RunLoop();
362 ASSERT_GE(1U, uploader()->upload_requests.size()); 385 ASSERT_GE(uploader()->upload_requests.size(), 1U);
363 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, 386 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first,
364 AttachmentUploader::UPLOAD_SUCCESS); 387 AttachmentUploader::UPLOAD_SUCCESS);
365 } 388 }
366 RunLoop(); 389 RunLoop();
367 ASSERT_EQ(0U, store()->read_ids.size()); 390 ASSERT_EQ(0U, store()->read_ids.size());
368 ASSERT_EQ(0U, uploader()->upload_requests.size()); 391 ASSERT_EQ(0U, uploader()->upload_requests.size());
369 392
370 // See that all the attachments were uploaded. 393 // See that all the attachments were uploaded.
371 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); 394 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size());
372 AttachmentIdSet::const_iterator iter = attachment_ids.begin(); 395 AttachmentIdSet::const_iterator iter = attachment_ids.begin();
373 const AttachmentIdSet::const_iterator end = attachment_ids.end(); 396 const AttachmentIdSet::const_iterator end = attachment_ids.end();
374 for (iter = attachment_ids.begin(); iter != end; ++iter) { 397 for (iter = attachment_ids.begin(); iter != end; ++iter) {
375 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); 398 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter));
376 } 399 }
377 } 400 }
378 401
379 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { 402 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) {
380 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), 403 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()),
381 make_scoped_ptr(new MockAttachmentDownloader()), 404 make_scoped_ptr(new MockAttachmentDownloader()),
382 NULL); // No delegate. 405 NULL); // No delegate.
383 406
384 AttachmentIdSet attachment_ids; 407 AttachmentIdSet attachment_ids;
385 attachment_ids.insert(AttachmentId::Create()); 408 attachment_ids.insert(AttachmentId::Create());
386 attachment_service()->UploadAttachments(attachment_ids); 409 attachment_service()->UploadAttachments(attachment_ids);
387 RunLoop(); 410 RunLoopAndFireTimer();
388 EXPECT_EQ(1U, store()->read_ids.size()); 411 ASSERT_EQ(1U, store()->read_ids.size());
389 EXPECT_EQ(0U, uploader()->upload_requests.size()); 412 ASSERT_EQ(0U, uploader()->upload_requests.size());
390 store()->RespondToRead(attachment_ids); 413 store()->RespondToRead(attachment_ids);
391 RunLoop(); 414 RunLoop();
392 EXPECT_EQ(0U, store()->read_ids.size()); 415 ASSERT_EQ(0U, store()->read_ids.size());
393 EXPECT_EQ(1U, uploader()->upload_requests.size()); 416 ASSERT_EQ(1U, uploader()->upload_requests.size());
394 uploader()->RespondToUpload(*attachment_ids.begin(), 417 uploader()->RespondToUpload(*attachment_ids.begin(),
395 AttachmentUploader::UPLOAD_SUCCESS); 418 AttachmentUploader::UPLOAD_SUCCESS);
396 RunLoop(); 419 RunLoop();
397 ASSERT_TRUE(on_attachment_uploaded_list().empty()); 420 ASSERT_TRUE(on_attachment_uploaded_list().empty());
398 } 421 }
399 422
400 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { 423 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) {
401 AttachmentIdSet attachment_ids; 424 AttachmentIdSet attachment_ids;
402 attachment_ids.insert(AttachmentId::Create()); 425 attachment_ids.insert(AttachmentId::Create());
403 attachment_ids.insert(AttachmentId::Create()); 426 attachment_ids.insert(AttachmentId::Create());
404 attachment_service()->UploadAttachments(attachment_ids); 427 attachment_service()->UploadAttachments(attachment_ids);
405 RunLoop(); 428 RunLoopAndFireTimer();
406 ASSERT_GE(1U, store()->read_ids.size()); 429 ASSERT_GE(store()->read_ids.size(), 1U);
407 430
408 ASSERT_EQ(0U, uploader()->upload_requests.size()); 431 ASSERT_EQ(0U, uploader()->upload_requests.size());
409 store()->RespondToRead(attachment_ids); 432 store()->RespondToRead(attachment_ids);
410 RunLoop(); 433 RunLoop();
411 ASSERT_EQ(1U, uploader()->upload_requests.size()); 434 ASSERT_EQ(1U, uploader()->upload_requests.size());
412 435
413 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, 436 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first,
414 AttachmentUploader::UPLOAD_SUCCESS); 437 AttachmentUploader::UPLOAD_SUCCESS);
415 RunLoop(); 438 RunLoopAndFireTimer();
416 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); 439 ASSERT_EQ(1U, on_attachment_uploaded_list().size());
417 ASSERT_GE(1U, store()->read_ids.size()); 440 ASSERT_GE(store()->read_ids.size(), 1U);
418 // Not found! 441 // Not found!
419 store()->RespondToRead(AttachmentIdSet()); 442 store()->RespondToRead(AttachmentIdSet());
420 RunLoop(); 443 RunLoop();
421 // No upload requests since the read failed. 444 // No upload requests since the read failed.
422 ASSERT_EQ(0U, uploader()->upload_requests.size()); 445 ASSERT_EQ(0U, uploader()->upload_requests.size());
423 } 446 }
424 447
425 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { 448 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) {
426 AttachmentIdSet attachment_ids; 449 AttachmentIdSet attachment_ids;
427 const unsigned num_attachments = 2; 450 const unsigned num_attachments = 2;
428 for (unsigned i = 0; i < num_attachments; ++i) { 451 for (unsigned i = 0; i < num_attachments; ++i) {
429 attachment_ids.insert(AttachmentId::Create()); 452 attachment_ids.insert(AttachmentId::Create());
430 } 453 }
431 attachment_service()->UploadAttachments(attachment_ids); 454 attachment_service()->UploadAttachments(attachment_ids);
432 455
433 for (unsigned i = 0; i < num_attachments; ++i) { 456 for (unsigned i = 0; i < num_attachments; ++i) {
434 RunLoop(); 457 RunLoopAndFireTimer();
435 ASSERT_GE(1U, store()->read_ids.size()); 458 ASSERT_GE(store()->read_ids.size(), 1U);
436 // None found! 459 // None found!
437 store()->RespondToRead(AttachmentIdSet()); 460 store()->RespondToRead(AttachmentIdSet());
438 } 461 }
439 RunLoop(); 462 RunLoop();
440 463
441 // Nothing uploaded. 464 // Nothing uploaded.
442 EXPECT_EQ(0U, uploader()->upload_requests.size()); 465 EXPECT_EQ(0U, uploader()->upload_requests.size());
443 // See that the delegate was never called. 466 // See that the delegate was never called.
444 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); 467 ASSERT_EQ(0U, on_attachment_uploaded_list().size());
445 } 468 }
(...skipping 13 matching lines...) Expand all
459 482
460 // Upload three attachments. For one of them, server responds with error. 483 // Upload three attachments. For one of them, server responds with error.
461 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { 484 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) {
462 AttachmentIdSet attachment_ids; 485 AttachmentIdSet attachment_ids;
463 const unsigned num_attachments = 3; 486 const unsigned num_attachments = 3;
464 for (unsigned i = 0; i < num_attachments; ++i) { 487 for (unsigned i = 0; i < num_attachments; ++i) {
465 attachment_ids.insert(AttachmentId::Create()); 488 attachment_ids.insert(AttachmentId::Create());
466 } 489 }
467 attachment_service()->UploadAttachments(attachment_ids); 490 attachment_service()->UploadAttachments(attachment_ids);
468 491
469 for (unsigned i = 0; i < num_attachments; ++i) { 492 for (unsigned i = 0; i < 3; ++i) {
470 RunLoop(); 493 RunLoopAndFireTimer();
471 ASSERT_GE(1U, store()->read_ids.size()); 494 ASSERT_GE(store()->read_ids.size(), 1U);
472 store()->RespondToRead(attachment_ids); 495 store()->RespondToRead(attachment_ids);
473 RunLoop(); 496 RunLoop();
474 ASSERT_EQ(1U, uploader()->upload_requests.size()); 497 ASSERT_EQ(1U, uploader()->upload_requests.size());
475 AttachmentUploader::UploadResult result = 498 AttachmentUploader::UploadResult result =
476 AttachmentUploader::UPLOAD_SUCCESS; 499 AttachmentUploader::UPLOAD_SUCCESS;
477 // Fail the 2nd one. 500 // Fail the 2nd one.
478 if (i == 2U) { 501 if (i == 2U) {
479 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; 502 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR;
480 } else { 503 } else {
481 result = AttachmentUploader::UPLOAD_SUCCESS; 504 result = AttachmentUploader::UPLOAD_SUCCESS;
482 } 505 }
483 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, 506 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first,
484 result); 507 result);
508 RunLoop();
485 } 509 }
486 RunLoop();
487 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); 510 ASSERT_EQ(2U, on_attachment_uploaded_list().size());
488 } 511 }
489 512
513 // Attempt an upload, respond with transient error to trigger backoff, issue
514 // network disconnect/connect events and see that backoff is cleared.
515 TEST_F(AttachmentServiceImplTest,
516 UploadAttachments_ResetBackoffAfterNetworkChange) {
517 AttachmentIdSet attachment_ids;
518 attachment_ids.insert(AttachmentId::Create());
519 attachment_service()->UploadAttachments(attachment_ids);
520
521 RunLoopAndFireTimer();
522 ASSERT_EQ(1U, store()->read_ids.size());
523 store()->RespondToRead(attachment_ids);
524 RunLoop();
525 ASSERT_EQ(1U, uploader()->upload_requests.size());
526
527 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first,
528 AttachmentUploader::UPLOAD_TRANSIENT_ERROR);
529 RunLoop();
530
531 // See that we are in backoff.
532 ASSERT_TRUE(mock_timer()->IsRunning());
533 ASSERT_GT(mock_timer()->GetCurrentDelay(), base::TimeDelta());
534
535 // Issue a network disconnect event.
536 network_change_notifier()->NotifyObserversOfNetworkChangeForTests(
537 net::NetworkChangeNotifier::CONNECTION_NONE);
538 RunLoop();
539
540 // Still in backoff.
541 ASSERT_TRUE(mock_timer()->IsRunning());
542 ASSERT_GT(mock_timer()->GetCurrentDelay(), base::TimeDelta());
543
544 // Issue a network connect event.
545 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
546 net::NetworkChangeNotifier::CONNECTION_WIFI);
547 RunLoop();
548
549 // No longer in backoff.
550 ASSERT_TRUE(mock_timer()->IsRunning());
551 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay());
552 }
553
490 } // namespace syncer 554 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/attachments/attachment_service_impl.cc ('k') | sync/internal_api/attachments/task_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698