|
14 | 14 |
|
15 | 15 | #include "google/cloud/storage/internal/async/writer_connection_impl.h" |
16 | 16 | #include "google/cloud/mocks/mock_async_streaming_read_write_rpc.h" |
| 17 | +#include "google/cloud/storage/internal/async/write_object.h" |
17 | 18 | #include "google/cloud/storage/internal/crc32c.h" |
18 | 19 | #include "google/cloud/storage/internal/grpc/ctype_cord_workaround.h" |
19 | 20 | #include "google/cloud/storage/internal/hash_function_impl.h" |
@@ -648,55 +649,148 @@ TEST(AsyncWriterConnectionTest, UnexpectedQueryFailsWithoutError) { |
648 | 649 | EXPECT_THAT(query.get(), StatusIs(StatusCode::kInternal)); |
649 | 650 | } |
650 | 651 |
|
651 | | -TEST(AsyncWriterConnectionImpl, WriteHandleIsPropagatedAfterQuery) { |
| 652 | +TEST(AsyncWriterConnectionTest, WriteHandleIsUpdatedAfterQuery) { |
652 | 653 | AsyncSequencer<bool> sequencer; |
653 | 654 | auto mock = std::make_unique<MockStream>(); |
654 | | - int write_call_count = 0; |
| 655 | + std::vector<std::string> seen_handles; |
| 656 | + |
655 | 657 | EXPECT_CALL(*mock, Write) |
656 | | - .Times(2) |
| 658 | + .Times(3) |
657 | 659 | .WillRepeatedly([&](Request const& req, grpc::WriteOptions) { |
658 | 660 | EXPECT_TRUE(req.has_append_object_spec()); |
659 | 661 | EXPECT_TRUE(req.append_object_spec().has_write_handle()); |
660 | | - EXPECT_EQ(req.append_object_spec().write_handle().handle(), |
661 | | - "test-handle"); |
662 | | - ++write_call_count; |
| 662 | + seen_handles.push_back( |
| 663 | + req.append_object_spec().write_handle().handle()); |
663 | 664 | return sequencer.PushBack("Write"); |
664 | 665 | }); |
665 | | - EXPECT_CALL(*mock, Read).WillOnce([&]() { |
| 666 | + |
| 667 | + int read_call_count = 0; |
| 668 | + EXPECT_CALL(*mock, Read).Times(2).WillRepeatedly([&]() { |
666 | 669 | Response resp; |
667 | | - resp.mutable_write_handle()->set_handle("test-handle"); |
668 | | - resp.set_persisted_size(42); |
| 670 | + if (read_call_count == 0) { |
| 671 | + resp.mutable_write_handle()->set_handle("handle1"); |
| 672 | + resp.set_persisted_size(42); |
| 673 | + } else { |
| 674 | + resp.mutable_write_handle()->set_handle("handle2"); |
| 675 | + resp.set_persisted_size(43); |
| 676 | + } |
| 677 | + ++read_call_count; |
669 | 678 | return make_ready_future(absl::make_optional(std::move(resp))); |
670 | 679 | }); |
| 680 | + |
| 681 | + auto hash = std::make_shared<MockHashFunction>(); |
671 | 682 | EXPECT_CALL(*mock, Cancel).Times(1); |
672 | 683 | EXPECT_CALL(*mock, Finish).WillOnce([] { |
673 | 684 | return make_ready_future(Status{}); |
674 | 685 | }); |
| 686 | + EXPECT_CALL(*hash, Update(_, An<absl::Cord const&>(), _)).Times(3); |
675 | 687 |
|
676 | | - auto hash = std::make_shared<MockHashFunction>(); |
677 | 688 | google::storage::v2::BidiWriteObjectRequest req; |
678 | 689 | req.mutable_append_object_spec()->set_bucket("bucket"); |
679 | 690 | req.mutable_append_object_spec()->set_object("object"); |
680 | 691 |
|
681 | 692 | auto tested = std::make_unique<AsyncWriterConnectionImpl>( |
682 | 693 | TestOptions(), req, std::move(mock), hash, 0); |
683 | 694 |
|
| 695 | + // First Query sets handle1. |
684 | 696 | EXPECT_THAT(tested->Query().get(), IsOkAndHolds(42)); |
685 | 697 |
|
| 698 | + // First Write uses handle1. |
686 | 699 | auto result1 = tested->Write(WritePayload("payload1")); |
687 | 700 | auto next1 = sequencer.PopFrontWithName(); |
688 | 701 | ASSERT_THAT(next1.second, "Write"); |
689 | 702 | next1.first.set_value(true); |
690 | 703 | EXPECT_STATUS_OK(result1.get()); |
691 | 704 |
|
692 | | - // Second write should also include write_handle. |
| 705 | + // Second Query sets handle2. |
| 706 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(43)); |
| 707 | + |
| 708 | + // Second Write uses handle2. |
| 709 | + auto result2 = tested->Write(WritePayload("payload2")); |
| 710 | + auto next2 = sequencer.PopFrontWithName(); |
| 711 | + ASSERT_THAT(next2.second, "Write"); |
| 712 | + next2.first.set_value(true); |
| 713 | + EXPECT_STATUS_OK(result2.get()); |
| 714 | + |
| 715 | + // Third Write also uses handle2. |
| 716 | + auto result3 = tested->Write(WritePayload("payload3")); |
| 717 | + auto next3 = sequencer.PopFrontWithName(); |
| 718 | + ASSERT_THAT(next3.second, "Write"); |
| 719 | + next3.first.set_value(true); |
| 720 | + EXPECT_STATUS_OK(result3.get()); |
| 721 | + |
| 722 | + ASSERT_EQ(seen_handles.size(), 3); |
| 723 | + EXPECT_EQ(seen_handles[0], "handle1"); |
| 724 | + EXPECT_EQ(seen_handles[1], "handle2"); |
| 725 | + EXPECT_EQ(seen_handles[2], "handle2"); |
| 726 | +} |
| 727 | + |
| 728 | +TEST(AsyncWriterConnectionTest, WriteHandleIsUpdatedAfterResume) { |
| 729 | + AsyncSequencer<bool> sequencer; |
| 730 | + auto mock = std::make_unique<MockStream>(); |
| 731 | + std::vector<std::string> seen_handles; |
| 732 | + |
| 733 | + EXPECT_CALL(*mock, Write) |
| 734 | + .Times(2) |
| 735 | + .WillRepeatedly([&](Request const& req, grpc::WriteOptions) { |
| 736 | + EXPECT_TRUE(req.has_append_object_spec()); |
| 737 | + EXPECT_TRUE(req.append_object_spec().has_write_handle()); |
| 738 | + seen_handles.push_back( |
| 739 | + req.append_object_spec().write_handle().handle()); |
| 740 | + return sequencer.PushBack("Write"); |
| 741 | + }); |
| 742 | + |
| 743 | + EXPECT_CALL(*mock, Read) |
| 744 | + .WillOnce([&]() { |
| 745 | + Response resp; |
| 746 | + resp.mutable_write_handle()->set_handle("handle1"); |
| 747 | + resp.set_persisted_size(42); |
| 748 | + return make_ready_future(absl::make_optional(std::move(resp))); |
| 749 | + }) |
| 750 | + .WillOnce([&]() { |
| 751 | + Response resp; |
| 752 | + resp.mutable_write_handle()->set_handle("handle2"); |
| 753 | + resp.set_persisted_size(43); |
| 754 | + return make_ready_future(absl::make_optional(std::move(resp))); |
| 755 | + }); |
| 756 | + |
| 757 | + EXPECT_CALL(*mock, Cancel).Times(1); |
| 758 | + EXPECT_CALL(*mock, Finish).WillOnce([] { |
| 759 | + return make_ready_future(Status{}); |
| 760 | + }); |
| 761 | + |
| 762 | + auto hash = std::make_shared<MockHashFunction>(); |
| 763 | + EXPECT_CALL(*hash, Update(_, An<absl::Cord const&>(), _)).Times(2); |
| 764 | + |
| 765 | + google::storage::v2::BidiWriteObjectRequest req; |
| 766 | + req.mutable_append_object_spec()->set_bucket("bucket"); |
| 767 | + req.mutable_append_object_spec()->set_object("object"); |
| 768 | + |
| 769 | + auto tested = std::make_unique<AsyncWriterConnectionImpl>( |
| 770 | + TestOptions(), req, std::move(mock), hash, 0); |
| 771 | + |
| 772 | + // First Query sets handle1. |
| 773 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(42)); |
| 774 | + |
| 775 | + // First Write uses handle1 but fails. |
| 776 | + auto result1 = tested->Write(WritePayload("payload1")); |
| 777 | + auto next1 = sequencer.PopFrontWithName(); |
| 778 | + ASSERT_THAT(next1.second, "Write"); |
| 779 | + next1.first.set_value(false); |
| 780 | + |
| 781 | + // Simulate resume by calling Query again which returns handle2. |
| 782 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(43)); |
| 783 | + |
| 784 | + // Second Write should use handle2. |
693 | 785 | auto result2 = tested->Write(WritePayload("payload2")); |
694 | 786 | auto next2 = sequencer.PopFrontWithName(); |
695 | 787 | ASSERT_THAT(next2.second, "Write"); |
696 | 788 | next2.first.set_value(true); |
697 | 789 | EXPECT_STATUS_OK(result2.get()); |
698 | 790 |
|
699 | | - EXPECT_EQ(write_call_count, 2); |
| 791 | + ASSERT_EQ(seen_handles.size(), 2); |
| 792 | + EXPECT_EQ(seen_handles[0], "handle1"); |
| 793 | + EXPECT_EQ(seen_handles[1], "handle2"); |
700 | 794 | } |
701 | 795 |
|
702 | 796 | GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
|
0 commit comments