IncompleteResultsThrottler { public int availableCapacity() { return blocker.availablePermits(); } IncompleteResultsThrottler(int minInFlightValue, int maxInFlightValue, Clock clock, ThrottlePolicy policy); int availableCapacity(); void operationStart(); String getDebugMessage(); void resultReady(boolean success); final long phaseSizeMs; }
@Test public void testInteractionWithPolicyByMockingPolicy() throws InterruptedException { ManualClock clock = new ManualClock(Instant.ofEpochMilli(0)); final int MAX_SIZE = 1000; final int MORE_THAN_MAX_SIZE = MAX_SIZE + 20; final int SIZE_AFTER_CYCLE_FIRST = 30; final int SIZE_AFTER_CYCLE_SECOND = 5000; ThrottlePolicy policy = mock(ThrottlePolicy.class); IncompleteResultsThrottler incompleteResultsThrottler = new IncompleteResultsThrottler(2, MAX_SIZE, clock, policy); long bucketSizeMs = incompleteResultsThrottler.phaseSizeMs; assertEquals(IncompleteResultsThrottler.INITIAL_MAX_IN_FLIGHT_VALUE, incompleteResultsThrottler.availableCapacity()); postOperations(20, incompleteResultsThrottler); postSuccesses(15, incompleteResultsThrottler); moveToNextCycle(incompleteResultsThrottler, clock); assertEquals("5 slots already taken earlier", IncompleteResultsThrottler.SECOND_MAX_IN_FLIGHT_VALUE - 5, incompleteResultsThrottler.availableCapacity()); postSuccesses(5, incompleteResultsThrottler); when(policy.calcNewMaxInFlight( anyDouble(), eq(5), eq(15), eq(IncompleteResultsThrottler.INITIAL_MAX_IN_FLIGHT_VALUE), eq(IncompleteResultsThrottler.SECOND_MAX_IN_FLIGHT_VALUE), eq(false))) .thenReturn(SIZE_AFTER_CYCLE_FIRST); moveToNextCycle(incompleteResultsThrottler, clock); assertEquals(SIZE_AFTER_CYCLE_FIRST, incompleteResultsThrottler.availableCapacity()); postOperations(MORE_THAN_MAX_SIZE, incompleteResultsThrottler); postSuccesses(MORE_THAN_MAX_SIZE, incompleteResultsThrottler); when(policy.calcNewMaxInFlight( anyDouble(), eq(MORE_THAN_MAX_SIZE), eq(5), eq(IncompleteResultsThrottler.SECOND_MAX_IN_FLIGHT_VALUE), eq(SIZE_AFTER_CYCLE_FIRST), eq(true))) .thenReturn(SIZE_AFTER_CYCLE_SECOND); moveToNextCycle(incompleteResultsThrottler, clock); assertEquals(MAX_SIZE, incompleteResultsThrottler.availableCapacity()); }