Search in sources :

Example 11 with MockChannelFactoryHelper

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

the class ChannelPoolResizeTest method should_shrink_outside_of_reconnection.

@Test
public void should_shrink_outside_of_reconnection() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE)).thenReturn(4);
    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.REMOTE, context, "test");
    factoryHelper.waitForCalls(node, 4);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(4)).fire(ChannelEvent.channelOpened(node));
    pool.resize(NodeDistance.LOCAL);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelClosed(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(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 12 with MockChannelFactoryHelper

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

the class ChannelPoolResizeTest method should_resize_during_reconnection_if_config_changes.

@Test
public void should_resize_during_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);
    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));
    // Simulate a configuration change
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(4);
    eventBus.fire(ConfigChangeEvent.INSTANCE);
    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 13 with MockChannelFactoryHelper

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

the class ChannelPoolResizeTest method should_grow_outside_of_reconnection.

@Test
public void should_grow_outside_of_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);
    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);
    pool.resize(NodeDistance.REMOTE);
    // The resizing 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 14 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_init_with_first_contact_point_if_reachable.

@Test
public void should_init_with_first_contact_point_if_reachable() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).build();
    // When
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    // Then
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 15 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_close_channel_when_closing.

@Test
public void should_close_channel_when_closing() {
    // Given
    DriverChannel channel1 = newMockDriverChannel(1);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess();
    // When
    CompletionStage<Void> closeFuture = controlConnection.forceCloseAsync();
    // Then
    assertThatStage(closeFuture).isSuccess();
    verify(channel1, VERIFY_TIMEOUT).forceClose();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) 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