|
17 | 17 | package com.google.cloud.spanner; |
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.junit.Assert.assertThat; |
20 | 22 | import static org.junit.Assert.fail; |
21 | 23 |
|
22 | 24 | import com.google.cloud.grpc.GrpcTransportOptions; |
23 | 25 | import com.google.cloud.spanner.spi.v1.SpannerRpc; |
24 | 26 | import java.util.HashMap; |
25 | 27 | import java.util.Map; |
26 | 28 | import java.util.concurrent.Callable; |
| 29 | +import javax.net.ssl.SSLHandshakeException; |
27 | 30 | import org.junit.Before; |
28 | 31 | import org.junit.Test; |
29 | 32 | import org.junit.runner.RunWith; |
@@ -133,4 +136,49 @@ public Void call() throws Exception { |
133 | 136 | assertThat(e.getMessage().contains("Unexpected exception thrown")); |
134 | 137 | } |
135 | 138 | } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void sslHandshakeExceptionIsNotRetryable() { |
| 142 | + // Verify that a SpannerException with code UNAVAILABLE and cause SSLHandshakeException is not |
| 143 | + // retryable. |
| 144 | + boolean gotExpectedException = false; |
| 145 | + try { |
| 146 | + SpannerImpl.runWithRetries( |
| 147 | + new Callable<Object>() { |
| 148 | + @Override |
| 149 | + public Void call() throws Exception { |
| 150 | + throw SpannerExceptionFactory.newSpannerException( |
| 151 | + ErrorCode.UNAVAILABLE, |
| 152 | + "This exception should not be retryable", |
| 153 | + new SSLHandshakeException("some SSL handshake exception")); |
| 154 | + } |
| 155 | + }); |
| 156 | + } catch (SpannerException e) { |
| 157 | + gotExpectedException = true; |
| 158 | + assertThat(e.isRetryable(), is(false)); |
| 159 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.UNAVAILABLE); |
| 160 | + assertThat(e.getMessage().contains("This exception should not be retryable")); |
| 161 | + } |
| 162 | + assertThat(gotExpectedException, is(true)); |
| 163 | + |
| 164 | + // Verify that any other SpannerException with code UNAVAILABLE is retryable. |
| 165 | + SpannerImpl.runWithRetries( |
| 166 | + new Callable<Object>() { |
| 167 | + private boolean firstTime = true; |
| 168 | + |
| 169 | + @Override |
| 170 | + public Void call() throws Exception { |
| 171 | + // Keep track of whethr this is the first call or a subsequent call to avoid an infinite |
| 172 | + // loop. |
| 173 | + if (firstTime) { |
| 174 | + firstTime = false; |
| 175 | + throw SpannerExceptionFactory.newSpannerException( |
| 176 | + ErrorCode.UNAVAILABLE, |
| 177 | + "This exception should be retryable", |
| 178 | + new Exception("some other exception")); |
| 179 | + } |
| 180 | + return null; |
| 181 | + } |
| 182 | + }); |
| 183 | + } |
136 | 184 | } |
0 commit comments