-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update FakeStorageRpc to better handle resumable sessions that o…
…verwrite existing objects (#1296)
- Loading branch information
1 parent
a73cff8
commit 8051cae
Showing
2 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
package com.google.cloud.storage.contrib.nio.testing; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.WriteChannel; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.Storage.BlobWriteOption; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper.FakeStorageRpcFactory; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
|
||
public final class FakeStorageRpcTest { | ||
|
||
@Test | ||
public void overwritingAnObjectOverwritesItsContent() throws IOException { | ||
Storage storage = | ||
StorageOptions.http() | ||
.setServiceRpcFactory(new FakeStorageRpcFactory()) | ||
.build() | ||
.getService(); | ||
|
||
try (WriteChannel writer = | ||
storage.writer( | ||
BlobInfo.newBuilder(BlobId.of("bucket", "obj", 0L)).build(), | ||
BlobWriteOption.generationMatch())) { | ||
writer.write(ByteBuffer.wrap("abc".getBytes(StandardCharsets.UTF_8))); | ||
} | ||
Blob gen1 = storage.get(BlobId.of("bucket", "obj")); | ||
// get existing generation | ||
String gen1read1 = new String(gen1.getContent(), StandardCharsets.UTF_8); | ||
assertThat(gen1read1).isEqualTo("abc"); | ||
System.out.println("gen1read1 = " + gen1read1); | ||
|
||
// start an upload that will overwrite the existing generation | ||
WriteChannel writer = storage.writer(gen1, BlobWriteOption.generationMatch()); | ||
writer.write(ByteBuffer.wrap("def".getBytes(StandardCharsets.UTF_8))); | ||
// make sure we can still read the existing generations value after starting but before closing | ||
String gen1read2 = new String(gen1.getContent(), StandardCharsets.UTF_8); | ||
assertThat(gen1read2).isEqualTo("abc"); | ||
writer.close(); | ||
|
||
Blob gen2 = storage.get(BlobId.of("bucket", "obj")); | ||
String gen2read1 = new String(gen2.getContent(), StandardCharsets.UTF_8); | ||
System.out.println("gen2read1 = " + gen2read1); | ||
assertThat(gen2read1).isEqualTo("def"); | ||
} | ||
} |