|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.rpc; |
| 31 | + |
| 32 | +import static com.google.api.gax.rpc.ResumableUploadCommand.FINALIZE; |
| 33 | +import static com.google.api.gax.rpc.ResumableUploadCommand.QUERY; |
| 34 | +import static com.google.api.gax.rpc.ResumableUploadCommand.START; |
| 35 | +import static com.google.api.gax.rpc.ResumableUploadCommand.UPLOAD; |
| 36 | +import static com.google.api.gax.rpc.ResumableUploadCommand.UPLOAD_FINALIZE; |
| 37 | +import static com.google.api.gax.rpc.ResumableUploadErrorClassifier.Category.FATAL; |
| 38 | +import static com.google.api.gax.rpc.ResumableUploadErrorClassifier.Category.RECOVERABLE; |
| 39 | +import static com.google.api.gax.rpc.ResumableUploadErrorClassifier.Category.TRANSIENT; |
| 40 | +import static com.google.common.truth.Truth.assertThat; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 42 | + |
| 43 | +import com.google.api.gax.rpc.StatusCode.Code; |
| 44 | +import java.io.IOException; |
| 45 | +import java.net.SocketException; |
| 46 | +import java.net.SocketTimeoutException; |
| 47 | +import java.net.UnknownHostException; |
| 48 | +import java.util.concurrent.CancellationException; |
| 49 | +import org.jspecify.annotations.Nullable; |
| 50 | +import org.junit.jupiter.api.Test; |
| 51 | + |
| 52 | +class ResumableUploadErrorClassifierTest { |
| 53 | + |
| 54 | + private static StatusCode statusCode(@Nullable Integer httpStatus, Code code) { |
| 55 | + return new StatusCode() { |
| 56 | + @Override |
| 57 | + public Code getCode() { |
| 58 | + return code; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public @Nullable Object getTransportCode() { |
| 63 | + return httpStatus; |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + private static ApiException createApiException(@Nullable Integer httpStatus, Code code) { |
| 69 | + return createApiException(httpStatus, code, null); |
| 70 | + } |
| 71 | + |
| 72 | + private static ApiException createApiException( |
| 73 | + @Nullable Integer httpStatus, Code code, @Nullable Throwable cause) { |
| 74 | + return ApiExceptionFactory.createException( |
| 75 | + "HTTP " + httpStatus, cause, statusCode(httpStatus, code), false); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testTransientHttpErrors_areTransientAcrossCommands() { |
| 80 | + ApiException error503 = createApiException(503, Code.UNAVAILABLE); |
| 81 | + assertThat(ResumableUploadErrorClassifier.classify(error503, START)).isEqualTo(TRANSIENT); |
| 82 | + assertThat(ResumableUploadErrorClassifier.classify(error503, UPLOAD)).isEqualTo(TRANSIENT); |
| 83 | + assertThat(ResumableUploadErrorClassifier.classify(error503, QUERY)).isEqualTo(TRANSIENT); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testUnknownStatusCode_transientOnlyWhenCausedBySocketTimeoutException() { |
| 88 | + ApiException wrappedTimeout = |
| 89 | + createApiException(500, Code.UNKNOWN, new SocketTimeoutException("read timeout")); |
| 90 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedTimeout, START)).isEqualTo(TRANSIENT); |
| 91 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedTimeout, UPLOAD)) |
| 92 | + .isEqualTo(TRANSIENT); |
| 93 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedTimeout, QUERY)).isEqualTo(TRANSIENT); |
| 94 | + |
| 95 | + ApiException wrappedSocket = |
| 96 | + createApiException(500, Code.UNKNOWN, new SocketException("connection reset")); |
| 97 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedSocket, UPLOAD)).isEqualTo(FATAL); |
| 98 | + |
| 99 | + ApiException wrappedUnknownHost = |
| 100 | + createApiException(500, Code.UNKNOWN, new UnknownHostException("unknown host")); |
| 101 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedUnknownHost, UPLOAD)) |
| 102 | + .isEqualTo(FATAL); |
| 103 | + |
| 104 | + ApiException genericIo = |
| 105 | + createApiException(500, Code.UNKNOWN, new IOException("generic io error")); |
| 106 | + assertThat(ResumableUploadErrorClassifier.classify(genericIo, UPLOAD)).isEqualTo(FATAL); |
| 107 | + |
| 108 | + ApiException wrappedRuntime = |
| 109 | + createApiException(500, Code.UNKNOWN, new IllegalStateException("local bug")); |
| 110 | + assertThat(ResumableUploadErrorClassifier.classify(wrappedRuntime, UPLOAD)).isEqualTo(FATAL); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testRecoverableHttpErrors_dependOnCommand() { |
| 115 | + ApiException error412 = createApiException(412, Code.FAILED_PRECONDITION); |
| 116 | + |
| 117 | + // Chunk transmission commands can recover by querying server status. |
| 118 | + assertThat(ResumableUploadErrorClassifier.classify(error412, UPLOAD)).isEqualTo(RECOVERABLE); |
| 119 | + assertThat(ResumableUploadErrorClassifier.classify(error412, FINALIZE)).isEqualTo(RECOVERABLE); |
| 120 | + assertThat(ResumableUploadErrorClassifier.classify(error412, UPLOAD_FINALIZE)) |
| 121 | + .isEqualTo(RECOVERABLE); |
| 122 | + |
| 123 | + // Non-chunk commands cannot recover via byte-offset query. |
| 124 | + assertThat(ResumableUploadErrorClassifier.classify(error412, START)).isEqualTo(FATAL); |
| 125 | + assertThat(ResumableUploadErrorClassifier.classify(error412, QUERY)).isEqualTo(FATAL); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void testCommand_isRecoverable() { |
| 130 | + assertThat(START.isRecoverable()).isFalse(); |
| 131 | + assertThat(UPLOAD.isRecoverable()).isTrue(); |
| 132 | + assertThat(FINALIZE.isRecoverable()).isTrue(); |
| 133 | + assertThat(UPLOAD_FINALIZE.isRecoverable()).isTrue(); |
| 134 | + assertThat(QUERY.isRecoverable()).isFalse(); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testUnmappedHttpCodesAndNonApiExceptions_areFatal() { |
| 139 | + assertThat( |
| 140 | + ResumableUploadErrorClassifier.classify( |
| 141 | + createApiException(403, Code.PERMISSION_DENIED), UPLOAD)) |
| 142 | + .isEqualTo(FATAL); |
| 143 | + assertThat( |
| 144 | + ResumableUploadErrorClassifier.classify( |
| 145 | + createApiException(null, Code.FAILED_PRECONDITION), UPLOAD)) |
| 146 | + .isEqualTo(FATAL); |
| 147 | + assertThat( |
| 148 | + ResumableUploadErrorClassifier.classify(new CancellationException("cancelled"), UPLOAD)) |
| 149 | + .isEqualTo(FATAL); |
| 150 | + assertThat( |
| 151 | + ResumableUploadErrorClassifier.classify(new IllegalStateException("bad state"), UPLOAD)) |
| 152 | + .isEqualTo(FATAL); |
| 153 | + |
| 154 | + assertThrows( |
| 155 | + NullPointerException.class, () -> ResumableUploadErrorClassifier.classify(null, UPLOAD)); |
| 156 | + assertThrows( |
| 157 | + NullPointerException.class, |
| 158 | + () -> |
| 159 | + ResumableUploadErrorClassifier.classify( |
| 160 | + createApiException(null, Code.FAILED_PRECONDITION), null)); |
| 161 | + } |
| 162 | +} |
0 commit comments