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: stop invoking callback after pausing and cancelling result set #1192

Merged
merged 1 commit into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -252,6 +252,13 @@ public void run() {
if (cursorReturnedDoneOrException) {
break;
}
if (state == State.CANCELLED) {
// The callback should always get at least one chance to catch the CANCELLED
// exception. It is however possible that the callback does not call tryNext(), and
// instead directly returns PAUSE or DONE. In those cases, the callback runner should
// also stop, even though the callback has not seen the CANCELLED state.
cursorReturnedDoneOrException = true;
}
}
CallbackResponse response;
try {
Expand Down
Expand Up @@ -16,7 +16,9 @@

package com.google.cloud.spanner;

import static com.google.cloud.spanner.SpannerApiFutures.get;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -371,6 +373,33 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
}
}

@Test
public void testCallbackIsNotCalledWhilePausedAndCanceled()
throws InterruptedException, ExecutionException {
Executor executor = Executors.newSingleThreadExecutor();
ResultSet delegate = mock(ResultSet.class);

final AtomicInteger callbackCounter = new AtomicInteger();
ApiFuture<Void> callbackResult;

try (AsyncResultSetImpl rs =
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
callbackResult =
rs.setCallback(
executor,
resultSet -> {
callbackCounter.getAndIncrement();
return CallbackResponse.PAUSE;
});

rs.cancel();

SpannerException exception = assertThrows(SpannerException.class, () -> get(callbackResult));
assertEquals(ErrorCode.CANCELLED, exception.getErrorCode());
assertEquals(1, callbackCounter.get());
}
}

@Test
public void cancel() throws InterruptedException {
Executor executor = Executors.newSingleThreadExecutor();
Expand Down