ConnectionThrottler extends ContainerLifeCycle implements SelectorManager.AcceptListener { @Override public void onAccepting(SelectableChannel channel) { throttleIfAnyThresholdIsExceeded(); } ConnectionThrottler(AbstractConnector connector, ConnectorConfig.Throttling config);   ConnectionThrottler(Runtime runtime,
                        RateStatistic rateStatistic,
                        Scheduler scheduler,
                        AbstractConnector connector,
                        ConnectorConfig.Throttling config); @Override void onAccepting(SelectableChannel channel);  }
@Test public void throttles_when_any_resource_check_exceeds_configured_threshold() { Runtime runtime = mock(Runtime.class); when(runtime.maxMemory()).thenReturn(100l); RateStatistic rateStatistic = new RateStatistic(1, TimeUnit.HOURS); MockScheduler scheduler = new MockScheduler(); ConnectorConfig.Throttling config = new ConnectorConfig.Throttling(new ConnectorConfig.Throttling.Builder() .maxHeapUtilization(0.8) .maxAcceptRate(1)); AbstractConnector connector = mock(AbstractConnector.class); ConnectionThrottler throttler = new ConnectionThrottler(runtime, rateStatistic, scheduler, connector, config); when(runtime.freeMemory()).thenReturn(10l); when(connector.isAccepting()).thenReturn(true); throttler.onAccepting(null); assertNotNull(scheduler.task); verify(connector).setAccepting(false); when(runtime.freeMemory()).thenReturn(80l); rateStatistic.record(); rateStatistic.record(); scheduler.task.run(); verify(connector, times(1)).setAccepting(anyBoolean()); when(runtime.freeMemory()).thenReturn(80l); when(connector.isAccepting()).thenReturn(false); rateStatistic.reset(); scheduler.task.run(); verify(connector).setAccepting(true); when(connector.isAccepting()).thenReturn(true); when(runtime.freeMemory()).thenReturn(80l); rateStatistic.record(); throttler.onAccepting(null); verify(connector, times(2)).setAccepting(anyBoolean()); }