Search in sources :

Example 11 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_adjust_distance_if_changed_while_recreating.

@Test
public void should_adjust_distance_if_changed_while_recreating() {
    when(node2.getDistance()).thenReturn(NodeDistance.IGNORED);
    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);
    eventBus.fire(new DistanceEvent(NodeDistance.LOCAL, node2));
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    // Distance changes again while pool init is in progress
    eventBus.fire(new DistanceEvent(NodeDistance.REMOTE, node2));
    // Now pool init succeeds
    pool2Future.complete(pool2);
    // Pool should have been adjusted
    verify(pool2, VERIFY_TIMEOUT).resize(NodeDistance.REMOTE);
    await().untilAsserted(() -> assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool2, pool3));
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Aggregations

DistanceEvent (com.datastax.oss.driver.internal.core.metadata.DistanceEvent)11 Test (org.junit.Test)11 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)9 Session (com.datastax.oss.driver.api.core.session.Session)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)2 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)2