@Test
  public void testRejectRequestsOnExpiryWithException() throws IOException, InterruptedException {
    ServerRequestResponseHelper requestResponseHelper = mock(ServerRequestResponseHelper.class);
    MockTime mockTime = new MockTime();
    FifoNetworkRequestQueue requestQueue = new FifoNetworkRequestQueue(TIMEOUT, mockTime, CAPACITY);
    NettyServerRequestResponseChannel channel =
        spy(new NettyServerRequestResponseChannel(new Http2ServerMetrics(new MetricRegistry()),
            new ServerMetrics(new MetricRegistry(), this.getClass()), requestResponseHelper, requestQueue));
    doNothing().when(channel).sendResponse(any(), any(), any());
    when(requestResponseHelper.getDecodedRequest(any())).thenThrow(
        new UnsupportedOperationException("Request type not supported"));
    try {
      // Queue 2 requests with a sleep between them so that 1st request expires
      NettyServerRequest expiredRequest = createNettyServerRequest(mockTime.milliseconds());
      channel.sendRequest(expiredRequest);
      mockTime.sleep(TIMEOUT + 1);
      NettyServerRequest validRequest = createNettyServerRequest(mockTime.milliseconds());
      channel.sendRequest(validRequest);
      // 1. Verify received request is 2nd request
      NetworkRequest receiveRequest = channel.receiveRequest();
      assertEquals("Mismatch in request dequeued", validRequest, receiveRequest);
      // 2. Verify rejectRequest() is invoked with 1st request
      verify(channel, times(1)).rejectRequest(eq(expiredRequest), eq(true));
      // Verify close connection is invoked due to exception thrown and caught in rejectRequests()
      verify(channel, times(1)).closeConnection(any());
    } catch (Exception e) {
      fail("Should not have thrown any exception");
    }
  }
Test send and receive requests
@throws Exception
ambry-network/src/test/java/com/github/ambry/network/NettyServerRequestResponseChannelTest.java