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;
}
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);
}
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();
}
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();
}
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();
}
Aggregations