Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for runtime and non-retryable exception handling #189

Merged
merged 2 commits into from
Sep 30, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}