use of com.datastax.oss.driver.api.core.AllNodesFailedException in project java-driver by datastax.
the class ShutdownIT method should_fail_requests_when_session_is_closed.
@Test
public void should_fail_requests_when_session_is_closed() throws Exception {
// Given
// Prime with a bit of delay to increase the chance that a query will be aborted in flight when
// we force-close the session
SIMULACRON_RULE.cluster().prime(when(QUERY_STRING).then(noRows()).delay(20, TimeUnit.MILLISECONDS));
CqlSession session = SessionUtils.newSession(SIMULACRON_RULE);
// When
// Max out the in-flight requests on the connection (from a separate thread pool to get a bit of
// contention), then force-close the session abruptly.
Set<String> unexpectedErrors = new ConcurrentSkipListSet<>();
ExecutorService requestExecutor = Executors.newFixedThreadPool(4);
int maxConcurrentRequests = session.getContext().getConfig().getDefaultProfile().getInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS);
Semaphore semaphore = new Semaphore(maxConcurrentRequests);
CountDownLatch gotSessionClosedError = new CountDownLatch(1);
for (int i = 0; i < 4; i++) {
requestExecutor.execute(() -> {
try {
while (!Thread.currentThread().isInterrupted()) {
semaphore.acquire();
session.executeAsync(QUERY_STRING).whenComplete((ignoredResult, error) -> {
semaphore.release();
// => AllNodesFailedException wrapping IllegalStateException
if (error instanceof IllegalStateException && "Session is closed".equals(error.getMessage())) {
gotSessionClosedError.countDown();
} else if (error instanceof AllNodesFailedException) {
AllNodesFailedException anfe = (AllNodesFailedException) error;
// acceptable.
if (anfe.getAllErrors().size() > 0) {
assertThat(anfe.getAllErrors()).hasSize(1);
error = anfe.getAllErrors().values().iterator().next().get(0);
if (!(error instanceof IllegalStateException) && !error.getMessage().endsWith("is closing")) {
unexpectedErrors.add(error.toString());
}
}
} else if (error != null && !(error instanceof ClosedConnectionException)) {
unexpectedErrors.add(error.toString());
}
});
}
} catch (InterruptedException e) {
// return
}
});
}
TimeUnit.MILLISECONDS.sleep(1000);
session.forceCloseAsync();
assertThat(gotSessionClosedError.await(10, TimeUnit.SECONDS)).as("Expected to get the 'Session is closed' error shortly after shutting down").isTrue();
requestExecutor.shutdownNow();
// Then
assertThat(unexpectedErrors).isEmpty();
}
Aggregations