@Test
  public void readIntoAWCFailureTest() throws Exception {
    String errMsg = "@@ExpectedExceptionMessage@@";
    byte[] in = fillRandomBytes(new byte[1]);

    // Bad AWC.
    ByteBufferReadableStreamChannel readableStreamChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    ReadIntoCallback callback = new ReadIntoCallback();
    try {
      readableStreamChannel.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());
    }

    // Reading more than once.
    readableStreamChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    ByteBufferAsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    readableStreamChannel.readInto(writeChannel, null);
    try {
      readableStreamChannel.readInto(writeChannel, null);
      fail("Should have failed because readInto cannot be called more than once");
    } catch (IllegalStateException e) {
      // expected. Nothing to do.
    }

    // Read after close.
    readableStreamChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    readableStreamChannel.close();
    writeChannel = new ByteBufferAsyncWritableChannel();
    callback = new ReadIntoCallback();
    try {
      readableStreamChannel.readInto(writeChannel, callback).get();
      fail("ByteBufferReadableStreamChannel 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());
    }
  }
Tests that the right exceptions are thrown when reading into {@link AsyncWritableChannel} fails.
@throws Exception
ambry-commons/src/test/java/com/github/ambry/commons/ByteBufferReadableStreamChannelTest.java