Skip to content

Commit 0625f80

Browse files
committed
address comments
1 parent 8c4b537 commit 0625f80

4 files changed

Lines changed: 45 additions & 19 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/BaseService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) {
3939
public RetryResult beforeEval(Exception exception) {
4040
if (exception instanceof BaseServiceException) {
4141
boolean retriable = ((BaseServiceException) exception).isRetryable();
42-
return retriable ? Interceptor.RetryResult.RETRY : RetryResult.CONTINUE_EVALUATION;
42+
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.CONTINUE_EVALUATION;
4343
}
4444
return Interceptor.RetryResult.CONTINUE_EVALUATION;
4545
}

google-cloud-datastore/src/main/java/com/google/cloud/datastore/TransactionExceptionHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
public class TransactionExceptionHandler {
2323
public static final Interceptor TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR =
2424
new Interceptor() {
25+
private final int ABORTED_CODE = 10;
26+
2527
@Override
2628
public RetryResult beforeEval(Exception exception) {
2729
if (exception instanceof DatastoreException) {
2830
DatastoreException e = getInnerException((DatastoreException) exception);
29-
if (e.getCode() == 10 || e.getReason() == "ABORTED") {
30-
return RetryResult.RETRY;
31+
if (e.getCode() == ABORTED_CODE || e.getReason() == "ABORTED") {
32+
return Interceptor.RetryResult.RETRY;
3133
}
3234
}
3335
return Interceptor.RetryResult.CONTINUE_EVALUATION;
@@ -53,4 +55,4 @@ public static ExceptionHandler build() {
5355
DatastoreImpl.EXCEPTION_HANDLER_INTERCEPTOR, TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR)
5456
.build();
5557
}
56-
}
58+
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/TransactionExceptionHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ public void testShouldTry() {
5757
assertTrue(transactionHandler.accept(nestedDatastoreException));
5858
assertFalse(handler.accept(nestedDatastoreException));
5959
}
60-
}
60+
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -733,19 +733,43 @@ public void testDelete() {
733733

734734
@Test
735735
public void testRunInTransaction() {
736-
Datastore.TransactionCallable<Integer> callable = new Datastore.TransactionCallable<Integer>() {
737-
private Integer attempts = 0;
738-
public Integer run(DatastoreReaderWriter transaction) {
739-
transaction.get(KEY1);
740-
if (attempts < 1) {
741-
++attempts;
742-
throw new DatastoreException(10,"","ABORTED",false,null);
743-
}
744-
745-
return attempts;
746-
}
747-
};
748-
int result = DATASTORE.runInTransaction(callable);
749-
assertTrue(result == 1);
736+
Datastore.TransactionCallable<Integer> callable1 =
737+
new Datastore.TransactionCallable<Integer>() {
738+
private Integer attempts = 0;
739+
740+
public Integer run(DatastoreReaderWriter transaction) {
741+
transaction.get(KEY1);
742+
if (attempts < 1) {
743+
++attempts;
744+
throw new DatastoreException(10, "", "ABORTED", false, null);
745+
}
746+
747+
return attempts;
748+
}
749+
};
750+
751+
int result = DATASTORE.runInTransaction(callable1);
752+
assertEquals(result, 1);
753+
754+
Datastore.TransactionCallable<Integer> callable2 =
755+
new Datastore.TransactionCallable<Integer>() {
756+
private Integer attempts = 0;
757+
758+
public Integer run(DatastoreReaderWriter transaction) {
759+
transaction.get(KEY1);
760+
if (attempts < 1) {
761+
++attempts;
762+
throw new DatastoreException(4, "", "DEADLINE_EXCEEDED", false, null);
763+
}
764+
return attempts;
765+
}
766+
};
767+
768+
try {
769+
DATASTORE.runInTransaction(callable2);
770+
fail("Expecting a failure");
771+
} catch (DatastoreException expected) {
772+
assertEquals(((DatastoreException) expected.getCause()).getCode(), 4);
773+
}
750774
}
751775
}

0 commit comments

Comments
 (0)