|
| 1 | +/* |
| 2 | + * Copyright 2024 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.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | + |
| 22 | +import com.google.api.core.ApiClock; |
| 23 | +import com.google.api.core.NanoClock; |
| 24 | +import com.google.api.gax.grpc.GrpcStatusCode; |
| 25 | +import com.google.api.gax.retrying.BasicResultRetryAlgorithm; |
| 26 | +import com.google.api.gax.retrying.RetrySettings; |
| 27 | +import com.google.api.gax.rpc.ApiException; |
| 28 | +import com.google.api.gax.rpc.ApiExceptionFactory; |
| 29 | +import com.google.api.gax.rpc.CancelledException; |
| 30 | +import com.google.api.gax.rpc.ResourceExhaustedException; |
| 31 | +import com.google.cloud.storage.Retrying.RetryingDependencies; |
| 32 | +import io.grpc.Status.Code; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public final class RetryContextTest { |
| 36 | + |
| 37 | + private static final Throwable T1 = apiException(Code.UNAVAILABLE, "{unavailable}"); |
| 38 | + private static final Throwable T2 = apiException(Code.INTERNAL, "{internal}"); |
| 39 | + private static final Throwable T3 = apiException(Code.RESOURCE_EXHAUSTED, "{resource exhausted}"); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void retriable_cancelledException_when_maxAttemptBudget_consumed() { |
| 43 | + RetryContext ctx = RetryContext.of(maxAttempts(1), Retrying.alwaysRetry()); |
| 44 | + |
| 45 | + CancelledException cancelled = |
| 46 | + assertThrows(CancelledException.class, () -> ctx.recordError(T1)); |
| 47 | + |
| 48 | + assertThat(cancelled).hasCauseThat().isEqualTo(T1); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void retriable_maxAttemptBudget_still_available() { |
| 53 | + RetryContext ctx = RetryContext.of(maxAttempts(2), Retrying.alwaysRetry()); |
| 54 | + |
| 55 | + ctx.recordError(T1); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void |
| 60 | + retriable_cancelledException_when_maxAttemptBudget_multipleAttempts_previousErrorsIncludedAsSuppressed() { |
| 61 | + RetryContext ctx = RetryContext.of(maxAttempts(3), Retrying.alwaysRetry()); |
| 62 | + |
| 63 | + ctx.recordError(T1); |
| 64 | + ctx.recordError(T2); |
| 65 | + |
| 66 | + CancelledException cancelled = |
| 67 | + assertThrows(CancelledException.class, () -> ctx.recordError(T3)); |
| 68 | + |
| 69 | + assertThat(cancelled).hasCauseThat().isEqualTo(T3); |
| 70 | + Throwable[] suppressed = cancelled.getSuppressed(); |
| 71 | + assertThat(suppressed).asList().containsExactly(T1, T2); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void nonRetriable_cancelledException_regardlessOfAttemptBudget() { |
| 76 | + RetryContext ctx = RetryContext.of(maxAttempts(3), Retrying.neverRetry()); |
| 77 | + |
| 78 | + CancelledException cancelled = |
| 79 | + assertThrows(CancelledException.class, () -> ctx.recordError(T1)); |
| 80 | + |
| 81 | + assertThat(cancelled).hasCauseThat().isEqualTo(T1); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void |
| 86 | + nonRetriable_cancelledException_regardlessOfAttemptBudget_previousErrorsIncludedAsSuppressed() { |
| 87 | + RetryContext ctx = |
| 88 | + RetryContext.of( |
| 89 | + maxAttempts(6), |
| 90 | + new BasicResultRetryAlgorithm<Object>() { |
| 91 | + @Override |
| 92 | + public boolean shouldRetry(Throwable previousThrowable, Object previousResponse) { |
| 93 | + return !(previousThrowable instanceof ResourceExhaustedException); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + ctx.recordError(T1); |
| 98 | + ctx.recordError(T2); |
| 99 | + |
| 100 | + CancelledException cancelled = |
| 101 | + assertThrows(CancelledException.class, () -> ctx.recordError(T3)); |
| 102 | + |
| 103 | + assertThat(cancelled).hasCauseThat().isEqualTo(T3); |
| 104 | + Throwable[] suppressed = cancelled.getSuppressed(); |
| 105 | + assertThat(suppressed).asList().containsExactly(T1, T2); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void resetDiscardsPreviousErrors() { |
| 110 | + RetryContext ctx = |
| 111 | + RetryContext.of( |
| 112 | + maxAttempts(6), |
| 113 | + new BasicResultRetryAlgorithm<Object>() { |
| 114 | + @Override |
| 115 | + public boolean shouldRetry(Throwable previousThrowable, Object previousResponse) { |
| 116 | + return !(previousThrowable instanceof ResourceExhaustedException); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + ctx.recordError(T1); |
| 121 | + ctx.recordError(T2); |
| 122 | + ctx.reset(); |
| 123 | + |
| 124 | + CancelledException cancelled = |
| 125 | + assertThrows(CancelledException.class, () -> ctx.recordError(T3)); |
| 126 | + |
| 127 | + assertThat(cancelled).hasCauseThat().isEqualTo(T3); |
| 128 | + Throwable[] suppressed = cancelled.getSuppressed(); |
| 129 | + assertThat(suppressed).asList().isEmpty(); |
| 130 | + } |
| 131 | + |
| 132 | + private static ApiException apiException(Code code, String message) { |
| 133 | + return ApiExceptionFactory.createException(message, null, GrpcStatusCode.of(code), false); |
| 134 | + } |
| 135 | + |
| 136 | + private static MaxAttemptRetryingDependencies maxAttempts(int maxAttempts) { |
| 137 | + return new MaxAttemptRetryingDependencies( |
| 138 | + RetrySettings.newBuilder().setMaxAttempts(maxAttempts).build(), |
| 139 | + NanoClock.getDefaultClock()); |
| 140 | + } |
| 141 | + |
| 142 | + private static final class MaxAttemptRetryingDependencies implements RetryingDependencies { |
| 143 | + private final RetrySettings settings; |
| 144 | + private final ApiClock clock; |
| 145 | + |
| 146 | + private MaxAttemptRetryingDependencies(RetrySettings settings, ApiClock clock) { |
| 147 | + this.settings = settings; |
| 148 | + this.clock = clock; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public RetrySettings getRetrySettings() { |
| 153 | + return settings; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ApiClock getClock() { |
| 158 | + return clock; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments