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

fix: handle thrown exceptions in runAsyncTransaction callback #671

Merged
merged 5 commits into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -146,24 +146,37 @@ public void run() {
*/
private SettableApiFuture<T> invokeUserCallback() {
final SettableApiFuture<T> callbackResult = SettableApiFuture.create();

userCallbackExecutor.execute(
new Runnable() {
@Override
public void run() {
ApiFutures.addCallback(
userCallback.updateCallback(transaction),
new ApiFutureCallback<T>() {
@Override
public void onFailure(Throwable t) {
callbackResult.setException(t);
}
try {
ApiFutures.addCallback(
ApiFutures.catchingAsync(
userCallback.updateCallback(transaction),
Throwable.class,
new ApiAsyncFunction<Throwable, T>() {
public ApiFuture<T> apply(Throwable throwable) throws Exception {
return ApiFutures.immediateFailedFuture(throwable);
}
},
MoreExecutors.directExecutor()),
new ApiFutureCallback<T>() {
@Override
public void onFailure(Throwable t) {
callbackResult.setException(t);
}

@Override
public void onSuccess(T result) {
callbackResult.set(result);
}
},
MoreExecutors.directExecutor());
@Override
public void onSuccess(T result) {
callbackResult.set(result);
}
},
firestoreExecutor);
} catch (Exception e) {
callbackResult.setException(e);
}
}
});
return callbackResult;
Expand Down
Expand Up @@ -253,7 +253,7 @@ public String updateCallback(Transaction transaction) throws Exception {
}

@Test
public void rollbackOnCallbackErrorAsync() {
public void rollbackOnCallbackApiFutureErrorAsync() {
doReturn(beginResponse())
.doReturn(rollbackResponse())
.when(firestoreMock)
Expand Down Expand Up @@ -339,6 +339,31 @@ public ApiFuture<String> updateCallback(Transaction transaction) {
assertEquals(1, requests.size());
}

@Test
public void noRollbackOnThrownExceptionAsync() {
doReturn(beginResponse())
.doReturn(rollbackResponse())
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

ApiFuture<String> transaction =
firestoreMock.runAsyncTransaction(
new Transaction.AsyncFunction<String>() {
@Override
public ApiFuture<String> updateCallback(Transaction transaction) {
throw new RuntimeException("User exception");
}
},
options);

try {
transaction.get();
fail();
} catch (Exception e) {
assertTrue(e.getMessage().endsWith("User exception"));
}
}

@Test
public void limitsRetriesWithFailure() {
ResponseStubber responseStubber =
Expand Down
Expand Up @@ -688,6 +688,24 @@ public String updateCallback(Transaction transaction) {
}
}

@Test
public void asyncTxFailsWithUserError() throws Exception {
try {
firestore
.runAsyncTransaction(
new Transaction.AsyncFunction<String>() {
@Override
public ApiFuture<String> updateCallback(Transaction transaction) {
throw new RuntimeException("User exception");
}
})
.get();
fail();
} catch (Exception e) {
assertTrue(e.getMessage().endsWith("User exception"));
}
}

@Test
public void doesNotRetryTransactionsWithFailedPreconditions() {
final DocumentReference documentReference = randomColl.document();
Expand Down