@Test
  public void behaviourUnderWriteFailuresTest() throws Exception {
    onResponseCompleteUnderWriteFailureTest(TestingUri.ImmediateResponseComplete);
    onResponseCompleteUnderWriteFailureTest(TestingUri.OnResponseCompleteWithNonRestException);

    // writing to channel with a outbound handler that generates an Exception
    String message = TestUtils.getRandomString(10);
    try {
      String content = "@@randomContent@@@";
      MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
      ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new Exception(message));
      EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
      channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
      // channel has been closed because of write failure
      channel.writeInbound(createContent(content, true));
      verifyCallbacks(processor);
      fail("Callback for write would have thrown an Exception");
    } catch (Exception e) {
      assertEquals("Exception not as expected", message, e.getMessage());
    }

    // writing to channel with a outbound handler that encounters a ClosedChannelException
    try {
      String content = "@@randomContent@@@";
      MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
      ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler(new ClosedChannelException());
      EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
      channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
      // channel has been closed because of write failure
      channel.writeInbound(createContent(content, true));
      verifyCallbacks(processor);
      fail("Callback for write would have thrown an Exception");
    } catch (IOException e) {
      assertTrue("Should be recognized as a client termination", Utils.isPossibleClientTermination(e));
    }

    // writing to channel with a outbound handler that generates an Error
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    EmbeddedChannel channel = new EmbeddedChannel(new ErrorOutboundHandler(), processor);
    try {
      channel.writeInbound(
          RestTestUtils.createRequest(HttpMethod.GET, TestingUri.WriteFailureWithThrowable.toString(), null));
      verifyCallbacks(processor);
    } catch (Error e) {
      assertEquals("Unexpected error", ErrorOutboundHandler.ERROR_MESSAGE, e.getMessage());
    }

    channel = createEmbeddedChannel();
    processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    channel.writeInbound(
        RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ResponseFailureMidway.toString(), null));
    verifyCallbacks(processor);
    assertFalse("Channel is not closed at the remote end", channel.isActive());
  }
Tests behaviour of various functions of {@link NettyResponseChannel} under write failures.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/NettyResponseChannelTest.java