use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.
the class ChannelPoolResizeTest method should_resize_outside_of_reconnection_if_config_changes.
@Test
public void should_resize_outside_of_reconnection_if_config_changes() 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);
DriverChannel channel4 = newMockDriverChannel(4);
MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).success(node, channel4).build();
InOrder inOrder = inOrder(eventBus);
CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
factoryHelper.waitForCalls(node, 2);
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);
// Simulate a configuration change
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(4);
eventBus.fire(ConfigChangeEvent.INSTANCE);
// It should have triggered a reconnection
verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
factoryHelper.waitForCalls(node, 2);
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4));
factoryHelper.verifyNoMoreCalls();
}
use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.
the class ChannelPoolShutdownTest method should_close_all_channels_when_closed.
@Test
public void should_close_all_channels_when_closed() throws Exception {
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
DriverChannel channel1 = newMockDriverChannel(1);
DriverChannel channel2 = newMockDriverChannel(2);
DriverChannel channel3 = newMockDriverChannel(3);
DriverChannel channel4 = newMockDriverChannel(4);
CompletableFuture<DriverChannel> channel4Future = new CompletableFuture<>();
MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).pending(node, channel4Future).build();
InOrder inOrder = inOrder(eventBus);
CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
factoryHelper.waitForCalls(node, 3);
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(3)).fire(ChannelEvent.channelOpened(node));
assertThatStage(poolFuture).isSuccess();
ChannelPool pool = poolFuture.toCompletableFuture().get();
// Simulate graceful shutdown on channel3
((ChannelPromise) channel3.closeStartedFuture()).setSuccess();
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(1)).fire(ChannelEvent.channelClosed(node));
// Reconnection should have kicked in and started to open channel4, do not complete it yet
verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
factoryHelper.waitForCalls(node, 1);
CompletionStage<Void> closeFuture = pool.closeAsync();
// The two original channels were closed normally
verify(channel1, VERIFY_TIMEOUT).close();
verify(channel2, VERIFY_TIMEOUT).close();
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelClosed(node));
// The closing channel was not closed again
verify(channel3, never()).close();
// Complete the reconnecting channel
channel4Future.complete(channel4);
// It should be force-closed once we find out the pool was closed
verify(channel4, VERIFY_TIMEOUT).forceClose();
// No events because the channel was never really associated to the pool
inOrder.verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
inOrder.verify(eventBus, never()).fire(ChannelEvent.channelClosed(node));
// We don't wait for reconnected channels to close, so the pool only depends on channel 1 to 3
((ChannelPromise) channel1.closeFuture()).setSuccess();
((ChannelPromise) channel2.closeFuture()).setSuccess();
((ChannelPromise) channel3.closeFuture()).setSuccess();
assertThatStage(closeFuture).isSuccess();
factoryHelper.verifyNoMoreCalls();
}
use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.
the class ChannelPoolShutdownTest method should_force_close_all_channels_when_force_closed.
@Test
public void should_force_close_all_channels_when_force_closed() throws Exception {
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(3);
DriverChannel channel1 = newMockDriverChannel(1);
DriverChannel channel2 = newMockDriverChannel(2);
DriverChannel channel3 = newMockDriverChannel(3);
DriverChannel channel4 = newMockDriverChannel(4);
CompletableFuture<DriverChannel> channel4Future = new CompletableFuture<>();
MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).pending(node, channel4Future).build();
InOrder inOrder = inOrder(eventBus);
CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
factoryHelper.waitForCalls(node, 3);
assertThatStage(poolFuture).isSuccess();
ChannelPool pool = poolFuture.toCompletableFuture().get();
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(3)).fire(ChannelEvent.channelOpened(node));
// Simulate graceful shutdown on channel3
((ChannelPromise) channel3.closeStartedFuture()).setSuccess();
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(1)).fire(ChannelEvent.channelClosed(node));
// Reconnection should have kicked in and started to open a channel, do not complete it yet
verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
factoryHelper.waitForCalls(node, 1);
CompletionStage<Void> closeFuture = pool.forceCloseAsync();
// The three original channels were force-closed
verify(channel1, VERIFY_TIMEOUT).forceClose();
verify(channel2, VERIFY_TIMEOUT).forceClose();
verify(channel3, VERIFY_TIMEOUT).forceClose();
// Only two events because the one for channel3 was sent earlier
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelClosed(node));
// Complete the reconnecting channel
channel4Future.complete(channel4);
// It should be force-closed once we find out the pool was closed
verify(channel4, VERIFY_TIMEOUT).forceClose();
// No events because the channel was never really associated to the pool
inOrder.verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
inOrder.verify(eventBus, never()).fire(ChannelEvent.channelClosed(node));
// We don't wait for reconnected channels to close, so the pool only depends on channel 1-3
((ChannelPromise) channel1.closeFuture()).setSuccess();
((ChannelPromise) channel2.closeFuture()).setSuccess();
((ChannelPromise) channel3.closeFuture()).setSuccess();
assertThatStage(closeFuture).isSuccess();
factoryHelper.verifyNoMoreCalls();
}
use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.
the class ChannelPoolTestBase method newMockDriverChannel.
DriverChannel newMockDriverChannel(int id) {
DriverChannel driverChannel = mock(DriverChannel.class);
EventLoop adminExecutor = adminEventLoopGroup.next();
Channel channel = mock(Channel.class);
DefaultChannelPromise closeFuture = new DefaultChannelPromise(channel, adminExecutor);
DefaultChannelPromise closeStartedFuture = new DefaultChannelPromise(channel, adminExecutor);
when(driverChannel.close()).thenReturn(closeFuture);
when(driverChannel.forceClose()).thenReturn(closeFuture);
when(driverChannel.closeFuture()).thenReturn(closeFuture);
when(driverChannel.closeStartedFuture()).thenReturn(closeStartedFuture);
when(driverChannel.setKeyspace(any(CqlIdentifier.class))).thenReturn(adminExecutor.newSucceededFuture(null));
when(driverChannel.toString()).thenReturn("channel" + id);
return driverChannel;
}
use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.
the class ChannelPoolInitTest method should_reconnect_when_init_incomplete.
@Test
public void should_reconnect_when_init_incomplete() throws Exception {
// Short delay so we don't have to wait in the test
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);
CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).failure(node, "mock channel init failure").success(node, channel1).pending(node, channel2Future).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);
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
// A reconnection should have been scheduled
verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
channel2Future.complete(channel2);
factoryHelper.waitForCalls(node, 1);
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2));
verify(nodeMetricUpdater, VERIFY_TIMEOUT).incrementCounter(DefaultNodeMetric.CONNECTION_INIT_ERRORS, null);
factoryHelper.verifyNoMoreCalls();
}
Aggregations