@Test
  public void clampMaxInflightRequestsTest() throws InterruptedException {
    primeTracker(localColoTracker, MIN_DATA_POINTS_REQUIRED, LOCAL_COLO_LATENCY_RANGE);
    primeTracker(crossColoTracker, MIN_DATA_POINTS_REQUIRED, CROSS_COLO_LATENCY_RANGE);
    double localColoCutoff = localColoTracker.getSnapshot().getValue(QUANTILE);
    double crossColoCutoff = crossColoTracker.getSnapshot().getValue(QUANTILE);
    // set max inflight number = 2 and excludeTimeout = false in this test
    OperationTracker ot = getOperationTracker(createRouterConfig(true, 3, 2, 3, null, false), mockPartition);
    // 3-0-0-0; 3-0-0-0
    sendRequests(ot, 2);
    // 1-2-0-0; 3-0-0-0
    // sleep for less than the cutoff
    time.sleep((long) localColoCutoff - 2);
    sendRequests(ot, 0);
    // push it over the edge
    time.sleep(5);
    // should only send one request because (inflight num + 1) == routerConfig.routerOperationTrackerMaxInflightRequests
    sendRequests(ot, 1);
    // 0-3-0-0; 3-0-0-0
    // mark one request TIMED_OUT
    ot.onResponse(partitionAndInflightReplicas.get(mockPartition).poll(), TrackedRequestFinalState.TIMED_OUT);
    // should send out 1 request
    sendRequests(ot, 1);
    // 0-2-0-1; 2-1-0-0

    // mark one request FAILURE
    ot.onResponse(partitionAndInflightReplicas.get(mockPartition).poll(), TrackedRequestFinalState.FAILURE);
    time.sleep((long) (crossColoCutoff - localColoCutoff) + 2);

    // should send out 1 request
    sendRequests(ot, 1);
    // 0-1-0-2; 1-2-0-0

    // mark 3 inflight requests SUCCESS
    while (!partitionAndInflightReplicas.get(mockPartition).isEmpty()) {
      assertFalse("Operation should not be done", ot.isDone());
      ot.onResponse(partitionAndInflightReplicas.get(mockPartition).poll(), TrackedRequestFinalState.SUCCESS);
    }
    assertTrue("Operation should have succeeded", ot.hasSucceeded());
    // past due counter should be 3 (note that pastDueCounter is updated only when Iterator.remove() is called)
    assertEquals("Past due counter is not expected", 3, pastDueCounter.getCount());
  }
Test that the max number of inflight requests should not exceed configured number.
@throws InterruptedException
ambry-router/src/test/java/com/github/ambry/router/AdaptiveOperationTrackerTest.java