|
| 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.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | + |
| 22 | +import com.google.cloud.ReadChannel; |
| 23 | +import com.google.cloud.storage.BlobId; |
| 24 | +import com.google.cloud.storage.BlobReadSession; |
| 25 | +import com.google.cloud.storage.BucketInfo; |
| 26 | +import com.google.cloud.storage.ReadProjectionConfig; |
| 27 | +import com.google.cloud.storage.ReadProjectionConfigs; |
| 28 | +import com.google.cloud.storage.Storage; |
| 29 | +import com.google.cloud.storage.TransportCompatibility.Transport; |
| 30 | +import com.google.cloud.storage.it.runner.StorageITRunner; |
| 31 | +import com.google.cloud.storage.it.runner.annotations.Backend; |
| 32 | +import com.google.cloud.storage.it.runner.annotations.CrossRun; |
| 33 | +import com.google.cloud.storage.it.runner.annotations.Inject; |
| 34 | +import com.google.cloud.storage.it.runner.registry.Generator; |
| 35 | +import com.google.cloud.storage.it.runner.registry.ObjectsFixture; |
| 36 | +import com.google.common.io.ByteStreams; |
| 37 | +import java.io.IOException; |
| 38 | +import java.nio.ByteBuffer; |
| 39 | +import java.nio.channels.Channels; |
| 40 | +import java.nio.channels.ReadableByteChannel; |
| 41 | +import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.ThreadLocalRandom; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | +import java.util.concurrent.TimeoutException; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.runner.RunWith; |
| 47 | + |
| 48 | +@RunWith(StorageITRunner.class) |
| 49 | +@CrossRun( |
| 50 | + backends = {Backend.PROD}, |
| 51 | + transports = {Transport.HTTP, Transport.GRPC}) |
| 52 | +public final class ITReadableByteChannelBehaviorTest { |
| 53 | + |
| 54 | + @Inject public Storage storage; |
| 55 | + @Inject public BucketInfo bucket; |
| 56 | + @Inject public Generator generator; |
| 57 | + @Inject public ObjectsFixture objectsFixture; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void eofReturnedMultipleTimes_reader() throws IOException { |
| 61 | + BlobId id = objectsFixture.getObj512KiB().getInfo().getBlobId(); |
| 62 | + |
| 63 | + try (ReadChannel reader = storage.reader(id)) { |
| 64 | + eofReturnedMultipleTimes_doTest(reader); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @CrossRun.Exclude(transports = Transport.HTTP) |
| 70 | + public void eofReturnedMultipleTimes_blobReadSession_channel() |
| 71 | + throws ExecutionException, InterruptedException, TimeoutException, IOException { |
| 72 | + eofReturnedMultipleTimes_doTestBlobReadSession(ReadProjectionConfigs.asChannel()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @CrossRun.Exclude(transports = Transport.HTTP) |
| 77 | + public void eofReturnedMultipleTimes_blobReadSession_seekableChannel() |
| 78 | + throws ExecutionException, InterruptedException, TimeoutException, IOException { |
| 79 | + eofReturnedMultipleTimes_doTestBlobReadSession(ReadProjectionConfigs.asSeekableChannel()); |
| 80 | + } |
| 81 | + |
| 82 | + private void eofReturnedMultipleTimes_doTestBlobReadSession( |
| 83 | + ReadProjectionConfig<? extends ReadableByteChannel> config) |
| 84 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 85 | + BlobId id = objectsFixture.getObj512KiB().getInfo().getBlobId(); |
| 86 | + |
| 87 | + try (BlobReadSession session = storage.blobReadSession(id).get(3, TimeUnit.SECONDS)) { |
| 88 | + try (ReadableByteChannel c = session.readAs(config)) { |
| 89 | + eofReturnedMultipleTimes_doTest(c); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void eofReturnedMultipleTimes_doTest(ReadableByteChannel c) throws IOException { |
| 95 | + long copy = ByteStreams.copy(c, Channels.newChannel(ByteStreams.nullOutputStream())); |
| 96 | + assertThat(copy).isEqualTo(objectsFixture.getObj512KiB().getInfo().getSize()); |
| 97 | + |
| 98 | + ByteBuffer buf = ByteBuffer.allocate(8); |
| 99 | + int i = ThreadLocalRandom.current().nextInt(3, 10); |
| 100 | + for (int j = 0; j < i; j++) { |
| 101 | + assertWithMessage("expected EOF " + j).that(c.read(buf)).isEqualTo(-1); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments