@Test public void testChunkedRequestsHttp2Statistical() { when(config.clientConfigs()).thenReturn(clientConfigs); Random mockRandom = mock(Random.class); subject = new RoundRobinProxyHandler(null, config, null, () -> mockRandom); int streamId1 = 1; int streamId2 = 3; Request request1a = mock(Request.class); Request request2a = mock(Request.class); Request request3a = mock(Request.class); Request request1b = mock(Request.class); Request request2b = mock(Request.class); Request request3b = mock(Request.class); when(request1a.isFullMessage()).thenReturn(false); when(request1a.startOfMessage()).thenReturn(true); when(request1a.endOfMessage()).thenReturn(false); when(request1a.path()).thenReturn("/foo"); when(request1a.streamId()).thenReturn(streamId1); when(request1b.isFullMessage()).thenReturn(false); when(request1b.startOfMessage()).thenReturn(true); when(request1b.endOfMessage()).thenReturn(false); when(request1b.path()).thenReturn("/foo"); when(request1b.streamId()).thenReturn(streamId2); when(request2a.isFullMessage()).thenReturn(false); when(request2a.startOfMessage()).thenReturn(false); when(request2a.endOfMessage()).thenReturn(false); when(request2a.path()).thenReturn("/foo"); when(request2a.streamId()).thenReturn(streamId1); when(request2b.isFullMessage()).thenReturn(false); when(request2b.startOfMessage()).thenReturn(false); when(request2b.endOfMessage()).thenReturn(false); when(request2b.path()).thenReturn("/foo"); when(request2b.streamId()).thenReturn(streamId2); when(request3a.isFullMessage()).thenReturn(false); when(request3a.startOfMessage()).thenReturn(false); when(request3a.endOfMessage()).thenReturn(true); when(request3a.path()).thenReturn("/foo"); when(request3a.streamId()).thenReturn(streamId1); when(request3b.isFullMessage()).thenReturn(false); when(request3b.startOfMessage()).thenReturn(false); when(request3b.endOfMessage()).thenReturn(true); when(request3b.path()).thenReturn("/foo"); when(request3b.streamId()).thenReturn(streamId2); IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>(); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0); ClientConfig result1a = subject.getClientConfig(cacheMap, request1a).orElse(null); assertEquals(clientConfig1, result1a); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(1); ClientConfig result1b = subject.getClientConfig(cacheMap, request1b).orElse(null); assertEquals(clientConfig2, result1b); ClientConfig result2a = subject.getClientConfig(cacheMap, request2a).orElse(null); assertEquals(clientConfig1, result2a); ClientConfig result2b = subject.getClientConfig(cacheMap, request2b).orElse(null); assertEquals(clientConfig2, result2b); ClientConfig result3a = subject.getClientConfig(cacheMap, request3a).orElse(null); assertEquals(clientConfig1, result3a); ClientConfig result3b = subject.getClientConfig(cacheMap, request3b).orElse(null); assertEquals(clientConfig2, result3b); assertTrue(cacheMap.isEmpty()); }
@Override public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) { IntObjectMap<Optional<ClientConfig>> cachedClientConfig = ctx.channel().attr(ROUND_ROBIN_KEY).get(); if (cachedClientConfig == null) { cachedClientConfig = new IntObjectHashMap<>(); ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig); } return getClientConfig(cachedClientConfig, request); }
RoundRobinProxyHandler extends ProxyHandler { @Override public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) { IntObjectMap<Optional<ClientConfig>> cachedClientConfig = ctx.channel().attr(ROUND_ROBIN_KEY).get(); if (cachedClientConfig == null) { cachedClientConfig = new IntObjectHashMap<>(); ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig); } return getClientConfig(cachedClientConfig, request); } }
RoundRobinProxyHandler extends ProxyHandler { @Override public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) { IntObjectMap<Optional<ClientConfig>> cachedClientConfig = ctx.channel().attr(ROUND_ROBIN_KEY).get(); if (cachedClientConfig == null) { cachedClientConfig = new IntObjectHashMap<>(); ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig); } return getClientConfig(cachedClientConfig, request); } private  RoundRobinProxyHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); @VisibleForTesting  RoundRobinProxyHandler(
      ClientFactory factory,
      ProxyRouteConfig config,
      SocketAddressHelper addressHelper,
      Supplier<Random> randomSupplier); }
RoundRobinProxyHandler extends ProxyHandler { @Override public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) { IntObjectMap<Optional<ClientConfig>> cachedClientConfig = ctx.channel().attr(ROUND_ROBIN_KEY).get(); if (cachedClientConfig == null) { cachedClientConfig = new IntObjectHashMap<>(); ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig); } return getClientConfig(cachedClientConfig, request); } private  RoundRobinProxyHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); @VisibleForTesting  RoundRobinProxyHandler(
      ClientFactory factory,
      ProxyRouteConfig config,
      SocketAddressHelper addressHelper,
      Supplier<Random> randomSupplier); static RoundRobinProxyHandler createStatisticalRoundRobinHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); static RoundRobinProxyHandler createStandardRoundRobinHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); @Override Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request); }
RoundRobinProxyHandler extends ProxyHandler { @Override public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) { IntObjectMap<Optional<ClientConfig>> cachedClientConfig = ctx.channel().attr(ROUND_ROBIN_KEY).get(); if (cachedClientConfig == null) { cachedClientConfig = new IntObjectHashMap<>(); ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig); } return getClientConfig(cachedClientConfig, request); } private  RoundRobinProxyHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); @VisibleForTesting  RoundRobinProxyHandler(
      ClientFactory factory,
      ProxyRouteConfig config,
      SocketAddressHelper addressHelper,
      Supplier<Random> randomSupplier); static RoundRobinProxyHandler createStatisticalRoundRobinHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); static RoundRobinProxyHandler createStandardRoundRobinHandler(
      ClientFactory factory, ProxyRouteConfig config, SocketAddressHelper addressHelper); @Override Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request);  }