use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper 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();
}
use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper in project java-driver by datastax.
the class ChannelPoolReconnectTest method should_reconnect_when_channel_closes.
@Test
public void should_reconnect_when_channel_closes() 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);
CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).pending(node, channel3Future).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, channel2);
inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
// Simulate fatal error on channel2
((ChannelPromise) channel2.closeFuture()).setFailure(new Exception("mock channel init failure"));
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node));
verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
factoryHelper.waitForCall(node);
channel3Future.complete(channel3);
inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel3));
factoryHelper.verifyNoMoreCalls();
}
use of com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper 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.MockChannelFactoryHelper 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();
}
Aggregations