Search in sources :

Example 1 with DriverChannel

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

the class ControlConnectionTestBase method newMockDriverChannel.

protected DriverChannel newMockDriverChannel(int id) {
    DriverChannel driverChannel = mock(DriverChannel.class);
    Channel channel = mock(Channel.class);
    EventLoop adminExecutor = adminEventLoopGroup.next();
    DefaultChannelPromise closeFuture = new DefaultChannelPromise(channel, adminExecutor);
    when(driverChannel.close()).thenAnswer(i -> {
        closeFuture.trySuccess(null);
        return closeFuture;
    });
    when(driverChannel.forceClose()).thenAnswer(i -> {
        closeFuture.trySuccess(null);
        return closeFuture;
    });
    when(driverChannel.closeFuture()).thenReturn(closeFuture);
    when(driverChannel.toString()).thenReturn("channel" + id);
    when(driverChannel.getEndPoint()).thenReturn(new DefaultEndPoint(new InetSocketAddress("127.0.0." + id, 9042)));
    return driverChannel;
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) EventLoop(io.netty.channel.EventLoop) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DefaultEndPoint(com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint)

Example 2 with DriverChannel

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

the class ControlConnectionTestBase method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    adminEventLoopGroup = new DefaultEventLoopGroup(1);
    when(context.getNettyOptions()).thenReturn(nettyOptions);
    when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventLoopGroup);
    eventBus = spy(new EventBus("test"));
    when(context.getEventBus()).thenReturn(eventBus);
    when(context.getChannelFactory()).thenReturn(channelFactory);
    channelFactoryFuture = new Exchanger<>();
    when(channelFactory.connect(any(Node.class), any(DriverChannelOptions.class))).thenAnswer(invocation -> {
        CompletableFuture<DriverChannel> channelFuture = new CompletableFuture<>();
        channelFactoryFuture.exchange(channelFuture, 100, TimeUnit.MILLISECONDS);
        return channelFuture;
    });
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(defaultProfile);
    when(defaultProfile.getBoolean(DefaultDriverOption.RECONNECT_ON_INIT)).thenReturn(false);
    when(context.getReconnectionPolicy()).thenReturn(reconnectionPolicy);
    // Child classes only cover "runtime" reconnections when the driver is already initialized
    when(reconnectionPolicy.newControlConnectionSchedule(false)).thenReturn(reconnectionSchedule);
    // By default, set a large reconnection delay. Tests that care about reconnection will override
    // it.
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofDays(1));
    when(context.getLoadBalancingPolicyWrapper()).thenReturn(loadBalancingPolicyWrapper);
    when(context.getMetricsFactory()).thenReturn(metricsFactory);
    node1 = TestNodeFactory.newNode(1, context);
    node2 = TestNodeFactory.newNode(2, context);
    mockQueryPlan(node1, node2);
    when(metadataManager.refreshNodes()).thenReturn(CompletableFuture.completedFuture(null));
    when(metadataManager.refreshSchema(anyString(), anyBoolean(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
    when(context.getMetadataManager()).thenReturn(metadataManager);
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(defaultProfile);
    when(defaultProfile.getBoolean(DefaultDriverOption.CONNECTION_WARN_INIT_ERROR)).thenReturn(false);
    controlConnection = new ControlConnection(context);
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) DriverChannelOptions(com.datastax.oss.driver.internal.core.channel.DriverChannelOptions) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Before(org.junit.Before)

Example 3 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel 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 4 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel 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 5 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel 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)

Aggregations

DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)54 Test (org.junit.Test)35 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)30 CompletableFuture (java.util.concurrent.CompletableFuture)17 InOrder (org.mockito.InOrder)13 Node (com.datastax.oss.driver.api.core.metadata.Node)9 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)6 DriverChannelOptions (com.datastax.oss.driver.internal.core.channel.DriverChannelOptions)6 ChannelPromise (io.netty.channel.ChannelPromise)5 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 InetSocketAddress (java.net.InetSocketAddress)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)3 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)3 EventCallback (com.datastax.oss.driver.internal.core.channel.EventCallback)3 Before (org.junit.Before)3 Version (com.datastax.oss.driver.api.core.Version)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2