use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.
the class DefaultSessionPoolsTest method should_close_pool_if_recreated_while_closing.
@Test
public void should_close_pool_if_recreated_while_closing() {
when(node2.getState()).thenReturn(NodeState.FORCED_DOWN);
ChannelPool pool1 = mockPool(node1);
ChannelPool pool2 = mockPool(node2);
CompletableFuture<ChannelPool> pool2Future = new CompletableFuture<>();
ChannelPool pool3 = mockPool(node3);
MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).pending(node2, KEYSPACE, NodeDistance.LOCAL, pool2Future).build();
CompletionStage<CqlSession> initFuture = newSession();
factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
assertThatStage(initFuture).isSuccess();
Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);
// node2 comes back up, start initializing a pool for it
eventBus.fire(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, node2));
factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
// but the session gets closed before pool init completes
CompletionStage<Void> closeFuture = session.closeAsync();
assertThatStage(closeFuture).isSuccess();
// now pool init completes
pool2Future.complete(pool2);
// Pool should have been closed
verify(pool2, VERIFY_TIMEOUT).forceCloseAsync();
}
use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.
the class ConnectIT method should_wait_for_contact_points_if_reconnection_enabled.
@Test
public void should_wait_for_contact_points_if_reconnection_enabled() throws Exception {
// Given
SIMULACRON_RULE.cluster().rejectConnections(0, RejectScope.STOP);
// When
DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withBoolean(DefaultDriverOption.RECONNECT_ON_INIT, true).withClass(DefaultDriverOption.RECONNECTION_POLICY_CLASS, InitOnlyReconnectionPolicy.class).withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofMillis(500)).build();
CompletableFuture<? extends Session> sessionFuture = newSessionAsync(loader).toCompletableFuture();
// wait a bit to ensure we have a couple of reconnections, otherwise we might race and allow
// reconnections before the initial attempt
TimeUnit.SECONDS.sleep(2);
// Then
assertThat(sessionFuture).isNotCompleted();
// When
SIMULACRON_RULE.cluster().acceptConnections();
// Then this doesn't throw
try (Session session = sessionFuture.get(30, TimeUnit.SECONDS)) {
assertThat(session.getMetadata().getKeyspaces()).containsKey(CqlIdentifier.fromCql("test"));
}
}
use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.
the class SessionLeakIT method should_never_warn_when_session_init_fails.
@Test
public void should_never_warn_when_session_init_fails() {
SIMULACRON_RULE.cluster().prime(PrimeDsl.when("USE \"non_existent_keyspace\"").then(PrimeDsl.invalid("irrelevant")));
int threshold = 4;
// Set the config option explicitly, in case it gets overridden in the test application.conf:
DriverConfigLoader configLoader = DriverConfigLoader.programmaticBuilder().withInt(DefaultDriverOption.SESSION_LEAK_THRESHOLD, threshold).build();
// Go over the threshold, no warnings expected
for (int i = 0; i < threshold + 1; i++) {
try (Session session = SessionUtils.newSession(SIMULACRON_RULE, CqlIdentifier.fromCql("non_existent_keyspace"), configLoader)) {
fail("Session %s should have failed to initialize", session.getName());
} catch (InvalidKeyspaceException e) {
assertThat(e.getMessage()).isEqualTo("Invalid keyspace non_existent_keyspace");
}
}
verify(appender, never()).doAppend(any());
}
use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.
the class DefaultSessionPoolsTest method should_force_close_all_pools_when_force_closing.
@Test
public void should_force_close_all_pools_when_force_closing() {
ChannelPool pool1 = mockPool(node1);
ChannelPool pool2 = mockPool(node2);
ChannelPool pool3 = mockPool(node3);
MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node2, KEYSPACE, NodeDistance.LOCAL, pool2).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).build();
CompletionStage<CqlSession> initFuture = newSession();
factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
assertThatStage(initFuture).isSuccess();
Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
CompletionStage<Void> closeFuture = session.forceCloseAsync();
assertThatStage(closeFuture).isSuccess();
verify(pool1, VERIFY_TIMEOUT).forceCloseAsync();
verify(pool2, VERIFY_TIMEOUT).forceCloseAsync();
verify(pool3, VERIFY_TIMEOUT).forceCloseAsync();
}
use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.
the class DefaultSessionPoolsTest method should_set_keyspace_on_all_pools.
@Test
public void should_set_keyspace_on_all_pools() {
ChannelPool pool1 = mockPool(node1);
ChannelPool pool2 = mockPool(node2);
ChannelPool pool3 = mockPool(node3);
MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node2, KEYSPACE, NodeDistance.LOCAL, pool2).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).build();
CompletionStage<CqlSession> initFuture = newSession();
factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
assertThatStage(initFuture).isSuccess();
Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
CqlIdentifier newKeyspace = CqlIdentifier.fromInternal("newKeyspace");
((DefaultSession) session).setKeyspace(newKeyspace);
verify(pool1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
verify(pool2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
verify(pool3, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
}
Aggregations