Search in sources :

Example 21 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_event.

private void should_reconnect_if_event(NodeStateEvent event) {
    // 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(event);
    // 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) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)

Example 22 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_event_during_reconnection_attempt.

private void should_reconnect_if_event_during_reconnection_attempt(NodeStateEvent event) {
    // 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();
    await().untilAsserted(() -> 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 goes into the new state
    eventBus.fire(event);
    // 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) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)

Example 23 with MockChannelFactoryHelper

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

the class ChannelPoolKeyspaceTest method should_switch_keyspace_on_pending_channels.

@Test
public void should_switch_keyspace_on_pending_channels() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    CompletableFuture<DriverChannel> channel1Future = new CompletableFuture<>();
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, "mock channel init failure").failure(node, "mock channel init failure").pending(node, channel1Future).pending(node, channel2Future).build();
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    // Check that reconnection has kicked in, but do not complete it yet
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCalls(node, 2);
    // Switch keyspace, it succeeds immediately since there is no active channel
    CqlIdentifier newKeyspace = CqlIdentifier.fromCql("new_keyspace");
    CompletionStage<Void> setKeyspaceFuture = pool.setKeyspace(newKeyspace);
    assertThatStage(setKeyspaceFuture).isSuccess();
    // Now let the two channels succeed to complete the reconnection
    channel1Future.complete(channel1);
    channel2Future.complete(channel2);
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    verify(channel1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(channel2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    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) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 24 with MockChannelFactoryHelper

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

the class ChannelPoolKeyspaceTest method should_switch_keyspace_on_existing_channels.

@Test
public void should_switch_keyspace_on_existing_channels() throws Exception {
    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();
    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);
    CqlIdentifier newKeyspace = CqlIdentifier.fromCql("new_keyspace");
    CompletionStage<Void> setKeyspaceFuture = pool.setKeyspace(newKeyspace);
    verify(channel1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(channel2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    assertThatStage(setKeyspaceFuture).isSuccess();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 25 with MockChannelFactoryHelper

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

the class ChannelPoolResizeTest method should_shrink_during_reconnection.

@Test
public void should_shrink_during_reconnection() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    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);
    CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
    DriverChannel channel4 = newMockDriverChannel(4);
    CompletableFuture<DriverChannel> channel4Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).failure(node, "mock channel init failure").failure(node, "mock channel init failure").pending(node, channel3Future).pending(node, channel4Future).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.REMOTE, context, "test");
    factoryHelper.waitForCalls(node, 4);
    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);
    // A reconnection should have been scheduled to add the missing channels, don't complete yet
    verify(reconnectionSchedule).nextDelay();
    inOrder.verify(eventBus).fire(ChannelEvent.reconnectionStarted(node));
    pool.resize(NodeDistance.LOCAL);
    TimeUnit.MILLISECONDS.sleep(200);
    // Now allow the reconnected channels to complete initialization
    channel3Future.complete(channel3);
    channel4Future.complete(channel4);
    factoryHelper.waitForCalls(node, 2);
    // Pool should have shrunk back to 2. We keep the most recent channels so 1 and 2 get closed.
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelClosed(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(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)

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