private TransportChannel createChannel() throws IOException { ManagedChannel outerChannel; if (poolSize == null || poolSize == 1) { outerChannel = createSingleChannel(); } else { ImmutableList.Builder<ManagedChannel> channels = ImmutableList.builder(); for (int i = 0; i < poolSize; i++) { channels.add(createSingleChannel()); } outerChannel = new ChannelPool(channels.build()); } return GrpcTransportChannel.create(outerChannel); }
private TransportChannel createChannel() throws IOException { ManagedChannel outerChannel; if (poolSize == null || poolSize == 1) { outerChannel = createSingleChannel(); } else { ImmutableList.Builder<ManagedChannel> channels = ImmutableList.builder(); for (int i = 0; i < poolSize; i++) { channels.add(createSingleChannel()); } outerChannel = new ChannelPool(channels.build()); } return GrpcTransportChannel.create(outerChannel); }
@Test public void testAuthority() { ManagedChannel sub1 = Mockito.mock(ManagedChannel.class); ManagedChannel sub2 = Mockito.mock(ManagedChannel.class); Mockito.when(sub1.authority()).thenReturn("myAuth"); ChannelPool pool = new ChannelPool(Lists.newArrayList(sub1, sub2)); Truth.assertThat(pool.authority()).isEqualTo("myAuth"); }
@Test public void testRoundRobin() { ManagedChannel sub1 = Mockito.mock(ManagedChannel.class); ManagedChannel sub2 = Mockito.mock(ManagedChannel.class); Mockito.when(sub1.authority()).thenReturn("myAuth"); ArrayList<ManagedChannel> channels = Lists.newArrayList(sub1, sub2); ChannelPool pool = new ChannelPool(channels); verifyTargetChannel(pool, channels, sub1); verifyTargetChannel(pool, channels, sub2); verifyTargetChannel(pool, channels, sub1); }
final ChannelPool pool = new ChannelPool(Arrays.asList(channels));
@Test public void testAffinity() { MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE; @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class); @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class); ManagedChannel channel0 = Mockito.mock(ManagedChannel.class); ManagedChannel channel1 = Mockito.mock(ManagedChannel.class); Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall0); Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall1); Channel pool = new ChannelPool(Arrays.asList(channel0, channel1)); GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool); ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1)); assertThat(gotCallA).isSameAs(gotCallB); assertThat(gotCallA).isNotSameAs(gotCallC); }