Search in sources :

Example 6 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_fail_to_init_if_all_contact_points_fail.

@Test
public void should_fail_to_init_if_all_contact_points_fail() {
    // Given
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node1, "mock failure").failure(node2, "mock failure").build();
    // When
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    factoryHelper.waitForCall(node2);
    // Then
    assertThatStage(initFuture).isFailed();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.controlConnectionFailed(node1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.controlConnectionFailed(node2));
    // no reconnections at init
    verify(reconnectionPolicy, never()).newNodeSchedule(any(Node.class));
    factoryHelper.verifyNoMoreCalls();
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 7 with MockChannelFactoryHelper

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

the class ChannelPoolInitTest method should_indicate_when_keyspace_failed_on_all_channels.

@Test
public void should_indicate_when_keyspace_failed_on_all_channels() {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, new InvalidKeyspaceException("invalid keyspace")).failure(node, new InvalidKeyspaceException("invalid keyspace")).failure(node, new InvalidKeyspaceException("invalid keyspace")).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 3);
    assertThatStage(poolFuture).isSuccess(pool -> {
        assertThat(pool.isInvalidKeyspace()).isTrue();
        verify(nodeMetricUpdater, VERIFY_TIMEOUT.times(3)).incrementCounter(DefaultNodeMetric.CONNECTION_INIT_ERRORS, null);
    });
}
Also used : InvalidKeyspaceException(com.datastax.oss.driver.api.core.InvalidKeyspaceException) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 8 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_succeed.

@Test
public void should_initialize_when_all_channels_succeed() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 3);
    assertThatStage(poolFuture).isSuccess(pool -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3));
    verify(eventBus, VERIFY_TIMEOUT.times(3)).fire(ChannelEvent.channelOpened(node));
    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 9 with MockChannelFactoryHelper

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

the class ChannelPoolReconnectTest method should_reconnect_when_channel_starts_graceful_shutdown.

@Test
public void should_reconnect_when_channel_starts_graceful_shutdown() 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);
    CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).pending(node, channel3Future).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    // Simulate graceful shutdown on channel2
    ((ChannelPromise) channel2.closeStartedFuture()).setSuccess();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCall(node);
    channel3Future.complete(channel3);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel3));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ChannelPromise(io.netty.channel.ChannelPromise) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 10 with MockChannelFactoryHelper

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

the class ChannelPoolReconnectTest method should_let_current_attempt_complete_when_reconnecting_now.

@Test
public void should_let_current_attempt_complete_when_reconnecting_now() throws ExecutionException, InterruptedException {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(1);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).pending(node, channel2Future).build();
    InOrder inOrder = inOrder(eventBus);
    // Initial connection
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 1);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(1)).fire(ChannelEvent.channelOpened(node));
    // Kill channel1, reconnection begins and starts initializing channel2, but the initialization
    // is still pending (channel2Future not completed)
    ((ChannelPromise) channel1.closeStartedFuture()).setSuccess();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    factoryHelper.waitForCalls(node, 1);
    // Force a reconnection, should not try to create a new channel since we have a pending one
    pool.reconnectNow();
    TimeUnit.MILLISECONDS.sleep(200);
    factoryHelper.verifyNoMoreCalls();
    inOrder.verify(eventBus, never()).fire(any());
    // Complete the initialization of channel2, reconnection succeeds
    channel2Future.complete(channel2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel2));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ChannelPromise(io.netty.channel.ChannelPromise) 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