<commit_before>package com.datastax.driver.core;

import static java.util.concurrent.TimeUnit.SECONDS;

import org.mockito.Mockito;
import org.testng.annotations.Test;

import com.datastax.driver.core.Host.State;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import com.datastax.driver.core.policies.LimitingLoadBalancingPolicy;
import com.datastax.driver.core.policies.RoundRobinPolicy;

import static com.datastax.driver.core.Assertions.assertThat;

public class PoolingOptionsTest {

    /**
     * Tests {@link PoolingOptions#refreshConnectedHost(Host)} through a custom load balancing policy.
     */
    @Test(groups = "long")
    public void should_refresh_single_connected_host() {
        CCMBridge ccm = null;
        Cluster cluster = null;
        try {
            // This will make the driver use at most 2 hosts, the others will be ignored
            LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1);

            // Setup a 3-host cluster, start only two hosts so that we know in advance which ones the policy will use
            ccm = CCMBridge.create("test");
            ccm.populate(3);
            ccm.start(1);
            ccm.start(2);
            ccm.waitForUp(1);
            ccm.waitForUp(2);

            PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions());
            cluster = Cluster.builder()
                             .addContactPoint(CCMBridge.ipOfNode(1))
                             .withPoolingOptions(poolingOptions)
                             .withLoadBalancingPolicy(loadBalancingPolicy)
                             .withReconnectionPolicy(new ConstantReconnectionPolicy(1000))
                             .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
            assertThat(cluster).host(2)
                               .comesUpWithin(20, SECONDS)
                               .isAtDistance(HostDistance.LOCAL);

            // Bring host 3 up, its presence should be acknowledged but it should be ignored
            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);
            assertThat(cluster).host(3)
                               .hasState(State.ADDED)
                               .isAtDistance(HostDistance.IGNORED);
            assertThat(session).hasNoPoolFor(3);

            // Kill host 2, host 3 should take its place
            ccm.stop(2);
            TestUtils.waitFor(CCMBridge.ipOfNode(3), cluster);

            assertThat(cluster).host(1)
                               .hasState(State.UP)
                               .isAtDistance(HostDistance.LOCAL);
            assertThat(cluster).host(2)<commit_msg>Try again to fix Jenkins timing issue.<commit_after>package com.datastax.driver.core;

import static java.util.concurrent.TimeUnit.SECONDS;

import org.mockito.Mockito;
import org.testng.annotations.Test;

import com.datastax.driver.core.Host.State;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import com.datastax.driver.core.policies.LimitingLoadBalancingPolicy;
import com.datastax.driver.core.policies.RoundRobinPolicy;

import static com.datastax.driver.core.Assertions.assertThat;

public class PoolingOptionsTest {

    /**
     * Tests {@link PoolingOptions#refreshConnectedHost(Host)} through a custom load balancing policy.
     */
    @Test(groups = "long")
    public void should_refresh_single_connected_host() {
        CCMBridge ccm = null;
        Cluster cluster = null;
        try {
            // This will make the driver use at most 2 hosts, the others will be ignored
            LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1);

            // Setup a 3-host cluster, start only two hosts so that we know in advance which ones the policy will use
            ccm = CCMBridge.create("test");
            ccm.populate(3);
            ccm.start(1);
            ccm.start(2);
            ccm.waitForUp(1);
            ccm.waitForUp(2);

            PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions());
            cluster = Cluster.builder()
                             .addContactPoint(CCMBridge.ipOfNode(1))
                             .withPoolingOptions(poolingOptions)
                             .withLoadBalancingPolicy(loadBalancingPolicy)
                             .withReconnectionPolicy(new ConstantReconnectionPolicy(1000))
                             .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
            assertThat(cluster).host(2)
                               .comesUpWithin(120, SECONDS)
                               .isAtDistance(HostDistance.LOCAL);

            // Bring host 3 up, its presence should be acknowledged but it should be ignored
            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);
            assertThat(cluster).host(3)
                               .hasState(State.ADDED)
                               .isAtDistance(HostDistance.IGNORED);
            assertThat(session).hasNoPoolFor(3);

            // Kill host 2, host 3 should take its place
            ccm.stop(2);
            TestUtils.waitFor(CCMBridge.ipOfNode(3), cluster);

            assertThat(cluster).host(1)
                               .hasState(State.UP)
                               .isAtDistance(HostDistance.LOCAL);
            assertThat(cluster).host(2)
{'fraction_non_alphanumeric': 0.0857185450208706, 'fraction_numerical': 0.009689922480620155, 'mean_word_length': 1.7586348684210527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2145769', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1703, 'n_words': 361}