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