| OLD | NEW |
| 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" |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 } | 253 } |
| 254 | 254 |
| 255 void RunLoopAndFireTimer() { | 255 void RunLoopAndFireTimer() { |
| 256 RunLoop(); | 256 RunLoop(); |
| 257 if (mock_timer()->IsRunning()) { | 257 if (mock_timer()->IsRunning()) { |
| 258 mock_timer()->Fire(); | 258 mock_timer()->Fire(); |
| 259 RunLoop(); | 259 RunLoop(); |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 | 262 |
| 263 static AttachmentIdSet AttachmentIdSetFromList( |
| 264 const AttachmentIdList& id_list) { |
| 265 AttachmentIdSet id_set; |
| 266 std::copy(id_list.begin(), id_list.end(), |
| 267 std::inserter(id_set, id_set.end())); |
| 268 return id_set; |
| 269 } |
| 270 |
| 263 const std::vector<AttachmentService::GetOrDownloadResult>& | 271 const std::vector<AttachmentService::GetOrDownloadResult>& |
| 264 download_results() const { | 272 download_results() const { |
| 265 return download_results_; | 273 return download_results_; |
| 266 } | 274 } |
| 267 | 275 |
| 268 const AttachmentMap& last_download_attachments() const { | 276 const AttachmentMap& last_download_attachments() const { |
| 269 return *last_download_attachments_.get(); | 277 return *last_download_attachments_.get(); |
| 270 } | 278 } |
| 271 | 279 |
| 272 net::NetworkChangeNotifier* network_change_notifier() { | 280 net::NetworkChangeNotifier* network_change_notifier() { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 | 421 |
| 414 AttachmentIdSet local_attachments; | 422 AttachmentIdSet local_attachments; |
| 415 store()->RespondToRead(local_attachments); | 423 store()->RespondToRead(local_attachments); |
| 416 RunLoop(); | 424 RunLoop(); |
| 417 ASSERT_EQ(1U, download_results().size()); | 425 ASSERT_EQ(1U, download_results().size()); |
| 418 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); | 426 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); |
| 419 EXPECT_TRUE(last_download_attachments().empty()); | 427 EXPECT_TRUE(last_download_attachments().empty()); |
| 420 } | 428 } |
| 421 | 429 |
| 422 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { | 430 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { |
| 423 AttachmentIdSet attachment_ids; | 431 AttachmentIdList attachment_ids; |
| 424 const unsigned num_attachments = 3; | 432 const unsigned num_attachments = 3; |
| 425 for (unsigned i = 0; i < num_attachments; ++i) { | 433 for (unsigned i = 0; i < num_attachments; ++i) { |
| 426 attachment_ids.insert(AttachmentId::Create(0, 0)); | 434 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 427 } | 435 } |
| 428 attachment_service()->UploadAttachments(attachment_ids); | 436 attachment_service()->UploadAttachments(attachment_ids); |
| 429 | 437 |
| 430 for (unsigned i = 0; i < num_attachments; ++i) { | 438 for (unsigned i = 0; i < num_attachments; ++i) { |
| 431 RunLoopAndFireTimer(); | 439 RunLoopAndFireTimer(); |
| 432 // See that the service has issued a read for at least one of the | 440 // See that the service has issued a read for at least one of the |
| 433 // attachments. | 441 // attachments. |
| 434 ASSERT_GE(store()->read_ids.size(), 1U); | 442 ASSERT_GE(store()->read_ids.size(), 1U); |
| 435 store()->RespondToRead(attachment_ids); | 443 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 436 RunLoop(); | 444 RunLoop(); |
| 437 ASSERT_GE(uploader()->upload_requests.size(), 1U); | 445 ASSERT_GE(uploader()->upload_requests.size(), 1U); |
| 438 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 446 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 439 AttachmentUploader::UPLOAD_SUCCESS); | 447 AttachmentUploader::UPLOAD_SUCCESS); |
| 440 } | 448 } |
| 441 RunLoop(); | 449 RunLoop(); |
| 442 ASSERT_EQ(0U, store()->read_ids.size()); | 450 ASSERT_EQ(0U, store()->read_ids.size()); |
| 443 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 451 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 444 | 452 |
| 445 // See that all the attachments were uploaded. | 453 // See that all the attachments were uploaded. |
| 446 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); | 454 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); |
| 447 AttachmentIdSet::const_iterator iter = attachment_ids.begin(); | 455 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); |
| 448 const AttachmentIdSet::const_iterator end = attachment_ids.end(); | 456 ++iter) { |
| 449 for (iter = attachment_ids.begin(); iter != end; ++iter) { | |
| 450 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); | 457 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); |
| 451 } | 458 } |
| 452 } | 459 } |
| 453 | 460 |
| 454 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { | 461 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { |
| 455 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 462 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), |
| 456 make_scoped_ptr(new MockAttachmentDownloader()), | 463 make_scoped_ptr(new MockAttachmentDownloader()), |
| 457 NULL); // No delegate. | 464 NULL); // No delegate. |
| 458 | 465 |
| 459 AttachmentIdSet attachment_ids; | 466 AttachmentIdList attachment_ids; |
| 460 attachment_ids.insert(AttachmentId::Create(0, 0)); | 467 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 461 attachment_service()->UploadAttachments(attachment_ids); | 468 attachment_service()->UploadAttachments(attachment_ids); |
| 462 RunLoopAndFireTimer(); | 469 RunLoopAndFireTimer(); |
| 463 ASSERT_EQ(1U, store()->read_ids.size()); | 470 ASSERT_EQ(1U, store()->read_ids.size()); |
| 464 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 471 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 465 store()->RespondToRead(attachment_ids); | 472 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 466 RunLoop(); | 473 RunLoop(); |
| 467 ASSERT_EQ(0U, store()->read_ids.size()); | 474 ASSERT_EQ(0U, store()->read_ids.size()); |
| 468 ASSERT_EQ(1U, uploader()->upload_requests.size()); | 475 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
| 469 uploader()->RespondToUpload(*attachment_ids.begin(), | 476 uploader()->RespondToUpload(*attachment_ids.begin(), |
| 470 AttachmentUploader::UPLOAD_SUCCESS); | 477 AttachmentUploader::UPLOAD_SUCCESS); |
| 471 RunLoop(); | 478 RunLoop(); |
| 472 ASSERT_TRUE(on_attachment_uploaded_list().empty()); | 479 ASSERT_TRUE(on_attachment_uploaded_list().empty()); |
| 473 } | 480 } |
| 474 | 481 |
| 475 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { | 482 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { |
| 476 AttachmentIdSet attachment_ids; | 483 AttachmentIdList attachment_ids; |
| 477 attachment_ids.insert(AttachmentId::Create(0, 0)); | 484 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 478 attachment_ids.insert(AttachmentId::Create(0, 0)); | 485 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 479 attachment_service()->UploadAttachments(attachment_ids); | 486 attachment_service()->UploadAttachments(attachment_ids); |
| 480 RunLoopAndFireTimer(); | 487 RunLoopAndFireTimer(); |
| 481 ASSERT_GE(store()->read_ids.size(), 1U); | 488 ASSERT_GE(store()->read_ids.size(), 1U); |
| 482 | 489 |
| 483 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 490 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 484 store()->RespondToRead(attachment_ids); | 491 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 485 RunLoop(); | 492 RunLoop(); |
| 486 ASSERT_EQ(1U, uploader()->upload_requests.size()); | 493 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
| 487 | 494 |
| 488 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 495 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 489 AttachmentUploader::UPLOAD_SUCCESS); | 496 AttachmentUploader::UPLOAD_SUCCESS); |
| 490 RunLoopAndFireTimer(); | 497 RunLoopAndFireTimer(); |
| 491 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); | 498 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); |
| 492 ASSERT_GE(store()->read_ids.size(), 1U); | 499 ASSERT_GE(store()->read_ids.size(), 1U); |
| 493 // Not found! | 500 // Not found! |
| 494 store()->RespondToRead(AttachmentIdSet()); | 501 store()->RespondToRead(AttachmentIdSet()); |
| 495 RunLoop(); | 502 RunLoop(); |
| 496 // No upload requests since the read failed. | 503 // No upload requests since the read failed. |
| 497 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 504 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 498 } | 505 } |
| 499 | 506 |
| 500 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { | 507 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { |
| 501 AttachmentIdSet attachment_ids; | 508 AttachmentIdList attachment_ids; |
| 502 const unsigned num_attachments = 2; | 509 const unsigned num_attachments = 2; |
| 503 for (unsigned i = 0; i < num_attachments; ++i) { | 510 for (unsigned i = 0; i < num_attachments; ++i) { |
| 504 attachment_ids.insert(AttachmentId::Create(0, 0)); | 511 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 505 } | 512 } |
| 506 attachment_service()->UploadAttachments(attachment_ids); | 513 attachment_service()->UploadAttachments(attachment_ids); |
| 507 | 514 |
| 508 for (unsigned i = 0; i < num_attachments; ++i) { | 515 for (unsigned i = 0; i < num_attachments; ++i) { |
| 509 RunLoopAndFireTimer(); | 516 RunLoopAndFireTimer(); |
| 510 ASSERT_GE(store()->read_ids.size(), 1U); | 517 ASSERT_GE(store()->read_ids.size(), 1U); |
| 511 // None found! | 518 // None found! |
| 512 store()->RespondToRead(AttachmentIdSet()); | 519 store()->RespondToRead(AttachmentIdSet()); |
| 513 } | 520 } |
| 514 RunLoop(); | 521 RunLoop(); |
| 515 | 522 |
| 516 // Nothing uploaded. | 523 // Nothing uploaded. |
| 517 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 524 EXPECT_EQ(0U, uploader()->upload_requests.size()); |
| 518 // See that the delegate was never called. | 525 // See that the delegate was never called. |
| 519 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 526 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
| 520 } | 527 } |
| 521 | 528 |
| 522 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { | 529 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { |
| 523 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), | 530 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), |
| 524 make_scoped_ptr(new MockAttachmentDownloader()), | 531 make_scoped_ptr(new MockAttachmentDownloader()), |
| 525 this); | 532 this); |
| 526 | 533 |
| 527 AttachmentIdSet attachment_ids; | 534 AttachmentIdList attachment_ids; |
| 528 attachment_ids.insert(AttachmentId::Create(0, 0)); | 535 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 529 attachment_service()->UploadAttachments(attachment_ids); | 536 attachment_service()->UploadAttachments(attachment_ids); |
| 530 RunLoop(); | 537 RunLoop(); |
| 531 EXPECT_EQ(0U, store()->read_ids.size()); | 538 EXPECT_EQ(0U, store()->read_ids.size()); |
| 532 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 539 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
| 533 } | 540 } |
| 534 | 541 |
| 535 // Upload three attachments. For one of them, server responds with error. | 542 // Upload three attachments. For one of them, server responds with error. |
| 536 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { | 543 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { |
| 537 AttachmentIdSet attachment_ids; | 544 AttachmentIdList attachment_ids; |
| 538 const unsigned num_attachments = 3; | 545 const unsigned num_attachments = 3; |
| 539 for (unsigned i = 0; i < num_attachments; ++i) { | 546 for (unsigned i = 0; i < num_attachments; ++i) { |
| 540 attachment_ids.insert(AttachmentId::Create(0, 0)); | 547 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 541 } | 548 } |
| 542 attachment_service()->UploadAttachments(attachment_ids); | 549 attachment_service()->UploadAttachments(attachment_ids); |
| 543 | 550 |
| 544 for (unsigned i = 0; i < 3; ++i) { | 551 for (unsigned i = 0; i < 3; ++i) { |
| 545 RunLoopAndFireTimer(); | 552 RunLoopAndFireTimer(); |
| 546 ASSERT_GE(store()->read_ids.size(), 1U); | 553 ASSERT_GE(store()->read_ids.size(), 1U); |
| 547 store()->RespondToRead(attachment_ids); | 554 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 548 RunLoop(); | 555 RunLoop(); |
| 549 ASSERT_EQ(1U, uploader()->upload_requests.size()); | 556 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
| 550 AttachmentUploader::UploadResult result = | 557 AttachmentUploader::UploadResult result = |
| 551 AttachmentUploader::UPLOAD_SUCCESS; | 558 AttachmentUploader::UPLOAD_SUCCESS; |
| 552 // Fail the 2nd one. | 559 // Fail the 2nd one. |
| 553 if (i == 2U) { | 560 if (i == 2U) { |
| 554 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; | 561 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; |
| 555 } else { | 562 } else { |
| 556 result = AttachmentUploader::UPLOAD_SUCCESS; | 563 result = AttachmentUploader::UPLOAD_SUCCESS; |
| 557 } | 564 } |
| 558 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 565 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 559 result); | 566 result); |
| 560 RunLoop(); | 567 RunLoop(); |
| 561 } | 568 } |
| 562 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); | 569 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); |
| 563 } | 570 } |
| 564 | 571 |
| 565 // Attempt an upload, respond with transient error to trigger backoff, issue | 572 // Attempt an upload, respond with transient error to trigger backoff, issue |
| 566 // network disconnect/connect events and see that backoff is cleared. | 573 // network disconnect/connect events and see that backoff is cleared. |
| 567 TEST_F(AttachmentServiceImplTest, | 574 TEST_F(AttachmentServiceImplTest, |
| 568 UploadAttachments_ResetBackoffAfterNetworkChange) { | 575 UploadAttachments_ResetBackoffAfterNetworkChange) { |
| 569 AttachmentIdSet attachment_ids; | 576 AttachmentIdList attachment_ids; |
| 570 attachment_ids.insert(AttachmentId::Create(0, 0)); | 577 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 571 attachment_service()->UploadAttachments(attachment_ids); | 578 attachment_service()->UploadAttachments(attachment_ids); |
| 572 | 579 |
| 573 RunLoopAndFireTimer(); | 580 RunLoopAndFireTimer(); |
| 574 ASSERT_EQ(1U, store()->read_ids.size()); | 581 ASSERT_EQ(1U, store()->read_ids.size()); |
| 575 store()->RespondToRead(attachment_ids); | 582 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 576 RunLoop(); | 583 RunLoop(); |
| 577 ASSERT_EQ(1U, uploader()->upload_requests.size()); | 584 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
| 578 | 585 |
| 579 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 586 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 580 AttachmentUploader::UPLOAD_TRANSIENT_ERROR); | 587 AttachmentUploader::UPLOAD_TRANSIENT_ERROR); |
| 581 RunLoop(); | 588 RunLoop(); |
| 582 | 589 |
| 583 // See that we are in backoff. | 590 // See that we are in backoff. |
| 584 ASSERT_TRUE(mock_timer()->IsRunning()); | 591 ASSERT_TRUE(mock_timer()->IsRunning()); |
| 585 ASSERT_GT(mock_timer()->GetCurrentDelay(), base::TimeDelta()); | 592 ASSERT_GT(mock_timer()->GetCurrentDelay(), base::TimeDelta()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 597 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 604 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
| 598 net::NetworkChangeNotifier::CONNECTION_WIFI); | 605 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 599 RunLoop(); | 606 RunLoop(); |
| 600 | 607 |
| 601 // No longer in backoff. | 608 // No longer in backoff. |
| 602 ASSERT_TRUE(mock_timer()->IsRunning()); | 609 ASSERT_TRUE(mock_timer()->IsRunning()); |
| 603 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); | 610 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); |
| 604 } | 611 } |
| 605 | 612 |
| 606 } // namespace syncer | 613 } // namespace syncer |
| OLD | NEW |