|
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" |
@@ -698,6 +699,150 @@ TEST(AsyncWriterConnectionTest, FinalizeAppendableNoChecksum) { |
698 | 699 | next.first.set_value(true); |
699 | 700 | } |
700 | 701 |
|
| 702 | +TEST(AsyncWriterConnectionTest, WriteHandleIsUpdatedAfterQuery) { |
| 703 | + AsyncSequencer<bool> sequencer; |
| 704 | + auto mock = std::make_unique<MockStream>(); |
| 705 | + std::vector<std::string> seen_handles; |
| 706 | + |
| 707 | + EXPECT_CALL(*mock, Write) |
| 708 | + .Times(3) |
| 709 | + .WillRepeatedly([&](Request const& req, grpc::WriteOptions) { |
| 710 | + EXPECT_TRUE(req.has_append_object_spec()); |
| 711 | + EXPECT_TRUE(req.append_object_spec().has_write_handle()); |
| 712 | + seen_handles.push_back( |
| 713 | + req.append_object_spec().write_handle().handle()); |
| 714 | + return sequencer.PushBack("Write"); |
| 715 | + }); |
| 716 | + |
| 717 | + int read_call_count = 0; |
| 718 | + EXPECT_CALL(*mock, Read).Times(2).WillRepeatedly([&]() { |
| 719 | + Response resp; |
| 720 | + if (read_call_count == 0) { |
| 721 | + resp.mutable_write_handle()->set_handle("handle1"); |
| 722 | + resp.set_persisted_size(42); |
| 723 | + } else { |
| 724 | + resp.mutable_write_handle()->set_handle("handle2"); |
| 725 | + resp.set_persisted_size(43); |
| 726 | + } |
| 727 | + ++read_call_count; |
| 728 | + return make_ready_future(absl::make_optional(std::move(resp))); |
| 729 | + }); |
| 730 | + |
| 731 | + auto hash = std::make_shared<MockHashFunction>(); |
| 732 | + EXPECT_CALL(*mock, Cancel).Times(1); |
| 733 | + EXPECT_CALL(*mock, Finish).WillOnce([] { |
| 734 | + return make_ready_future(Status{}); |
| 735 | + }); |
| 736 | + EXPECT_CALL(*hash, Update(_, An<absl::Cord const&>(), _)).Times(3); |
| 737 | + |
| 738 | + google::storage::v2::BidiWriteObjectRequest req; |
| 739 | + req.mutable_append_object_spec()->set_bucket("bucket"); |
| 740 | + req.mutable_append_object_spec()->set_object("object"); |
| 741 | + |
| 742 | + auto tested = std::make_unique<AsyncWriterConnectionImpl>( |
| 743 | + TestOptions(), req, std::move(mock), hash, 0); |
| 744 | + |
| 745 | + // First Query sets handle1. |
| 746 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(42)); |
| 747 | + |
| 748 | + // First Write uses handle1. |
| 749 | + auto result1 = tested->Write(WritePayload("payload1")); |
| 750 | + auto next1 = sequencer.PopFrontWithName(); |
| 751 | + ASSERT_THAT(next1.second, "Write"); |
| 752 | + next1.first.set_value(true); |
| 753 | + EXPECT_STATUS_OK(result1.get()); |
| 754 | + |
| 755 | + // Second Query sets handle2. |
| 756 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(43)); |
| 757 | + |
| 758 | + // Second Write uses handle2. |
| 759 | + auto result2 = tested->Write(WritePayload("payload2")); |
| 760 | + auto next2 = sequencer.PopFrontWithName(); |
| 761 | + ASSERT_THAT(next2.second, "Write"); |
| 762 | + next2.first.set_value(true); |
| 763 | + EXPECT_STATUS_OK(result2.get()); |
| 764 | + |
| 765 | + // Third Write also uses handle2. |
| 766 | + auto result3 = tested->Write(WritePayload("payload3")); |
| 767 | + auto next3 = sequencer.PopFrontWithName(); |
| 768 | + ASSERT_THAT(next3.second, "Write"); |
| 769 | + next3.first.set_value(true); |
| 770 | + EXPECT_STATUS_OK(result3.get()); |
| 771 | + |
| 772 | + ASSERT_EQ(seen_handles.size(), 3); |
| 773 | + EXPECT_EQ(seen_handles[0], "handle1"); |
| 774 | + EXPECT_EQ(seen_handles[1], "handle2"); |
| 775 | + EXPECT_EQ(seen_handles[2], "handle2"); |
| 776 | +} |
| 777 | + |
| 778 | +TEST(AsyncWriterConnectionTest, WriteHandleIsUpdatedAfterResume) { |
| 779 | + AsyncSequencer<bool> sequencer; |
| 780 | + auto mock = std::make_unique<MockStream>(); |
| 781 | + std::vector<std::string> seen_handles; |
| 782 | + |
| 783 | + EXPECT_CALL(*mock, Write) |
| 784 | + .Times(2) |
| 785 | + .WillRepeatedly([&](Request const& req, grpc::WriteOptions) { |
| 786 | + EXPECT_TRUE(req.has_append_object_spec()); |
| 787 | + EXPECT_TRUE(req.append_object_spec().has_write_handle()); |
| 788 | + seen_handles.push_back( |
| 789 | + req.append_object_spec().write_handle().handle()); |
| 790 | + return sequencer.PushBack("Write"); |
| 791 | + }); |
| 792 | + |
| 793 | + EXPECT_CALL(*mock, Read) |
| 794 | + .WillOnce([&]() { |
| 795 | + Response resp; |
| 796 | + resp.mutable_write_handle()->set_handle("handle1"); |
| 797 | + resp.set_persisted_size(42); |
| 798 | + return make_ready_future(absl::make_optional(std::move(resp))); |
| 799 | + }) |
| 800 | + .WillOnce([&]() { |
| 801 | + Response resp; |
| 802 | + resp.mutable_write_handle()->set_handle("handle2"); |
| 803 | + resp.set_persisted_size(43); |
| 804 | + return make_ready_future(absl::make_optional(std::move(resp))); |
| 805 | + }); |
| 806 | + |
| 807 | + EXPECT_CALL(*mock, Cancel).Times(1); |
| 808 | + EXPECT_CALL(*mock, Finish).WillOnce([] { |
| 809 | + return make_ready_future(Status{}); |
| 810 | + }); |
| 811 | + |
| 812 | + auto hash = std::make_shared<MockHashFunction>(); |
| 813 | + EXPECT_CALL(*hash, Update(_, An<absl::Cord const&>(), _)).Times(2); |
| 814 | + |
| 815 | + google::storage::v2::BidiWriteObjectRequest req; |
| 816 | + req.mutable_append_object_spec()->set_bucket("bucket"); |
| 817 | + req.mutable_append_object_spec()->set_object("object"); |
| 818 | + |
| 819 | + auto tested = std::make_unique<AsyncWriterConnectionImpl>( |
| 820 | + TestOptions(), req, std::move(mock), hash, 0); |
| 821 | + |
| 822 | + // First Query sets handle1. |
| 823 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(42)); |
| 824 | + |
| 825 | + // First Write uses handle1 but fails. |
| 826 | + auto result1 = tested->Write(WritePayload("payload1")); |
| 827 | + auto next1 = sequencer.PopFrontWithName(); |
| 828 | + ASSERT_THAT(next1.second, "Write"); |
| 829 | + next1.first.set_value(false); |
| 830 | + |
| 831 | + // Simulate resume by calling Query again which returns handle2. |
| 832 | + EXPECT_THAT(tested->Query().get(), IsOkAndHolds(43)); |
| 833 | + |
| 834 | + // Second Write should use handle2. |
| 835 | + auto result2 = tested->Write(WritePayload("payload2")); |
| 836 | + auto next2 = sequencer.PopFrontWithName(); |
| 837 | + ASSERT_THAT(next2.second, "Write"); |
| 838 | + next2.first.set_value(true); |
| 839 | + EXPECT_STATUS_OK(result2.get()); |
| 840 | + |
| 841 | + ASSERT_EQ(seen_handles.size(), 2); |
| 842 | + EXPECT_EQ(seen_handles[0], "handle1"); |
| 843 | + EXPECT_EQ(seen_handles[1], "handle2"); |
| 844 | +} |
| 845 | + |
701 | 846 | GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
702 | 847 | } // namespace storage_internal |
703 | 848 | } // namespace cloud |
|
0 commit comments