|
| 1 | +// Copyright 2025 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/bigtable/internal/logging_result_set_reader.h" |
| 16 | +#include "google/cloud/bigtable/testing/mock_partial_result_set_reader.h" |
| 17 | +#include "google/cloud/log.h" |
| 18 | +#include "google/cloud/testing_util/scoped_log.h" |
| 19 | +#include "google/cloud/testing_util/status_matchers.h" |
| 20 | +#include "google/cloud/tracing_options.h" |
| 21 | +#include <gmock/gmock.h> |
| 22 | + |
| 23 | +namespace google { |
| 24 | +namespace cloud { |
| 25 | +namespace bigtable_internal { |
| 26 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 27 | +namespace { |
| 28 | + |
| 29 | +using ::testing::_; |
| 30 | +using ::testing::AllOf; |
| 31 | +using ::testing::Contains; |
| 32 | +using ::testing::HasSubstr; |
| 33 | +using ::testing::IsEmpty; |
| 34 | +using ::testing::StartsWith; |
| 35 | + |
| 36 | +class LoggingResultSetReaderTest : public ::testing::Test { |
| 37 | + protected: |
| 38 | + testing_util::ScopedLog log_; |
| 39 | +}; |
| 40 | + |
| 41 | +TEST_F(LoggingResultSetReaderTest, TryCancel) { |
| 42 | + auto mock = std::make_unique<bigtable_testing::MockPartialResultSetReader>(); |
| 43 | + EXPECT_CALL(*mock, TryCancel()).Times(1); |
| 44 | + LoggingResultSetReader reader(std::move(mock), TracingOptions{}); |
| 45 | + reader.TryCancel(); |
| 46 | + |
| 47 | + EXPECT_THAT(log_.ExtractLines(), IsEmpty()); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_F(LoggingResultSetReaderTest, Read) { |
| 51 | + auto mock = std::make_unique<bigtable_testing::MockPartialResultSetReader>(); |
| 52 | + EXPECT_CALL(*mock, Read(_, _)) |
| 53 | + .WillOnce([](absl::optional<std::string> const&, |
| 54 | + UnownedPartialResultSet& result) { |
| 55 | + result.resumption = false; |
| 56 | + result.result.set_resume_token("test-token"); |
| 57 | + return true; |
| 58 | + }) |
| 59 | + .WillOnce([] { return false; }); |
| 60 | + LoggingResultSetReader reader(std::move(mock), TracingOptions{}); |
| 61 | + google::bigtable::v2::PartialResultSet partial_result_set; |
| 62 | + auto result = |
| 63 | + UnownedPartialResultSet::FromPartialResultSet(partial_result_set); |
| 64 | + ASSERT_TRUE(reader.Read("", result)); |
| 65 | + EXPECT_EQ("test-token", result.result.resume_token()); |
| 66 | + |
| 67 | + auto log_lines = log_.ExtractLines(); |
| 68 | + EXPECT_THAT(log_lines, AllOf(Contains(StartsWith("Read()")))); |
| 69 | + EXPECT_THAT(log_lines, Contains(HasSubstr("resume_token=\"\""))); |
| 70 | + EXPECT_THAT(log_lines, Contains(HasSubstr("resumption=false"))); |
| 71 | + |
| 72 | + ASSERT_FALSE(reader.Read("test-token", result)); |
| 73 | + |
| 74 | + log_lines = log_.ExtractLines(); |
| 75 | + EXPECT_THAT(log_lines, AllOf(Contains(StartsWith("Read()")))); |
| 76 | + EXPECT_THAT(log_lines, Contains(HasSubstr("resume_token=\"test-token\""))); |
| 77 | + EXPECT_THAT(log_lines, Contains(HasSubstr("(failed)"))); |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(LoggingResultSetReaderTest, Finish) { |
| 81 | + Status const expected_status = Status(StatusCode::kOutOfRange, "weird"); |
| 82 | + auto mock = std::make_unique<bigtable_testing::MockPartialResultSetReader>(); |
| 83 | + EXPECT_CALL(*mock, Finish()).WillOnce([expected_status] { |
| 84 | + return expected_status; // NOLINT(performance-no-automatic-move) |
| 85 | + }); |
| 86 | + LoggingResultSetReader reader(std::move(mock), TracingOptions{}); |
| 87 | + auto status = reader.Finish(); |
| 88 | + EXPECT_EQ(expected_status, status); |
| 89 | + |
| 90 | + auto log_lines = log_.ExtractLines(); |
| 91 | + EXPECT_THAT(log_lines, IsEmpty()); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
| 95 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 96 | +} // namespace bigtable_internal |
| 97 | +} // namespace cloud |
| 98 | +} // namespace google |
0 commit comments