@Test
  public void readIntoAWCFailureTest() throws Exception {
    String errMsg = "@@ExpectedExceptionMessage@@";
    InputStream stream = new ByteBufferInputStream(ByteBuffer.allocate(1));

    // Bad AWC.
    InputStreamReadableStreamChannel channel = new InputStreamReadableStreamChannel(stream, EXECUTOR_SERVICE);
    ReadIntoCallback callback = new ReadIntoCallback();
    try {
      channel.readInto(new BadAsyncWritableChannel(new IOException(errMsg)), callback).get();
      fail("Should have failed because BadAsyncWritableChannel would have thrown exception");
    } catch (ExecutionException e) {
      Exception exception = (Exception) Utils.getRootCause(e);
      assertEquals("Exception message does not match expected (future)", errMsg, exception.getMessage());
      callback.awaitCallback();
      assertEquals("Exception message does not match expected (callback)", errMsg, callback.exception.getMessage());
    }

    // Read after close.
    channel = new InputStreamReadableStreamChannel(stream, EXECUTOR_SERVICE);
    channel.close();
    RetainingAsyncWritableChannel writeChannel = new RetainingAsyncWritableChannel();
    callback = new ReadIntoCallback();
    try {
      channel.readInto(writeChannel, callback).get();
      fail("InputStreamReadableStreamChannel has been closed, so read should have thrown ClosedChannelException");
    } catch (ExecutionException e) {
      Exception exception = (Exception) Utils.getRootCause(e);
      assertTrue("Exception is not ClosedChannelException", exception instanceof ClosedChannelException);
      callback.awaitCallback();
      assertEquals("Exceptions of callback and future differ", exception.getMessage(), callback.exception.getMessage());
    }

    // Reading more than once.
    channel = new InputStreamReadableStreamChannel(stream, EXECUTOR_SERVICE);
    writeChannel = new RetainingAsyncWritableChannel();
    channel.readInto(writeChannel, null);
    try {
      channel.readInto(writeChannel, null);
      fail("Should have failed because readInto cannot be called more than once");
    } catch (IllegalStateException e) {
      // expected. Nothing to do.
    }
  }
Tests that the right exceptions are thrown when reading into {@link AsyncWritableChannel} fails.
@throws Exception
ambry-commons/src/test/java/com/github/ambry/commons/InputStreamReadableStreamChannelTest.java