Skip to content

Commit

Permalink
Merge pull request #189 from ajkannan/datastore-exception-handling
Browse files Browse the repository at this point in the history
Add tests for runtime and non-retryable exception handling
  • Loading branch information
aozarov committed Sep 30, 2015
2 parents abb421e + e776320 commit e13fc60
Showing 1 changed file with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
}

0 comments on commit e13fc60

Please sign in to comment.