|
| 1 | +package com.datastax.oss.driver.internal.core.pool; |
| 2 | + |
| 3 | +import static com.datastax.oss.driver.Assertions.assertThatStage; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 7 | +import static org.mockito.ArgumentMatchers.eq; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.timeout; |
| 10 | +import static org.mockito.Mockito.verify; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import com.datastax.oss.driver.api.core.config.DefaultDriverOption; |
| 14 | +import com.datastax.oss.driver.api.core.loadbalancing.NodeDistance; |
| 15 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 16 | +import com.datastax.oss.driver.internal.core.channel.ChannelEvent; |
| 17 | +import com.datastax.oss.driver.internal.core.channel.DriverChannel; |
| 18 | +import com.datastax.oss.driver.internal.core.channel.DriverChannelOptions; |
| 19 | +import com.datastax.oss.driver.internal.core.protocol.ShardingInfo; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.CompletionStage; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.mockito.InOrder; |
| 26 | +import org.mockito.Mockito; |
| 27 | + |
| 28 | +public class ChannelPoolShardAwareInitTest extends ChannelPoolTestBase { |
| 29 | + |
| 30 | + @Before |
| 31 | + @Override |
| 32 | + public void setup() { |
| 33 | + super.setup(); |
| 34 | + when(defaultProfile.getBoolean(DefaultDriverOption.CONNECTION_ADVANCED_SHARD_AWARENESS_ENABLED)) |
| 35 | + .thenReturn(true); |
| 36 | + when(defaultProfile.getInt(DefaultDriverOption.ADVANCED_SHARD_AWARENESS_PORT_LOW)) |
| 37 | + .thenReturn(10000); |
| 38 | + when(defaultProfile.getInt(DefaultDriverOption.ADVANCED_SHARD_AWARENESS_PORT_HIGH)) |
| 39 | + .thenReturn(60000); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void should_initialize_when_all_channels_succeed() throws Exception { |
| 44 | + when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(4); |
| 45 | + int shardsPerNode = 2; |
| 46 | + DriverChannel channel1 = newMockDriverChannel(1); |
| 47 | + DriverChannel channel2 = newMockDriverChannel(2); |
| 48 | + DriverChannel channel3 = newMockDriverChannel(3); |
| 49 | + DriverChannel channel4 = newMockDriverChannel(4); |
| 50 | + |
| 51 | + ShardingInfo shardingInfo = mock(ShardingInfo.class); |
| 52 | + when(shardingInfo.getShardsCount()).thenReturn(shardsPerNode); |
| 53 | + node.setShardingInfo(shardingInfo); |
| 54 | + |
| 55 | + when(channel1.getShardingInfo()).thenReturn(shardingInfo); |
| 56 | + when(channel2.getShardingInfo()).thenReturn(shardingInfo); |
| 57 | + when(channel3.getShardingInfo()).thenReturn(shardingInfo); |
| 58 | + when(channel4.getShardingInfo()).thenReturn(shardingInfo); |
| 59 | + |
| 60 | + when(channel1.getShardId()).thenReturn(0); |
| 61 | + when(channel2.getShardId()).thenReturn(0); |
| 62 | + when(channel3.getShardId()).thenReturn(1); |
| 63 | + when(channel4.getShardId()).thenReturn(1); |
| 64 | + |
| 65 | + when(channelFactory.connect(eq(node), any(DriverChannelOptions.class))) |
| 66 | + .thenReturn(CompletableFuture.completedFuture(channel1)); |
| 67 | + when(channelFactory.connect(eq(node), eq(0), any(DriverChannelOptions.class))) |
| 68 | + .thenReturn(CompletableFuture.completedFuture(channel2)); |
| 69 | + when(channelFactory.connect(eq(node), eq(1), any(DriverChannelOptions.class))) |
| 70 | + .thenReturn(CompletableFuture.completedFuture(channel3)) |
| 71 | + .thenReturn(CompletableFuture.completedFuture(channel4)); |
| 72 | + |
| 73 | + CompletionStage<ChannelPool> poolFuture = |
| 74 | + ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test"); |
| 75 | + |
| 76 | + ArgumentCaptor<DriverChannelOptions> optionsCaptor = |
| 77 | + ArgumentCaptor.forClass(DriverChannelOptions.class); |
| 78 | + InOrder inOrder = Mockito.inOrder(channelFactory); |
| 79 | + inOrder |
| 80 | + .verify(channelFactory, timeout(500).atLeast(1)) |
| 81 | + .connect(eq(node), optionsCaptor.capture()); |
| 82 | + int num = optionsCaptor.getAllValues().size(); |
| 83 | + assertThat(num).isEqualTo(1); |
| 84 | + inOrder |
| 85 | + .verify(channelFactory, timeout(500).atLeast(3)) |
| 86 | + .connect(eq(node), anyInt(), optionsCaptor.capture()); |
| 87 | + int num2 = optionsCaptor.getAllValues().size(); |
| 88 | + assertThat(num2).isEqualTo(4); |
| 89 | + |
| 90 | + assertThatStage(poolFuture) |
| 91 | + .isSuccess( |
| 92 | + pool -> { |
| 93 | + assertThat(pool.channels[0]).containsOnly(channel1, channel2); |
| 94 | + assertThat(pool.channels[1]).containsOnly(channel3, channel4); |
| 95 | + }); |
| 96 | + verify(eventBus, VERIFY_TIMEOUT.times(4)).fire(ChannelEvent.channelOpened(node)); |
| 97 | + |
| 98 | + inOrder |
| 99 | + .verify(channelFactory, timeout(500).times(0)) |
| 100 | + .connect(any(Node.class), any(DriverChannelOptions.class)); |
| 101 | + inOrder |
| 102 | + .verify(channelFactory, timeout(500).times(0)) |
| 103 | + .connect(any(Node.class), anyInt(), any(DriverChannelOptions.class)); |
| 104 | + } |
| 105 | +} |
0 commit comments