From 8928c498a3c90586990c13e5496e406c064cd35a Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Sep 2015 13:53:09 -0700 Subject: [PATCH 1/2] Add test for runtime exception --- .../gcloud/datastore/DatastoreTest.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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..98f30b075071 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 testRetries() throws Exception { DatastoreV1.LookupRequest requestPb = DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() @@ -657,4 +662,27 @@ public void testRetires() throws Exception { assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); } + + @Test + public void testRuntimeExceptionHandling() 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); + } } From e776320245ef694d89fbbf59195487a141aebf4d Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Sep 2015 17:59:31 -0700 Subject: [PATCH 2/2] add test for nonretryable DatastoreException --- .../gcloud/datastore/DatastoreTest.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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 98f30b075071..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 @@ -640,7 +640,7 @@ public void testKeyFactory() { } @Test - public void testRetries() throws Exception { + public void testRetryableException() throws Exception { DatastoreV1.LookupRequest requestPb = DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build(); DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder() @@ -664,7 +664,31 @@ public void testRetries() throws Exception { } @Test - public void testRuntimeExceptionHandling() throws Exception { + 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);