Search in sources :

Example 1 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_second_contact_point_if_first_one_fails.

@Test
public void should_init_with_second_contact_point_if_first_one_fails() {
    // Given
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node1, "mock failure").success(node2, channel2).build();
    // When
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    factoryHelper.waitForCall(node2);
    // Then
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel2));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.controlConnectionFailed(node1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node2));
    // each attempt tries all nodes, so there is no reconnection
    verify(reconnectionPolicy, never()).newNodeSchedule(any(Node.class));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) Node(com.datastax.oss.driver.api.core.metadata.Node) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 2 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_reconnect_if_node_becomes_ignored.

@Test
public void should_reconnect_if_node_becomes_ignored() {
    // Given
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).success(node2, channel2).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    // When
    mockQueryPlan(node2);
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node1));
    // Then
    // an immediate reconnection was started
    factoryHelper.waitForCall(node2);
    await().untilAsserted(() -> assertThat(controlConnection.channel()).isEqualTo(channel2));
    verify(reconnectionSchedule, never()).nextDelay();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node2));
    verify(metadataManager, VERIFY_TIMEOUT).refreshNodes();
    verify(loadBalancingPolicyWrapper, VERIFY_TIMEOUT).init();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 3 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_not_force_reconnection_if_closed.

@Test
public void should_not_force_reconnection_if_closed() throws InterruptedException {
    // 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();
    CompletionStage<Void> closeFuture = controlConnection.forceCloseAsync();
    assertThatStage(closeFuture).isSuccess();
    // When
    controlConnection.reconnectNow();
    TimeUnit.MILLISECONDS.sleep(500);
    // Then
    verify(reconnectionSchedule, never()).nextDelay();
    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 4 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_handle_channel_failure_if_closed_during_reconnection.

@Test
public void should_handle_channel_failure_if_closed_during_reconnection() {
    // Given
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel1Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).pending(node1, channel1Future).success(node2, channel2).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    // the channel fails and a reconnection is scheduled
    channel1.close();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    // channel1 starts initializing (but the future is not completed yet)
    factoryHelper.waitForCall(node1);
    // When
    // the control connection gets closed before channel1 initialization fails
    CompletionStage<Void> closeFuture = controlConnection.forceCloseAsync();
    assertThatStage(closeFuture).isSuccess();
    channel1Future.completeExceptionally(new Exception("mock failure"));
    // Then
    // should never try channel2 because the reconnection has detected that it can stop after the
    // first failure
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 5 with MockChannelFactoryHelper

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

the class ControlConnectionTest method should_reconnect_if_node_became_ignored_during_reconnection_attempt.

@Test
public void should_reconnect_if_node_became_ignored_during_reconnection_attempt() {
    // Given
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    DriverChannel channel3 = newMockDriverChannel(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).pending(node2, channel2Future).success(node1, channel3).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    mockQueryPlan(node2, node1);
    // channel1 goes down, triggering a reconnection
    channel1.close();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    // the reconnection to node2 is in progress
    factoryHelper.waitForCall(node2);
    // When
    // node2 becomes ignored
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
    // the reconnection to node2 completes
    channel2Future.complete(channel2);
    // Then
    // The channel should get closed and we should try the next node
    verify(channel2, VERIFY_TIMEOUT).forceClose();
    factoryHelper.waitForCall(node1);
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) 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