From 15db816e6ff0ccff6472379639a567f2665ebb9f Mon Sep 17 00:00:00 2001 From: dbolduc Date: Mon, 22 May 2023 15:15:19 -0400 Subject: [PATCH] feat(storage): offer mock client without decorators --- .../examples/storage_client_mock_samples.cc | 8 ++--- .../storage/google_cloud_cpp_storage.cmake | 1 + .../cloud/storage/storage_client_testing.bzl | 1 + google/cloud/storage/testing/mock_client.cc | 29 +++++++++++++++++++ google/cloud/storage/testing/mock_client.h | 14 ++++++++- .../testing/remove_stale_buckets_test.cc | 4 +-- 6 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 google/cloud/storage/testing/mock_client.cc diff --git a/google/cloud/storage/examples/storage_client_mock_samples.cc b/google/cloud/storage/examples/storage_client_mock_samples.cc index c34002cad1418..cee48946a6dde 100644 --- a/google/cloud/storage/examples/storage_client_mock_samples.cc +++ b/google/cloud/storage/examples/storage_client_mock_samples.cc @@ -29,7 +29,7 @@ TEST(StorageMockingSamples, MockReadObject) { std::shared_ptr mock = std::make_shared(); - auto client = gcs::testing::ClientFromMock(mock); + auto client = gcs::testing::UndecoratedClientFromMock(mock); std::string const text = "this is a mock http response"; std::size_t offset = 0; @@ -72,7 +72,7 @@ TEST(StorageMockingSamples, MockWriteObject) { std::shared_ptr mock = std::make_shared(); - auto client = gcs::testing::ClientFromMock(mock); + auto client = gcs::testing::UndecoratedClientFromMock(mock); gcs::ObjectMetadata expected_metadata; @@ -97,7 +97,7 @@ TEST(StorageMockingSamples, MockReadObjectFailure) { std::shared_ptr mock = std::make_shared(); - auto client = gcs::testing::ClientFromMock(mock); + auto client = gcs::testing::UndecoratedClientFromMock(mock); std::string text = "this is a mock http response"; EXPECT_CALL(*mock, ReadObject) @@ -130,7 +130,7 @@ TEST(StorageMockingSamples, MockWriteObjectFailure) { std::shared_ptr mock = std::make_shared(); - auto client = gcs::testing::ClientFromMock(mock); + auto client = gcs::testing::UndecoratedClientFromMock(mock); using gcs::internal::CreateResumableUploadResponse; using gcs::internal::QueryResumableUploadResponse; diff --git a/google/cloud/storage/google_cloud_cpp_storage.cmake b/google/cloud/storage/google_cloud_cpp_storage.cmake index d475554d45736..5e53e972a990b 100644 --- a/google/cloud/storage/google_cloud_cpp_storage.cmake +++ b/google/cloud/storage/google_cloud_cpp_storage.cmake @@ -406,6 +406,7 @@ if (BUILD_TESTING) testing/client_unit_test.cc testing/client_unit_test.h testing/constants.h + testing/mock_client.cc testing/mock_client.h testing/mock_http_request.cc testing/mock_http_request.h diff --git a/google/cloud/storage/storage_client_testing.bzl b/google/cloud/storage/storage_client_testing.bzl index e060351a27faf..5727e9f9a90f2 100644 --- a/google/cloud/storage/storage_client_testing.bzl +++ b/google/cloud/storage/storage_client_testing.bzl @@ -35,6 +35,7 @@ storage_client_testing_hdrs = [ storage_client_testing_srcs = [ "testing/client_unit_test.cc", + "testing/mock_client.cc", "testing/mock_http_request.cc", "testing/object_integration_test.cc", "testing/random_names.cc", diff --git a/google/cloud/storage/testing/mock_client.cc b/google/cloud/storage/testing/mock_client.cc new file mode 100644 index 0000000000000..88cd26aba3f33 --- /dev/null +++ b/google/cloud/storage/testing/mock_client.cc @@ -0,0 +1,29 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "google/cloud/storage/testing/mock_client.h" + +namespace google { +namespace cloud { +namespace storage { +namespace testing { + +Client UndecoratedClientFromMock(std::shared_ptr mock) { + return internal::ClientImplDetails::CreateWithoutDecorations(std::move(mock)); +} + +} // namespace testing +} // namespace storage +} // namespace cloud +} // namespace google diff --git a/google/cloud/storage/testing/mock_client.h b/google/cloud/storage/testing/mock_client.h index 9d71ac94b9852..be48831c02785 100644 --- a/google/cloud/storage/testing/mock_client.h +++ b/google/cloud/storage/testing/mock_client.h @@ -185,7 +185,12 @@ class MockStreambuf : public internal::ObjectWriteStreambuf { MOCK_METHOD(std::uint64_t, next_expected_byte, (), (const, override)); }; -/// Create a client configured to use the given mock. +/** + * Create a client configured to use the given mock. + * + * Unless you specifically need to mock the behavior of retries, prefer + * #google::cloud::storage::testing::UndecoratedClientFromMock + */ template Client ClientFromMock(std::shared_ptr const& mock, Policies&&... p) { @@ -193,6 +198,13 @@ Client ClientFromMock(std::shared_ptr const& mock, mock, std::forward(p)...); } +/** + * Create a client configured to use the given mock. + * + * This client does not retry on transient errors. + */ +Client UndecoratedClientFromMock(std::shared_ptr mock); + } // namespace testing } // namespace storage } // namespace cloud diff --git a/google/cloud/storage/testing/remove_stale_buckets_test.cc b/google/cloud/storage/testing/remove_stale_buckets_test.cc index 23c1962fcc720..afc5e85c03416 100644 --- a/google/cloud/storage/testing/remove_stale_buckets_test.cc +++ b/google/cloud/storage/testing/remove_stale_buckets_test.cc @@ -72,7 +72,7 @@ TEST(CleanupStaleBucketsTest, RemoveBucketContents) { EXPECT_CALL(*mock, GetBucketMetadata) .WillOnce( Return(make_status_or(BucketMetadata{}.set_name("fake-bucket")))); - auto client = internal::ClientImplDetails::CreateWithoutDecorations(mock); + auto client = UndecoratedClientFromMock(mock); auto const actual = RemoveBucketAndContents(client, "fake-bucket"); EXPECT_STATUS_OK(actual); } @@ -114,7 +114,7 @@ TEST(CleanupStaleBucketsTest, RemoveStaleBuckets) { return response; }); - auto client = internal::ClientImplDetails::CreateWithoutDecorations(mock); + auto client = UndecoratedClientFromMock(mock); auto const actual = RemoveStaleBuckets(client, "matching", create_time_limit); EXPECT_STATUS_OK(actual); }