@Test
  public void testReceiveTimeout() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    Http2BlockingChannelResponseHandler handler = new Http2BlockingChannelResponseHandler() {
      @Override
      protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
        latch.await();
        super.channelRead0(ctx, msg);
      }
    };

    long receiveTimeoutMs = 1000;
    EmbeddedChannelPool channelPool = new EmbeddedChannelPool(handler);
    InetSocketAddress address = new InetSocketAddress("localhost", 2021);
    Http2BlockingChannel blockingChannel =
        new Http2BlockingChannel(channelPool, address, createHttp2ClientConfig(1000, 1000, receiveTimeoutMs));

    int sendSize = 1234;
    byte[] byteArray = new byte[sendSize];
    new Random().nextBytes(byteArray);
    ByteBuf content = PooledByteBufAllocator.DEFAULT.heapBuffer(sendSize).writeBytes(byteArray);
    MockSend send = new MockSend(content);
    ScheduledFuture<ChannelOutput> future =
        service.schedule(() -> blockingChannel.sendAndReceive(send), 0, TimeUnit.MILLISECONDS);

    EmbeddedChannel channel = channelPool.getCurrentChannel();
    int responseSize = 100;
    byteArray = new byte[responseSize];
    new Random().nextBytes(byteArray);
    ByteBuf response =
        PooledByteBufAllocator.DEFAULT.heapBuffer(responseSize + 8).writeLong(responseSize + 8).writeBytes(byteArray);
    service.submit(() -> {
      channel.writeInbound(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, response));
    });

    // Blocking channel is waiting for response. Response is blocked by countdown latch.
    Thread.sleep(2 * receiveTimeoutMs);
    latch.countDown();

    try {
      future.get();
      Assert.fail("Should fail on receiving response");
    } catch (Exception e) {
    }

    content.release();
    // response.release();
  }
Test when receiving response times out
@throws Exception
ambry-network/src/test/java/com/github/ambry/network/http2/Http2BlockingChannelTest.java