|
32 | 32 | import com.google.api.gax.rpc.ClientStreamReadyObserver; |
33 | 33 | import com.google.api.gax.rpc.ResponseObserver; |
34 | 34 | import com.google.cloud.storage.Backoff.Jitterer; |
| 35 | +import com.google.cloud.storage.BlobDescriptorStreamRead.AccumulatingRead; |
| 36 | +import com.google.cloud.storage.BlobDescriptorStreamRead.StreamingRead; |
35 | 37 | import com.google.cloud.storage.GrpcUtils.ZeroCopyBidiStreamingCallable; |
36 | 38 | import com.google.cloud.storage.ResponseContentLifecycleHandle.ChildRef; |
37 | 39 | import com.google.cloud.storage.RetryContext.OnFailure; |
38 | 40 | import com.google.cloud.storage.RetryContext.OnSuccess; |
39 | 41 | import com.google.cloud.storage.RetryContext.RetryContextProvider; |
40 | 42 | import com.google.cloud.storage.RetryContextTest.BlockingOnSuccess; |
41 | 43 | import com.google.cloud.storage.Retrying.RetryingDependencies; |
| 44 | +import com.google.protobuf.ByteString; |
42 | 45 | import com.google.storage.v2.BidiReadObjectRequest; |
43 | 46 | import com.google.storage.v2.BidiReadObjectResponse; |
44 | 47 | import com.google.storage.v2.BidiReadObjectSpec; |
45 | 48 | import com.google.storage.v2.BucketName; |
46 | 49 | import com.google.storage.v2.Object; |
47 | 50 | import java.io.IOException; |
| 51 | +import java.nio.ByteBuffer; |
48 | 52 | import java.nio.channels.AsynchronousCloseException; |
49 | 53 | import java.util.concurrent.Executors; |
50 | 54 | import java.util.concurrent.ScheduledExecutorService; |
51 | 55 | import java.util.concurrent.TimeUnit; |
| 56 | +import java.util.concurrent.atomic.AtomicBoolean; |
52 | 57 | import java.util.concurrent.atomic.AtomicLong; |
| 58 | +import java.util.function.Function; |
53 | 59 | import org.junit.AfterClass; |
54 | 60 | import org.junit.BeforeClass; |
55 | 61 | import org.junit.Test; |
@@ -229,6 +235,96 @@ public void closingShouldFailPendingReads() throws Exception { |
229 | 235 | }); |
230 | 236 | } |
231 | 237 |
|
| 238 | + @Test |
| 239 | + public void streamingRead_mustCloseQueuedResponsesWhenFailed() throws Exception { |
| 240 | + try (StreamingRead read1 = |
| 241 | + BlobDescriptorStreamRead.streamingRead(1, RangeSpec.all(), RetryContext.neverRetry())) { |
| 242 | + state.putOutstandingRead(1, read1); |
| 243 | + BlobDescriptorStream stream = |
| 244 | + BlobDescriptorStream.create(exec, callable, state, RetryContext.neverRetry()); |
| 245 | + |
| 246 | + ByteString bytes1 = ByteString.copyFrom(DataGenerator.base64Characters().genBytes(9)); |
| 247 | + ByteString bytes2 = ByteString.copyFrom(DataGenerator.base64Characters().genBytes(9)); |
| 248 | + |
| 249 | + AtomicBoolean bytes1Close = new AtomicBoolean(false); |
| 250 | + AtomicBoolean bytes2Close = new AtomicBoolean(false); |
| 251 | + |
| 252 | + try (ResponseContentLifecycleHandle<ByteString> handle = |
| 253 | + ResponseContentLifecycleHandle.create( |
| 254 | + bytes1, |
| 255 | + ByteString::asReadOnlyByteBufferList, |
| 256 | + () -> bytes1Close.compareAndSet(false, true))) { |
| 257 | + read1.accept(handle.borrow(Function.identity())); |
| 258 | + } |
| 259 | + try (ResponseContentLifecycleHandle<ByteString> handle = |
| 260 | + ResponseContentLifecycleHandle.create( |
| 261 | + bytes2, |
| 262 | + ByteString::asReadOnlyByteBufferList, |
| 263 | + () -> bytes2Close.compareAndSet(false, true))) { |
| 264 | + read1.accept(handle.borrow(Function.identity())); |
| 265 | + } |
| 266 | + |
| 267 | + // read some bytes, causing leftovers to be populated |
| 268 | + read1.read(ByteBuffer.allocate(1)); |
| 269 | + stream.close(); |
| 270 | + |
| 271 | + // call read again to observe the async close that happens |
| 272 | + IOException ioe = assertThrows(IOException.class, () -> read1.read(ByteBuffer.allocate(32))); |
| 273 | + |
| 274 | + assertAll( |
| 275 | + () -> assertThat(bytes1Close.get()).isTrue(), |
| 276 | + () -> assertThat(bytes2Close.get()).isTrue(), |
| 277 | + () -> assertThat(read1.acceptingBytes()).isFalse(), |
| 278 | + () -> assertThat(ioe).hasCauseThat().isInstanceOf(StorageException.class), |
| 279 | + () -> assertThat(ioe).hasCauseThat().hasMessageThat().contains("Parent stream shutdown")); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + public void accumulatingRead_mustCloseQueuedResponsesWhenFailed() throws Exception { |
| 285 | + SettableApiFuture<byte[]> complete = SettableApiFuture.create(); |
| 286 | + try (AccumulatingRead<byte[]> read1 = |
| 287 | + BlobDescriptorStreamRead.createByteArrayAccumulatingRead( |
| 288 | + 1, RangeSpec.all(), RetryContext.neverRetry(), complete)) { |
| 289 | + state.putOutstandingRead(1, read1); |
| 290 | + BlobDescriptorStream stream = |
| 291 | + BlobDescriptorStream.create(exec, callable, state, RetryContext.neverRetry()); |
| 292 | + |
| 293 | + ByteString bytes1 = ByteString.copyFrom(DataGenerator.base64Characters().genBytes(9)); |
| 294 | + ByteString bytes2 = ByteString.copyFrom(DataGenerator.base64Characters().genBytes(9)); |
| 295 | + |
| 296 | + AtomicBoolean bytes1Close = new AtomicBoolean(false); |
| 297 | + AtomicBoolean bytes2Close = new AtomicBoolean(false); |
| 298 | + |
| 299 | + try (ResponseContentLifecycleHandle<ByteString> handle = |
| 300 | + ResponseContentLifecycleHandle.create( |
| 301 | + bytes1, |
| 302 | + ByteString::asReadOnlyByteBufferList, |
| 303 | + () -> bytes1Close.compareAndSet(false, true))) { |
| 304 | + read1.accept(handle.borrow(Function.identity())); |
| 305 | + } |
| 306 | + try (ResponseContentLifecycleHandle<ByteString> handle = |
| 307 | + ResponseContentLifecycleHandle.create( |
| 308 | + bytes2, |
| 309 | + ByteString::asReadOnlyByteBufferList, |
| 310 | + () -> bytes2Close.compareAndSet(false, true))) { |
| 311 | + read1.accept(handle.borrow(Function.identity())); |
| 312 | + } |
| 313 | + |
| 314 | + stream.close(); |
| 315 | + |
| 316 | + StorageException se = |
| 317 | + assertThrows( |
| 318 | + StorageException.class, () -> TestUtils.await(complete, 2, TimeUnit.SECONDS)); |
| 319 | + assertAll( |
| 320 | + () -> assertThat(bytes1Close.get()).isTrue(), |
| 321 | + () -> assertThat(bytes2Close.get()).isTrue(), |
| 322 | + () -> assertThat(read1.acceptingBytes()).isFalse(), |
| 323 | + () -> assertThat(se).hasMessageThat().contains("Parent stream shutdown"), |
| 324 | + () -> assertThat(se).hasCauseThat().isInstanceOf(AsynchronousCloseException.class)); |
| 325 | + } |
| 326 | + } |
| 327 | + |
232 | 328 | private static class TestBlobDescriptorStreamRead extends BlobDescriptorStreamRead { |
233 | 329 |
|
234 | 330 | private static final AtomicLong readIdSeq = new AtomicLong(1); |
|
0 commit comments