Skip to content

Commit

Permalink
cleanup(storage): simplify test initialization (googleapis#14498)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan authored and cuiy0006 committed Jul 22, 2024
1 parent b213bc7 commit e30a274
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 312 deletions.
14 changes: 5 additions & 9 deletions google/cloud/storage/tests/async_client_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ auto AlwaysRetry() {
}

TEST_F(AsyncClientIntegrationTest, ObjectCRUD) {
auto client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);

auto client = MakeIntegrationTestClient(Options{});
auto object_name = MakeRandomObjectName();

auto async = AsyncClient();

auto insert = async
.InsertObject(BucketName(bucket_name()), object_name,
LoremIpsum(), AlwaysRetry())
Expand Down Expand Up @@ -105,19 +103,17 @@ TEST_F(AsyncClientIntegrationTest, ObjectCRUD) {
.get();
EXPECT_STATUS_OK(status);

auto get = client->GetObjectMetadata(bucket_name(), object_name);
auto get = client.GetObjectMetadata(bucket_name(), object_name);
EXPECT_THAT(get, StatusIs(StatusCode::kNotFound));
}

TEST_F(AsyncClientIntegrationTest, ComposeObject) {
auto client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);

auto client = MakeIntegrationTestClient(Options{});
auto o1 = MakeRandomObjectName();
auto o2 = MakeRandomObjectName();
auto destination = MakeRandomObjectName();

auto async = AsyncClient();

auto insert1 = async.InsertObject(BucketName(bucket_name()), o1, LoremIpsum(),
AlwaysRetry());
auto insert2 = async.InsertObject(BucketName(bucket_name()), o2, LoremIpsum(),
Expand Down
39 changes: 18 additions & 21 deletions google/cloud/storage/tests/auto_finalize_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,59 +46,56 @@ class AutoFinalizeIntegrationTest
};

TEST_F(AutoFinalizeIntegrationTest, DefaultIsEnabled) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto client = MakeIntegrationTestClient(Options{});

auto const object_name = MakeRandomObjectName();
auto const expected_text = LoremIpsum();
{
auto stream =
client->WriteObject(bucket_name(), object_name, IfGenerationMatch(0));
client.WriteObject(bucket_name(), object_name, IfGenerationMatch(0));
stream << expected_text;
}
auto reader = client->ReadObject(bucket_name(), object_name);
auto reader = client.ReadObject(bucket_name(), object_name);
ASSERT_TRUE(reader.good());
ASSERT_STATUS_OK(reader.status());
auto const actual =
std::string{std::istreambuf_iterator<char>{reader.rdbuf()}, {}};
EXPECT_EQ(expected_text, actual);

(void)client->DeleteObject(bucket_name(), object_name);
(void)client.DeleteObject(bucket_name(), object_name);
}

TEST_F(AutoFinalizeIntegrationTest, ExplicitlyEnabled) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto client = MakeIntegrationTestClient(Options{});

auto const object_name = MakeRandomObjectName();
auto const expected_text = LoremIpsum();
{
auto stream =
client->WriteObject(bucket_name(), object_name, IfGenerationMatch(0),
AutoFinalizeEnabled());
client.WriteObject(bucket_name(), object_name, IfGenerationMatch(0),
AutoFinalizeEnabled());
stream << expected_text;
}
auto reader = client->ReadObject(bucket_name(), object_name);
auto reader = client.ReadObject(bucket_name(), object_name);
EXPECT_TRUE(reader.good());
EXPECT_STATUS_OK(reader.status());
auto const actual =
std::string{std::istreambuf_iterator<char>{reader.rdbuf()}, {}};
EXPECT_EQ(expected_text, actual);

(void)client->DeleteObject(bucket_name(), object_name);
(void)client.DeleteObject(bucket_name(), object_name);
}

TEST_F(AutoFinalizeIntegrationTest, Disabled) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto client = MakeIntegrationTestClient(Options{});

auto const object_name = MakeRandomObjectName();
auto constexpr kQuantum = internal::UploadChunkRequest::kChunkSizeQuantum;
auto constexpr kSize = 8 * kQuantum;
auto const expected_text = MakeRandomData(kSize);
auto const upload_session = [&] {
auto os = client->WriteObject(bucket_name(), object_name,
IfGenerationMatch(0), AutoFinalizeDisabled());
auto os = client.WriteObject(bucket_name(), object_name,
IfGenerationMatch(0), AutoFinalizeDisabled());
auto id = os.resumable_session_id();
os.write(expected_text.data(), kQuantum);
os << std::flush;
Expand All @@ -107,14 +104,14 @@ TEST_F(AutoFinalizeIntegrationTest, Disabled) {

{
// The upload is not finalized, so the object should not exist:
auto reader = client->ReadObject(bucket_name(), object_name);
auto reader = client.ReadObject(bucket_name(), object_name);
ASSERT_THAT(reader.status(), StatusIs(StatusCode::kNotFound));
}

for (std::uint64_t from = 0; kQuantum <= (kSize - from);) {
auto os =
client->WriteObject(bucket_name(), object_name, AutoFinalizeDisabled(),
UseResumableUploadSession(upload_session));
client.WriteObject(bucket_name(), object_name, AutoFinalizeDisabled(),
UseResumableUploadSession(upload_session));
from = os.next_expected_byte();
if (from >= kSize) break;
os.write(expected_text.data() + from, kQuantum);
Expand All @@ -123,14 +120,14 @@ TEST_F(AutoFinalizeIntegrationTest, Disabled) {
EXPECT_THAT(os.last_status(), IsOk());
}
auto os =
client->WriteObject(bucket_name(), object_name, AutoFinalizeDisabled(),
UseResumableUploadSession(upload_session));
client.WriteObject(bucket_name(), object_name, AutoFinalizeDisabled(),
UseResumableUploadSession(upload_session));
os.Close();
ASSERT_THAT(os.metadata(), IsOk());
ScheduleForDelete(*os.metadata());
EXPECT_EQ(os.metadata()->size(), kSize);

auto reader = client->ReadObject(bucket_name(), object_name);
auto reader = client.ReadObject(bucket_name(), object_name);
EXPECT_TRUE(reader.good());
EXPECT_STATUS_OK(reader.status());
auto const actual =
Expand Down
Loading

0 comments on commit e30a274

Please sign in to comment.