Skip to content

Commit

Permalink
googleapis#3889 added test for retryability of SSLHandshakeException
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Mar 8, 2019
1 parent 84969fb commit da72d8a
Showing 1 changed file with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.net.ssl.SSLHandshakeException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -33,6 +33,8 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.spi.v1.SpannerRpc;

/** Unit tests for {@link SpannerImpl}. */
@RunWith(JUnit4.class)
Expand Down Expand Up @@ -133,4 +135,44 @@ public Void call() throws Exception {
assertThat(e.getMessage().contains("Unexpected exception thrown"));
}
}

@Test
public void sslHandshakeExceptionIsNotRetryable() {
// Verify that a SpannerException with code UNAVAILABLE and cause SSLHandshakeException is not
// retryable.
boolean gotExpectedException = false;
try {
SpannerImpl.runWithRetries(new Callable<Object>() {
@Override
public Void call() throws Exception {
throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNAVAILABLE,
"This exception should not be retryable",
new SSLHandshakeException("some SSL handshake exception"));
}
});
} catch (SpannerException e) {
gotExpectedException = true;
assertThat(e.isRetryable(), is(false));
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.UNAVAILABLE);
assertThat(e.getMessage().contains("This exception should not be retryable"));
}
assertThat(gotExpectedException, is(true));

// Verify that any other SpannerException with code UNAVAILABLE is retryable.
SpannerImpl.runWithRetries(new Callable<Object>() {
private boolean firstTime = true;

@Override
public Void call() throws Exception {
// Keep track of whethr this is the first call or a subsequent call to avoid an infinite
// loop.
if (firstTime) {
firstTime = false;
throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNAVAILABLE,
"This exception should be retryable", new Exception("some other exception"));
}
return null;
}
});
}
}

0 comments on commit da72d8a

Please sign in to comment.