Search in sources :

Example 26 with MockChannelFactoryHelper

use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.

the class ChannelPoolResizeTest method should_resize_outside_of_reconnection_if_config_changes.

@Test
public void should_resize_outside_of_reconnection_if_config_changes() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    DriverChannel channel4 = newMockDriverChannel(4);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).success(node, channel4).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    // Simulate a configuration change
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(4);
    eventBus.fire(ConfigChangeEvent.INSTANCE);
    // It should have triggered a reconnection
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 27 with MockChannelFactoryHelper

use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.

the class ChannelPoolResizeTest method should_grow_during_reconnection.

@Test
public void should_grow_during_reconnection() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE)).thenReturn(4);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    DriverChannel channel3 = newMockDriverChannel(3);
    CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
    DriverChannel channel4 = newMockDriverChannel(4);
    CompletableFuture<DriverChannel> channel4Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).failure(node, "mock channel init failure").pending(node, channel2Future).pending(node, channel3Future).pending(node, channel4Future).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1);
    // A reconnection should have been scheduled to add the missing channel, don't complete yet
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    pool.resize(NodeDistance.REMOTE);
    TimeUnit.MILLISECONDS.sleep(200);
    // Complete the channel for the first reconnection, bringing the count to 2
    channel2Future.complete(channel2);
    factoryHelper.waitForCall(node);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2));
    // A second attempt should have been scheduled since we're now still under the target size
    verify(reconnectionSchedule, VERIFY_TIMEOUT.times(2)).nextDelay();
    // Same reconnection is still running, no additional events
    inOrder.verify(eventBus, never()).fire(ChannelEvent.reconnectionStopped(node));
    inOrder.verify(eventBus, never()).fire(ChannelEvent.reconnectionStarted(node));
    // Two more channels get opened, bringing us to the target count
    factoryHelper.waitForCalls(node, 2);
    channel3Future.complete(channel3);
    channel4Future.complete(channel4);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 28 with MockChannelFactoryHelper

use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.

the class ChannelPoolResizeTest method should_ignore_config_change_if_not_relevant.

@Test
public void should_ignore_config_change_if_not_relevant() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    // Config changes, but not for our distance
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE)).thenReturn(1);
    eventBus.fire(ConfigChangeEvent.INSTANCE);
    TimeUnit.MILLISECONDS.sleep(200);
    // It should not have triggered a reconnection
    verify(reconnectionSchedule, never()).nextDelay();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 29 with MockChannelFactoryHelper

use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.

the class ChannelPoolInitTest method should_fire_force_down_event_when_cluster_name_does_not_match.

@Test
public void should_fire_force_down_event_when_cluster_name_does_not_match() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
    ClusterNameMismatchException error = new ClusterNameMismatchException(node.getEndPoint(), "actual", "expected");
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, error).failure(node, error).failure(node, error).build();
    ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 3);
    verify(eventBus, VERIFY_TIMEOUT).fire(TopologyEvent.forceDown(node.getBroadcastRpcAddress().get()));
    verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
    verify(nodeMetricUpdater, VERIFY_TIMEOUT.times(3)).incrementCounter(DefaultNodeMetric.CONNECTION_INIT_ERRORS, null);
    factoryHelper.verifyNoMoreCalls();
}
Also used : MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) ClusterNameMismatchException(com.datastax.oss.driver.internal.core.channel.ClusterNameMismatchException) Test(org.junit.Test)

Example 30 with MockChannelFactoryHelper

use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.

the class ChannelPoolInitTest method should_initialize_when_all_channels_fail.

@Test
public void should_initialize_when_all_channels_fail() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, "mock channel init failure").failure(node, "mock channel init failure").failure(node, "mock channel init failure").build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 3);
    assertThatStage(poolFuture).isSuccess(pool -> assertThat(pool.channels).isEmpty());
    verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
    verify(nodeMetricUpdater, VERIFY_TIMEOUT.times(3)).incrementCounter(DefaultNodeMetric.CONNECTION_INIT_ERRORS, null);
    factoryHelper.verifyNoMoreCalls();
}
Also used : MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Aggregations

MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)34 Test (org.junit.Test)32 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)30 CompletableFuture (java.util.concurrent.CompletableFuture)14 InOrder (org.mockito.InOrder)13 ChannelPromise (io.netty.channel.ChannelPromise)5 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 Node (com.datastax.oss.driver.api.core.metadata.Node)2 DistanceEvent (com.datastax.oss.driver.internal.core.metadata.DistanceEvent)2 InvalidKeyspaceException (com.datastax.oss.driver.api.core.InvalidKeyspaceException)1 ClusterNameMismatchException (com.datastax.oss.driver.internal.core.channel.ClusterNameMismatchException)1 ExecutionException (java.util.concurrent.ExecutionException)1