|
| 1 | +/* |
| 2 | + * Copyright 2025 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.TestUtils.xxd; |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | + |
| 22 | +import com.google.api.core.SettableApiFuture; |
| 23 | +import com.google.api.gax.rpc.ApiCallContext; |
| 24 | +import com.google.api.gax.rpc.ResponseObserver; |
| 25 | +import com.google.api.gax.rpc.ServerStreamingCallable; |
| 26 | +import com.google.api.gax.rpc.StreamController; |
| 27 | +import com.google.cloud.storage.GrpcUtils.ZeroCopyServerStreamingCallable; |
| 28 | +import com.google.cloud.storage.Retrying.Retrier; |
| 29 | +import com.google.cloud.storage.it.ChecksummedTestContent; |
| 30 | +import com.google.storage.v2.ReadObjectRequest; |
| 31 | +import com.google.storage.v2.ReadObjectResponse; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.ByteBuffer; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public final class GapicUnbufferedReadableByteChannelTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void ensureResponseAreClosed() throws IOException { |
| 41 | + ChecksummedTestContent testContent = |
| 42 | + ChecksummedTestContent.of(DataGenerator.base64Characters().genBytes(10)); |
| 43 | + |
| 44 | + AtomicBoolean close = new AtomicBoolean(false); |
| 45 | + |
| 46 | + ResponseContentLifecycleManager<ReadObjectResponse> manager = |
| 47 | + resp -> ResponseContentLifecycleHandle.create(resp, () -> close.compareAndSet(false, true)); |
| 48 | + |
| 49 | + try (GapicUnbufferedReadableByteChannel c = |
| 50 | + new GapicUnbufferedReadableByteChannel( |
| 51 | + SettableApiFuture.create(), |
| 52 | + new ZeroCopyServerStreamingCallable<>( |
| 53 | + new ServerStreamingCallable<ReadObjectRequest, ReadObjectResponse>() { |
| 54 | + @Override |
| 55 | + public void call( |
| 56 | + ReadObjectRequest request, |
| 57 | + ResponseObserver<ReadObjectResponse> respond, |
| 58 | + ApiCallContext context) { |
| 59 | + respond.onStart(new NullStreamController()); |
| 60 | + respond.onResponse( |
| 61 | + ReadObjectResponse.newBuilder() |
| 62 | + .setChecksummedData(testContent.asChecksummedData()) |
| 63 | + .build()); |
| 64 | + respond.onComplete(); |
| 65 | + } |
| 66 | + }, |
| 67 | + manager), |
| 68 | + ReadObjectRequest.getDefaultInstance(), |
| 69 | + Hasher.noop(), |
| 70 | + Retrier.attemptOnce(), |
| 71 | + Retrying.neverRetry())) { |
| 72 | + |
| 73 | + ByteBuffer buffer = ByteBuffer.allocate(15); |
| 74 | + c.read(buffer); |
| 75 | + assertThat(xxd(buffer)).isEqualTo(xxd(testContent.getBytes())); |
| 76 | + assertThat(close.get()).isTrue(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static class NullStreamController implements StreamController { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void cancel() {} |
| 84 | + |
| 85 | + @Override |
| 86 | + public void disableAutoInboundFlowControl() {} |
| 87 | + |
| 88 | + @Override |
| 89 | + public void request(int count) {} |
| 90 | + } |
| 91 | +} |
0 commit comments