@Test
  public void completeRequestWithDelayedCloseTest() throws Exception {
    Properties properties = new Properties();
    long delayMs = 500;
    properties.setProperty(NettyConfig.NETTY_SERVER_CLOSE_DELAY_TIMEOUT_MS, String.valueOf(delayMs));
    NettyConfig nettyConfig = new NettyConfig(new VerifiableProperties(properties));
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    processor.setNettyConfig(nettyConfig);
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    EmbeddedChannel channel = new EmbeddedChannel(chunkedWriteHandler, processor);

    RestServiceErrorCode REST_ERROR_CODE = RestServiceErrorCode.BadRequest;
    String content = "@@randomContent@@@";
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, REST_ERROR_CODE);
    httpHeaders.set(MockNettyMessageProcessor.INCLUDE_EXCEPTION_MESSAGE_IN_RESPONSE_HEADER_NAME, "true");
    HttpRequest httpRequest =
        RestTestUtils.createFullRequest(HttpMethod.POST, TestingUri.OnResponseCompleteWithRestException.toString(),
            httpHeaders, content.getBytes());
    channel.writeInbound(httpRequest);
    HttpResponse response = channel.readOutbound();
    assertEquals("Unexpected response status", getExpectedHttpResponseStatus(REST_ERROR_CODE), response.status());
    //channel should not be closed right away.
    assertTrue("Channel closed on the server", channel.isActive());
    //wait for delayed time * 2 times (to rule out timing out on border) and then check again.
    Thread.sleep(delayMs * 2);
    channel.runPendingTasks();
    assertFalse("Channel not closed on the server", channel.isActive());
    assertEquals("delayed close scheduled counter mismatch", 1,
        processor.getNettyMetrics().delayedCloseScheduledCount.getCount());
    assertEquals("delayed close executed counter mismatch", 1,
        processor.getNettyMetrics().delayedCloseExecutedCount.getCount());
    assertEquals("delayed close expired counter mismatch", 1,
        processor.getNettyMetrics().delayedCloseActivatedCount.getCount());
  }
Tests the invocation of DELAYED_CLOSE when post failures happen in {@link NettyResponseChannel}.
ambry-rest/src/test/java/com/github/ambry/rest/NettyResponseChannelTest.java