Search in sources :

Example 1 with LimitingLoadBalancingPolicy

use of com.datastax.driver.core.policies.LimitingLoadBalancingPolicy in project java-driver by datastax.

the class RefreshConnectedHostTest method should_refresh_single_connected_host.

/**
 * Tests {@link PoolingOptions#refreshConnectedHost(Host)} through a custom load balancing policy.
 */
@Test(groups = "long")
public void should_refresh_single_connected_host() {
    // This will make the driver use at most 2 hosts, the others will be ignored
    LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1);
    PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions());
    Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)).withPort(ccm().getBinaryPort()).withPoolingOptions(poolingOptions).withLoadBalancingPolicy(loadBalancingPolicy).withReconnectionPolicy(new ConstantReconnectionPolicy(1000)).withQueryOptions(TestUtils.nonDebouncingQueryOptions()).build());
    Session session = cluster.connect();
    assertThat(cluster).usesControlHost(1);
    assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
    // Wait for the node to be up, because apparently on Jenkins it's still only ADDED when we reach this line
    // Waiting for NEW_NODE_DELAY_SECONDS+1 allows the driver to create a connection pool and mark the node up
    assertThat(cluster).host(2).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS + 1, SECONDS).isAtDistance(HostDistance.LOCAL);
    // Add and bring host 3 up, its presence should be acknowledged but it should be ignored
    ccm().add(3);
    ccm().start(3);
    ccm().waitForUp(3);
    assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
    assertThat(cluster).host(2).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
    // Ensure that the host is added to the Cluster.
    assertThat(cluster).host(3).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS + 1, SECONDS).isAtDistance(HostDistance.IGNORED);
    assertThat(session).hasNoPoolFor(3);
    // Kill host 2, host 3 should take its place
    ccm().stop(2);
    TestUtils.waitForUp(TestUtils.ipOfNode(3), cluster);
    assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
    assertThat(cluster).host(2).hasState(State.DOWN);
    assertThat(cluster).host(3).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
    assertThat(session).hasPoolFor(3);
    // This is when refreshConnectedHost should have been invoked, it triggers pool creation when
    // we switch the node from IGNORED to UP:
    Mockito.verify(poolingOptions).refreshConnectedHost(TestUtils.findHost(cluster, 3));
}
Also used : LimitingLoadBalancingPolicy(com.datastax.driver.core.policies.LimitingLoadBalancingPolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.testng.annotations.Test)

Aggregations

ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)1 LimitingLoadBalancingPolicy (com.datastax.driver.core.policies.LimitingLoadBalancingPolicy)1 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)1 Test (org.testng.annotations.Test)1