@Test
  public void testOneExpiredAnotherServed() throws Exception {
    final int size = 1024;
    final long capacity = 1024;
    final int numOfRequests = 3;
    final long[] blockTimeInMs = {10, 10, 1000};
    SimpleByteBufferPool pool = new SimpleByteBufferPool(capacity);
    CountDownLatch[] allocated = new CountDownLatch[numOfRequests];
    CountDownLatch[] used = new CountDownLatch[numOfRequests];
    BufferConsumer[] consumers = new BufferConsumer[numOfRequests];
    Thread[] consumerThreads = new Thread[numOfRequests];
    for (int i = 0; i < numOfRequests; i++) {
      allocated[i] = new CountDownLatch(1);
      used[i] = new CountDownLatch(1);
      consumers[i] = new BufferConsumer(size, blockTimeInMs[i], pool, allocated[i], used[i]);
      consumerThreads[i] = new Thread(consumers[i]);
    }
    consumerThreads[0].start(); // Time t0
    if (!allocated[0].await(100, TimeUnit.MILLISECONDS)) {
      fail("SimpleByteBufferPool takes too long time to allocate a buffer to BufferConsumer[0].");
    }
    consumerThreads[1].start(); // Time t0 + delta
    consumerThreads[2].start(); // Time t0 + delta
    consumerThreads[1].join(); // wait until consumers[1] timeout
    if (!(consumers[1].exception instanceof TimeoutException)) {
      throw consumers[1].exception;
    }
    used[0].countDown(); // Time t2, R0 releases its buffer back to the buffer pool.
    used[2].countDown();
    consumerThreads[0].join();
    consumerThreads[2].join();
    if (consumers[0].exception != null) {
      throw consumers[0].exception;
    } else if (consumers[2].exception != null) {
      throw consumers[2].exception;
    }
    assertEquals(capacity, pool.availableMemory());
  }
In this test scenario, there are three requests: R0, R1, and R2.
At time t0: R0 is made and get served
At time t0 + delta: R1 and R2 are made and get blocked.
At time t1: R1 gets timeout.
At time t2: R0 releases its buffer back to the buffer pool.
At time t2 + delta: R2 gets served.
ambry-utils/src/test/java/com/github/ambry/utils/SimpleByteBufferPoolTest.java