|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import static com.google.cloud.storage.ByteSizeConstants._2MiB; |
| 20 | +import static com.google.cloud.storage.ITObjectReadSessionFakeTest.getReadRange; |
| 21 | +import static com.google.cloud.storage.PackagePrivateMethodWorkarounds.maybeGetStorageDataClient; |
| 22 | +import static com.google.common.truth.Truth.assertThat; |
| 23 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 24 | + |
| 25 | +import com.google.api.gax.grpc.GrpcCallContext; |
| 26 | +import com.google.cloud.storage.ITObjectReadSessionFakeTest.FakeStorage; |
| 27 | +import com.google.cloud.storage.it.ChecksummedTestContent; |
| 28 | +import com.google.common.collect.ImmutableList; |
| 29 | +import com.google.common.collect.ImmutableMap; |
| 30 | +import com.google.storage.v2.BidiReadObjectRequest; |
| 31 | +import com.google.storage.v2.BidiReadObjectResponse; |
| 32 | +import com.google.storage.v2.BidiReadObjectSpec; |
| 33 | +import com.google.storage.v2.BucketName; |
| 34 | +import com.google.storage.v2.Object; |
| 35 | +import com.google.storage.v2.ObjectRangeData; |
| 36 | +import io.grpc.stub.StreamObserver; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.function.Consumer; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +public final class ITFastOpenFakeTest { |
| 45 | + |
| 46 | + private static final byte[] ALL_OBJECT_BYTES = DataGenerator.base64Characters().genBytes(64); |
| 47 | + private static final Object METADATA = |
| 48 | + Object.newBuilder() |
| 49 | + .setBucket(BucketName.format("_", "b")) |
| 50 | + .setName("o") |
| 51 | + .setGeneration(1) |
| 52 | + .setSize(_2MiB) |
| 53 | + .build(); |
| 54 | + private static final GrpcCallContext CTX = GrpcCallContext.createDefault(); |
| 55 | + |
| 56 | + private static final BidiReadObjectRequest OPEN = |
| 57 | + BidiReadObjectRequest.newBuilder() |
| 58 | + .setReadObjectSpec( |
| 59 | + BidiReadObjectSpec.newBuilder() |
| 60 | + .setBucket(METADATA.getBucket()) |
| 61 | + .setObject(METADATA.getName()) |
| 62 | + .build()) |
| 63 | + .build(); |
| 64 | + private static final BidiReadObjectRequest OPEN_WITH_READ_OF_10_TO_20 = |
| 65 | + OPEN.toBuilder().addReadRanges(getReadRange(1, 10, 10)).build(); |
| 66 | + |
| 67 | + private static final ReadAsFutureBytes READ_10_TO_20 = |
| 68 | + ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(10, 10)); |
| 69 | + |
| 70 | + /** Every request the fake server received, in order. */ |
| 71 | + private final List<BidiReadObjectRequest> requestsReceived = |
| 72 | + Collections.synchronizedList(new ArrayList<>()); |
| 73 | + |
| 74 | + @Test |
| 75 | + public void openRequest_includesTheFastOpenRead() throws Exception { |
| 76 | + FakeStorage server = |
| 77 | + fakeServer(ImmutableMap.of(OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10))); |
| 78 | + |
| 79 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 80 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 81 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 82 | + |
| 83 | + ObjectReadSession session = |
| 84 | + dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS); |
| 85 | + session.close(); |
| 86 | + } |
| 87 | + |
| 88 | + assertThat(requestsReceived).containsExactly(OPEN_WITH_READ_OF_10_TO_20); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void openRequest_withoutFastOpenReads_isUnchanged() throws Exception { |
| 93 | + FakeStorage server = fakeServer(ImmutableMap.of(OPEN, metadataOnly())); |
| 94 | + |
| 95 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 96 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 97 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 98 | + |
| 99 | + ObjectReadSession session = |
| 100 | + dataClient.readSession(OPEN, CTX, ImmutableList.of()).get(5, SECONDS); |
| 101 | + session.close(); |
| 102 | + } |
| 103 | + |
| 104 | + assertThat(requestsReceived).containsExactly(OPEN); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A fake server that answers each request in {@code responses} and adds it to {@link |
| 109 | + * #requestsReceived}. Any other request is answered with an error. |
| 110 | + */ |
| 111 | + private FakeStorage fakeServer(Map<BidiReadObjectRequest, BidiReadObjectResponse> responses) { |
| 112 | + ImmutableMap.Builder<BidiReadObjectRequest, Consumer<StreamObserver<BidiReadObjectResponse>>> |
| 113 | + behaviour = ImmutableMap.builder(); |
| 114 | + responses.forEach( |
| 115 | + (request, response) -> |
| 116 | + behaviour.put( |
| 117 | + request, |
| 118 | + client -> { |
| 119 | + requestsReceived.add(request); |
| 120 | + client.onNext(response); |
| 121 | + })); |
| 122 | + return FakeStorage.of(behaviour.buildOrThrow()); |
| 123 | + } |
| 124 | + |
| 125 | + private static BidiReadObjectResponse metadataOnly() { |
| 126 | + return BidiReadObjectResponse.newBuilder().setMetadata(METADATA).build(); |
| 127 | + } |
| 128 | + |
| 129 | + /** The response to an open request that has a read of {@code [offset, offset + length)}. */ |
| 130 | + private static BidiReadObjectResponse metadataAndBytes(int readId, int offset, int length) { |
| 131 | + return BidiReadObjectResponse.newBuilder() |
| 132 | + .setMetadata(METADATA) |
| 133 | + .addObjectDataRanges( |
| 134 | + ObjectRangeData.newBuilder() |
| 135 | + .setChecksummedData( |
| 136 | + ChecksummedTestContent.of(ALL_OBJECT_BYTES, offset, length).asChecksummedData()) |
| 137 | + .setReadRange(getReadRange(readId, offset, length)) |
| 138 | + .setRangeEnd(true)) |
| 139 | + .build(); |
| 140 | + } |
| 141 | +} |
0 commit comments