@Test public void testFullRequestsStatistical() { when(config.clientConfigs()).thenReturn(clientConfigs); Random mockRandom = mock(Random.class); subject = new RoundRobinProxyHandler(null, config, null, () -> mockRandom); Request request1 = mock(Request.class); Request request2 = mock(Request.class); Request request3 = mock(Request.class); Request request4 = mock(Request.class); when(request1.isFullMessage()).thenReturn(true); when(request1.path()).thenReturn("dontcare"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.isFullMessage()).thenReturn(true); when(request2.path()).thenReturn("dontcare"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.isFullMessage()).thenReturn(true); when(request3.path()).thenReturn("dontcare"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request4.isFullMessage()).thenReturn(true); when(request4.path()).thenReturn("dontcare"); when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>(); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0); ClientConfig result1 = subject.getClientConfig(cacheMap, request1).orElse(null); assertEquals(clientConfig1, result1); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(1); ClientConfig result2 = subject.getClientConfig(cacheMap, request2).orElse(null); assertEquals(clientConfig2, result2); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(2); ClientConfig result3 = subject.getClientConfig(cacheMap, request3).orElse(null); assertEquals(clientConfig3, result3); when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0); ClientConfig result4 = subject.getClientConfig(cacheMap, request3).orElse(null); assertEquals(clientConfig1, result4); 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);  }