@@ -404,6 +404,115 @@ void testResumeCall_throwsUnsupportedOperationException() {
404404 () -> callable .resumeCall ("https://upload.url/session" , streamOf ("data" ), null ));
405405 }
406406
407+ @ Test
408+ void testChunkRetry_transientFailureThenSuccess_retriesAndSucceeds () throws Exception {
409+ stubStartSession ("https://upload.url/chunk-retry-ok" );
410+ TrackableStream stream = new TrackableStream ("01234567" ); // exactly 1 chunk of 8 bytes
411+ when (mockChunkCallable .futureCall (any (ChunkUploadRequest .class ), any ()))
412+ .thenReturn (
413+ ApiFutures .immediateFailedFuture (createApiException (503 , StatusCode .Code .UNAVAILABLE )))
414+ .thenReturn (
415+ ApiFutures .immediateFuture (
416+ ChunkUploadResponse .create (ResumableUploadStatus .FINAL , "chunk-done" )));
417+
418+ ResumableUploadFuture <String > future = callable .futureCall ("resource-path" , stream , null );
419+
420+ assertThat (future .get ()).isEqualTo ("chunk-done" );
421+ assertThat (future .isDone ()).isTrue ();
422+ assertThat (stream .totalBytesRead ).isEqualTo (8 );
423+ assertThat (stream .closed ).isTrue ();
424+
425+ ArgumentCaptor <ChunkUploadRequest > captor = ArgumentCaptor .forClass (ChunkUploadRequest .class );
426+ verify (mockChunkCallable , times (2 )).futureCall (captor .capture (), any ());
427+ List <ChunkUploadRequest > requests = captor .getAllValues ();
428+ assertThat (requests .get (0 ).getOffset ()).isEqualTo (0 );
429+ assertThat (requests .get (0 ).getPayload ()).isEqualTo ("01234567" .getBytes (StandardCharsets .UTF_8 ));
430+ assertThat (requests .get (1 ).getOffset ()).isEqualTo (0 );
431+ assertThat (requests .get (1 ).getPayload ()).isEqualTo ("01234567" .getBytes (StandardCharsets .UTF_8 ));
432+ }
433+
434+ @ Test
435+ void testChunkRetry_transientFailureExhaustion_surfacesLastError () {
436+ stubStartSession ("https://upload.url/chunk-exhaustion" );
437+ when (mockChunkCallable .futureCall (any (ChunkUploadRequest .class ), any ()))
438+ .thenReturn (
439+ ApiFutures .immediateFailedFuture (createApiException (503 , StatusCode .Code .UNAVAILABLE )));
440+
441+ ResumableUploadFuture <String > future =
442+ callable .futureCall ("resource-path" , streamOf ("hello" ), null );
443+
444+ ExecutionException exception = assertThrows (ExecutionException .class , future ::get );
445+ assertThat (exception .getCause ()).isInstanceOf (ApiException .class );
446+ assertThat (((ApiException ) exception .getCause ()).getStatusCode ().getTransportCode ())
447+ .isEqualTo (503 );
448+
449+ // Default chunk retry settings has maxAttempts = 5
450+ verify (mockChunkCallable , times (5 )).futureCall (any (), any ());
451+ }
452+
453+ @ Test
454+ void testChunkRetry_cancellationDuringBackoff_deschedulesPendingAttempt () {
455+ SettableApiFuture <ChunkUploadResponse <String >> chunkAttempt0Future = SettableApiFuture .create ();
456+ when (mockChunkCallable .futureCall (any (ChunkUploadRequest .class ), any ()))
457+ .thenReturn (chunkAttempt0Future )
458+ .thenReturn (
459+ ApiFutures .immediateFuture (
460+ ChunkUploadResponse .create (ResumableUploadStatus .FINAL , "should-not-reach" )));
461+
462+ ClientContext clientContext =
463+ ClientContext .newBuilder ().setDefaultCallContext (callContext ).build ();
464+ SettableApiFuture <ResumableUploadSession > startSessionFuture = SettableApiFuture .create ();
465+ startSessionFuture .set (
466+ ResumableUploadSession .newBuilder ()
467+ .setUploadUrl ("https://upload.url/cancel-backoff" )
468+ .build ());
469+ ResumableUploadCallSettings customSettings =
470+ defaultSettings .toBuilder ().setChunkSize (8 ).build ();
471+ ResumableUploadFutureImpl <String > sessionFuture =
472+ ResumableUploadFutureImpl .create (
473+ startSessionFuture ,
474+ mockChunkCallable ,
475+ streamOf ("hello" ),
476+ customSettings ,
477+ clientContext );
478+
479+ // Fail attempt 0 with 503 to schedule backoff
480+ chunkAttempt0Future .setException (createApiException (503 , StatusCode .Code .UNAVAILABLE ));
481+
482+ // Cancel while backoff is pending
483+ assertThat (sessionFuture .cancel (true )).isTrue ();
484+ assertThat (sessionFuture .isCancelled ()).isTrue ();
485+ assertThrows (CancellationException .class , sessionFuture ::get );
486+
487+ // Only attempt 0 occurred; attempt 1 was de-scheduled
488+ verify (mockChunkCallable , times (1 )).futureCall (any (), any ());
489+ }
490+
491+ private static class HttpStatusStatusCode implements StatusCode {
492+ private final int httpStatus ;
493+ private final StatusCode .Code code ;
494+
495+ HttpStatusStatusCode (int httpStatus , StatusCode .Code code ) {
496+ this .httpStatus = httpStatus ;
497+ this .code = code ;
498+ }
499+
500+ @ Override
501+ public StatusCode .Code getCode () {
502+ return code ;
503+ }
504+
505+ @ Override
506+ public Integer getTransportCode () {
507+ return httpStatus ;
508+ }
509+ }
510+
511+ private static ApiException createApiException (int httpStatus , StatusCode .Code code ) {
512+ return ApiExceptionFactory .createException (
513+ "HTTP " + httpStatus , null , new HttpStatusStatusCode (httpStatus , code ), false );
514+ }
515+
407516 private void stubStartSession (String uploadUrl ) {
408517 when (mockStartCallable .futureCall (any (), any ()))
409518 .thenReturn (
@@ -424,11 +533,21 @@ private static void assertChunk(
424533
425534 private static class TrackableStream extends ByteArrayInputStream {
426535 boolean closed = false ;
536+ int totalBytesRead = 0 ;
427537
428538 TrackableStream (String content ) {
429539 super (content .getBytes (StandardCharsets .UTF_8 ));
430540 }
431541
542+ @ Override
543+ public int read (byte [] b , int off , int len ) {
544+ int read = super .read (b , off , len );
545+ if (read > 0 ) {
546+ totalBytesRead += read ;
547+ }
548+ return read ;
549+ }
550+
432551 @ Override
433552 public void close () throws IOException {
434553 closed = true ;
0 commit comments