|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/pubsub/internal/subscriber_connection_impl.h" |
| 16 | +#include "google/cloud/pubsub/ack_handler.h" |
| 17 | +#include "google/cloud/pubsub/exactly_once_ack_handler.h" |
| 18 | +#include "google/cloud/pubsub/internal/defaults.h" |
| 19 | +#include "google/cloud/pubsub/testing/fake_streaming_pull.h" |
| 20 | +#include "google/cloud/pubsub/testing/mock_subscriber_stub.h" |
| 21 | +#include "google/cloud/pubsub/testing/test_retry_policies.h" |
| 22 | +#include "google/cloud/credentials.h" |
| 23 | +#include "google/cloud/testing_util/status_matchers.h" |
| 24 | +#include <gmock/gmock.h> |
| 25 | +#include <atomic> |
| 26 | + |
| 27 | +namespace google { |
| 28 | +namespace cloud { |
| 29 | +namespace pubsub_internal { |
| 30 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 31 | +namespace { |
| 32 | + |
| 33 | +using ::google::cloud::pubsub::AckHandler; |
| 34 | +using ::google::cloud::pubsub::ExactlyOnceAckHandler; |
| 35 | +using ::google::cloud::pubsub::Message; |
| 36 | +using ::google::cloud::pubsub::Subscription; |
| 37 | +using ::google::cloud::pubsub_testing::FakeAsyncStreamingPull; |
| 38 | +using ::google::cloud::testing_util::StatusIs; |
| 39 | +using ::testing::AtLeast; |
| 40 | +using ::testing::Contains; |
| 41 | +using ::testing::HasSubstr; |
| 42 | +using ::testing::StartsWith; |
| 43 | + |
| 44 | +Options MakeTestOptions(Options opts = {}) { |
| 45 | + opts.set<UnifiedCredentialsOption>(MakeInsecureCredentials()); |
| 46 | + opts = pubsub_internal::DefaultSubscriberOptions( |
| 47 | + pubsub_testing::MakeTestOptions(std::move(opts))); |
| 48 | + // The CI scripts set an environment variable that overrides this option. We |
| 49 | + // are not interested in this behavior for this test. |
| 50 | + opts.unset<google::cloud::UserProjectOption>(); |
| 51 | + return opts; |
| 52 | +} |
| 53 | + |
| 54 | +Options MakeTestOptions(CompletionQueue const& cq) { |
| 55 | + return MakeTestOptions(Options{}.set<GrpcCompletionQueueOption>(cq)); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(SubscriberConnectionTest, Basic) { |
| 59 | + auto const subscription = Subscription("test-project", "test-subscription"); |
| 60 | + auto mock = std::make_shared<pubsub_testing::MockSubscriberStub>(); |
| 61 | + EXPECT_CALL(*mock, AsyncModifyAckDeadline) |
| 62 | + .WillRepeatedly([](google::cloud::CompletionQueue&, |
| 63 | + std::unique_ptr<grpc::ClientContext>, |
| 64 | + google::pubsub::v1::ModifyAckDeadlineRequest const&) { |
| 65 | + return make_ready_future(Status{}); |
| 66 | + }); |
| 67 | + EXPECT_CALL(*mock, AsyncAcknowledge) |
| 68 | + .WillOnce([](google::cloud::CompletionQueue&, |
| 69 | + std::unique_ptr<grpc::ClientContext>, |
| 70 | + google::pubsub::v1::AcknowledgeRequest const& request) { |
| 71 | + EXPECT_THAT(request.ack_ids(), Contains("test-ack-id-0")); |
| 72 | + return make_ready_future(Status{}); |
| 73 | + }); |
| 74 | + EXPECT_CALL(*mock, AsyncStreamingPull) |
| 75 | + .Times(AtLeast(1)) |
| 76 | + .WillRepeatedly(FakeAsyncStreamingPull); |
| 77 | + |
| 78 | + CompletionQueue cq; |
| 79 | + auto subscriber = std::make_shared<SubscriberConnectionImpl>( |
| 80 | + subscription, MakeTestOptions(cq), mock); |
| 81 | + std::atomic_flag received_one{false}; |
| 82 | + promise<void> waiter; |
| 83 | + auto handler = [&](Message const& m, AckHandler h) { |
| 84 | + if (received_one.test_and_set()) return; |
| 85 | + EXPECT_THAT(m.message_id(), StartsWith("test-message-id-")); |
| 86 | + ASSERT_NO_FATAL_FAILURE(std::move(h).ack()); |
| 87 | + waiter.set_value(); |
| 88 | + }; |
| 89 | + std::thread t([&cq] { cq.Run(); }); |
| 90 | + google::cloud::internal::OptionsSpan span(subscriber->options()); |
| 91 | + auto response = subscriber->Subscribe({handler}); |
| 92 | + waiter.get_future().wait(); |
| 93 | + response.cancel(); |
| 94 | + ASSERT_STATUS_OK(response.get()); |
| 95 | + // We need to explicitly cancel any pending timers (some of which may be quite |
| 96 | + // long) left by the subscription. |
| 97 | + cq.CancelAll(); |
| 98 | + cq.Shutdown(); |
| 99 | + t.join(); |
| 100 | +} |
| 101 | + |
| 102 | +TEST(SubscriberConnectionTest, ExactlyOnce) { |
| 103 | + auto const subscription = Subscription("test-project", "test-subscription"); |
| 104 | + auto mock = std::make_shared<pubsub_testing::MockSubscriberStub>(); |
| 105 | + EXPECT_CALL(*mock, AsyncModifyAckDeadline) |
| 106 | + .WillRepeatedly([](google::cloud::CompletionQueue&, |
| 107 | + std::unique_ptr<grpc::ClientContext>, |
| 108 | + google::pubsub::v1::ModifyAckDeadlineRequest const&) { |
| 109 | + return make_ready_future(Status{}); |
| 110 | + }); |
| 111 | + EXPECT_CALL(*mock, AsyncAcknowledge) |
| 112 | + .WillOnce([](google::cloud::CompletionQueue&, |
| 113 | + std::unique_ptr<grpc::ClientContext>, |
| 114 | + google::pubsub::v1::AcknowledgeRequest const& request) { |
| 115 | + EXPECT_THAT(request.ack_ids(), Contains("test-ack-id-0")); |
| 116 | + return make_ready_future( |
| 117 | + Status{StatusCode::kUnknown, "test-only-unknown"}); |
| 118 | + }); |
| 119 | + EXPECT_CALL(*mock, AsyncStreamingPull) |
| 120 | + .Times(AtLeast(1)) |
| 121 | + .WillRepeatedly(FakeAsyncStreamingPull); |
| 122 | + |
| 123 | + CompletionQueue cq; |
| 124 | + auto subscriber = std::make_shared<SubscriberConnectionImpl>( |
| 125 | + subscription, MakeTestOptions(cq), mock); |
| 126 | + std::atomic_flag received_one{false}; |
| 127 | + promise<void> waiter; |
| 128 | + auto callback = [&](Message const& m, ExactlyOnceAckHandler h) { |
| 129 | + if (received_one.test_and_set()) return; |
| 130 | + EXPECT_THAT(m.message_id(), StartsWith("test-message-id-")); |
| 131 | + auto status = std::move(h).ack().get(); |
| 132 | + EXPECT_THAT(status, StatusIs(StatusCode::kUnknown, "test-only-unknown")); |
| 133 | + waiter.set_value(); |
| 134 | + }; |
| 135 | + std::thread t([&cq] { cq.Run(); }); |
| 136 | + google::cloud::internal::OptionsSpan span(subscriber->options()); |
| 137 | + auto response = subscriber->ExactlyOnceSubscribe({callback}); |
| 138 | + waiter.get_future().wait(); |
| 139 | + response.cancel(); |
| 140 | + ASSERT_STATUS_OK(response.get()); |
| 141 | + // We need to explicitly cancel any pending timers (some of which may be quite |
| 142 | + // long) left by the subscription. |
| 143 | + cq.CancelAll(); |
| 144 | + cq.Shutdown(); |
| 145 | + t.join(); |
| 146 | +} |
| 147 | + |
| 148 | +TEST(SubscriberConnectionTest, PullFailure) { |
| 149 | + auto const subscription = Subscription("test-project", "test-subscription"); |
| 150 | + auto const expected = Status(StatusCode::kPermissionDenied, "uh-oh"); |
| 151 | + |
| 152 | + auto mock = std::make_shared<pubsub_testing::MockSubscriberStub>(); |
| 153 | + EXPECT_CALL(*mock, AsyncModifyAckDeadline) |
| 154 | + .WillRepeatedly([](google::cloud::CompletionQueue&, |
| 155 | + std::unique_ptr<grpc::ClientContext>, |
| 156 | + google::pubsub::v1::ModifyAckDeadlineRequest const&) { |
| 157 | + return make_ready_future(Status{}); |
| 158 | + }); |
| 159 | + EXPECT_CALL(*mock, AsyncAcknowledge) |
| 160 | + .WillRepeatedly([](google::cloud::CompletionQueue&, |
| 161 | + std::unique_ptr<grpc::ClientContext>, |
| 162 | + google::pubsub::v1::AcknowledgeRequest const&) { |
| 163 | + return make_ready_future(Status{}); |
| 164 | + }); |
| 165 | + EXPECT_CALL(*mock, AsyncStreamingPull) |
| 166 | + .Times(AtLeast(1)) |
| 167 | + .WillRepeatedly([](google::cloud::CompletionQueue const& cq, |
| 168 | + std::unique_ptr<grpc::ClientContext>) { |
| 169 | + using TimerFuture = |
| 170 | + future<StatusOr<std::chrono::system_clock::time_point>>; |
| 171 | + using us = std::chrono::microseconds; |
| 172 | + |
| 173 | + auto start_response = [q = cq]() mutable { |
| 174 | + return q.MakeRelativeTimer(us(10)).then( |
| 175 | + [](TimerFuture) { return false; }); |
| 176 | + }; |
| 177 | + auto finish_response = [q = cq]() mutable { |
| 178 | + return q.MakeRelativeTimer(us(10)).then([](TimerFuture) { |
| 179 | + return Status{StatusCode::kPermissionDenied, "uh-oh"}; |
| 180 | + }); |
| 181 | + }; |
| 182 | + |
| 183 | + auto stream = absl::make_unique<pubsub_testing::MockAsyncPullStream>(); |
| 184 | + EXPECT_CALL(*stream, Start).WillOnce(start_response); |
| 185 | + EXPECT_CALL(*stream, Finish).WillOnce(finish_response); |
| 186 | + return stream; |
| 187 | + }); |
| 188 | + |
| 189 | + auto subscriber = std::make_shared<SubscriberConnectionImpl>( |
| 190 | + subscription, pubsub_testing::MakeTestOptions(), mock); |
| 191 | + auto handler = [&](Message const&, AckHandler const&) {}; |
| 192 | + google::cloud::internal::OptionsSpan span(subscriber->options()); |
| 193 | + auto response = subscriber->Subscribe({handler}); |
| 194 | + EXPECT_THAT(response.get(), |
| 195 | + StatusIs(StatusCode::kPermissionDenied, HasSubstr("uh-oh"))); |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace |
| 199 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 200 | +} // namespace pubsub_internal |
| 201 | +} // namespace cloud |
| 202 | +} // namespace google |
0 commit comments