|
| 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/ack_handler_wrapper.h" |
| 16 | +#include "google/cloud/internal/make_status.h" |
| 17 | +#include "google/cloud/testing_util/scoped_log.h" |
| 18 | +#include "absl/memory/memory.h" |
| 19 | +#include <gmock/gmock.h> |
| 20 | + |
| 21 | +namespace google { |
| 22 | +namespace cloud { |
| 23 | +namespace pubsub_internal { |
| 24 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 25 | + |
| 26 | +using ::google::cloud::internal::PermissionDeniedError; |
| 27 | +using ::google::cloud::testing_util::ScopedLog; |
| 28 | +using ::testing::AllOf; |
| 29 | +using ::testing::ByMove; |
| 30 | +using ::testing::Contains; |
| 31 | +using ::testing::HasSubstr; |
| 32 | +using ::testing::Return; |
| 33 | + |
| 34 | +class MockExactlyOnceAckHandlerImpl |
| 35 | + : public pubsub::ExactlyOnceAckHandler::Impl { |
| 36 | + public: |
| 37 | + MOCK_METHOD(future<Status>, ack, (), (override)); |
| 38 | + MOCK_METHOD(future<Status>, nack, (), (override)); |
| 39 | + MOCK_METHOD(std::int32_t, delivery_attempt, (), (const, override)); |
| 40 | +}; |
| 41 | + |
| 42 | +TEST(AckHandlerWrapper, Ack) { |
| 43 | + auto mock = absl::make_unique<MockExactlyOnceAckHandlerImpl>(); |
| 44 | + EXPECT_CALL(*mock, ack) |
| 45 | + .WillOnce( |
| 46 | + Return(ByMove(make_ready_future(PermissionDeniedError("uh-oh"))))); |
| 47 | + ScopedLog log; |
| 48 | + AckHandlerWrapper tested(std::move(mock), "test-id"); |
| 49 | + tested.ack(); |
| 50 | + EXPECT_THAT(log.ExtractLines(), |
| 51 | + Contains(AllOf(HasSubstr(" ack()"), HasSubstr("uh-oh"), |
| 52 | + HasSubstr("test-id")))); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(AckHandlerWrapper, AckEmpty) { |
| 56 | + auto mock = absl::make_unique<MockExactlyOnceAckHandlerImpl>(); |
| 57 | + EXPECT_CALL(*mock, ack) |
| 58 | + .WillOnce( |
| 59 | + Return(ByMove(make_ready_future(PermissionDeniedError("uh-oh"))))); |
| 60 | + ScopedLog log; |
| 61 | + AckHandlerWrapper tested(std::move(mock)); |
| 62 | + tested.ack(); |
| 63 | + EXPECT_THAT(log.ExtractLines(), Not(Contains(HasSubstr(" ack()")))); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(AckHandlerWrapper, Nack) { |
| 67 | + auto mock = absl::make_unique<MockExactlyOnceAckHandlerImpl>(); |
| 68 | + EXPECT_CALL(*mock, nack) |
| 69 | + .WillOnce( |
| 70 | + Return(ByMove(make_ready_future(PermissionDeniedError("uh-oh"))))); |
| 71 | + ScopedLog log; |
| 72 | + AckHandlerWrapper tested(std::move(mock), "test-id"); |
| 73 | + tested.nack(); |
| 74 | + EXPECT_THAT(log.ExtractLines(), |
| 75 | + Contains(AllOf(HasSubstr(" nack()"), HasSubstr("uh-oh"), |
| 76 | + HasSubstr("test-id")))); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(AckHandlerWrapper, NackEmpty) { |
| 80 | + auto mock = absl::make_unique<MockExactlyOnceAckHandlerImpl>(); |
| 81 | + EXPECT_CALL(*mock, nack) |
| 82 | + .WillOnce( |
| 83 | + Return(ByMove(make_ready_future(PermissionDeniedError("uh-oh"))))); |
| 84 | + ScopedLog log; |
| 85 | + AckHandlerWrapper tested(std::move(mock)); |
| 86 | + tested.nack(); |
| 87 | + EXPECT_THAT(log.ExtractLines(), Not(Contains(HasSubstr(" nack()")))); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(AckHandlerWrapper, DeliveryAttempt) { |
| 91 | + auto mock = absl::make_unique<MockExactlyOnceAckHandlerImpl>(); |
| 92 | + EXPECT_CALL(*mock, delivery_attempt).WillOnce(Return(42)); |
| 93 | + AckHandlerWrapper tested(std::move(mock), "test-id"); |
| 94 | + EXPECT_EQ(tested.delivery_attempt(), 42); |
| 95 | +} |
| 96 | + |
| 97 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 98 | +} // namespace pubsub_internal |
| 99 | +} // namespace cloud |
| 100 | +} // namespace google |
0 commit comments