@Test
  public void handleResponseExceptionTest() throws Exception {
    ByteBufferRSC response = new ByteBufferRSC(ByteBuffer.allocate(0));
    // RestRequest null.
    try {
      asyncRequestResponseHandler.handleResponse(null, new MockRestResponseChannel(), response, null);
      fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
      // expected. Nothing to do.
    }

    // RestResponseChannel null.
    MockRestRequest restRequest = createRestRequest(RestMethod.GET, "/", new JSONObject(), null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    try {
      asyncRequestResponseHandler.handleResponse(restRequest, null, response, null);
      fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
      // expected. Nothing to do.
    }

    // Writing response throws exception.
    MockRestRequest goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    String exceptionMsg = "@@randomMsg@@";
    ReadableStreamChannel badResponse = new IncompleteReadReadableStreamChannel(null, new Exception(exceptionMsg));
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Exception message does not match", exceptionMsg, restResponseChannel.getException().getMessage());

    // Writing response is incomplete
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(0L, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());

    // No bytes read and no exception
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(null, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());

    // RestRequest is bad.
    BadRestRequest badRestRequest = new BadRestRequest();
    badRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    ByteBufferRSC goodResponse = new ByteBufferRSC(ByteBuffer.allocate(0));
    EventMonitor<ByteBufferRSC.Event> responseCloseMonitor =
        new EventMonitor<ByteBufferRSC.Event>(ByteBufferRSC.Event.Close);
    goodResponse.addListener(responseCloseMonitor);
    awaitResponse(asyncRequestResponseHandler, badRestRequest, restResponseChannel, goodResponse, null);
    assertTrue("Response is not closed", responseCloseMonitor.awaitEvent(1, TimeUnit.SECONDS));

    // AsyncRequestResponseHandler should still be alive and serving requests
    assertTrue("AsyncRequestResponseHandler is dead", asyncRequestResponseHandler.isRunning());
  }
Tests the reaction of {@link AsyncRequestResponseHandler#handleResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)} to some misbehaving components.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/AsyncRequestResponseHandlerTest.java