diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 156f9684f8ba..391876b3c216 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -41,7 +41,9 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -102,6 +104,9 @@ public class DatastoreTest { private static LocalGcdHelper gcdHelper; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @BeforeClass public static void beforeClass() throws IOException, InterruptedException { if (!LocalGcdHelper.isActive(PROJECT_ID)) { @@ -635,7 +640,7 @@ public void testKeyFactory() { } @Test - public void testRetires() throws Exception { + public void testRetryableException() throws Exception { DatastoreV1.LookupRequest requestPb = DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() @@ -657,4 +662,51 @@ public void testRetires() throws Exception { assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); } + + @Test + public void testNonRetryableException() throws Exception { + DatastoreV1.LookupRequest requestPb = + DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + EasyMock.expect(rpcMock.lookup(requestPb)) + .andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED)) + .times(1); + EasyMock.replay(rpcFactoryMock, rpcMock); + RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build(); + DatastoreOptions options = this.options.toBuilder() + .retryParams(retryParams) + .serviceRpcFactory(rpcFactoryMock) + .build(); + Datastore datastore = DatastoreFactory.instance().get(options); + thrown.expect(DatastoreException.class); + thrown.expectMessage(Reason.PERMISSION_DENIED.description()); + datastore.get(KEY1); + EasyMock.verify(rpcFactoryMock, rpcMock); + } + + @Test + public void testRuntimeException() throws Exception { + DatastoreV1.LookupRequest requestPb = + DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); + DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class); + DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class))) + .andReturn(rpcMock); + String exceptionMessage = "Artificial runtime exception"; + EasyMock.expect(rpcMock.lookup(requestPb)) + .andThrow(new RuntimeException(exceptionMessage)); + EasyMock.replay(rpcFactoryMock, rpcMock); + DatastoreOptions options = this.options.toBuilder() + .retryParams(RetryParams.getDefaultInstance()) + .serviceRpcFactory(rpcFactoryMock) + .build(); + Datastore datastore = DatastoreFactory.instance().get(options); + thrown.expect(DatastoreException.class); + thrown.expectMessage(exceptionMessage); + datastore.get(KEY1); + EasyMock.verify(rpcFactoryMock, rpcMock); + } }