@Test
  public void testRejectRequestErrorCode() throws IOException, InterruptedException {
    // Create a delete network request
    MockClusterMap clusterMap = new MockClusterMap();
    short accountId = Utils.getRandomShort(TestUtils.RANDOM);
    short containerId = Utils.getRandomShort(TestUtils.RANDOM);
    BlobId id1 = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE,
        ClusterMap.UNKNOWN_DATACENTER_ID, accountId, containerId,
        clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false,
        BlobId.BlobDataType.DATACHUNK);
    long deletionTimeMs = Utils.getRandomLong(TestUtils.RANDOM, Long.MAX_VALUE);
    int correlationId = TestUtils.RANDOM.nextInt();
    String clientId = "clientId";
    DeleteRequest deleteRequest;
    deleteRequest = new DeleteRequest(correlationId, clientId, id1, deletionTimeMs);
    DataInputStream requestStream = serAndPrepForRead(deleteRequest);
    MockRequest mockRequest = new MockRequest(requestStream);

    // Create NettyServerRequestResponseChannel
    ServerRequestResponseHelper requestResponseHelper = new ServerRequestResponseHelper(clusterMap, null);
    Properties properties = new Properties();
    properties.setProperty(NetworkConfig.REQUEST_QUEUE_CAPACITY, "2");
    NettyServerRequestResponseChannel channel =
        spy(new NettyServerRequestResponseChannel(new NetworkConfig(new VerifiableProperties(properties)),
            new Http2ServerMetrics(new MetricRegistry()), new ServerMetrics(new MetricRegistry(), this.getClass()),
            requestResponseHelper));
    doNothing().when(channel).sendResponse(any(), any(), any());

    channel.sendRequest(createNettyServerRequest(1));
    channel.sendRequest(createNettyServerRequest(1));
    channel.sendRequest(mockRequest);
    // 1.Verify rejectRequest() is invoked
    verify(channel, times(1)).rejectRequest(eq(mockRequest), eq(false));
    // 2. Verify send response is called
    ArgumentCaptor<DeleteResponse> argument = ArgumentCaptor.forClass(DeleteResponse.class);
    verify(channel, atLeastOnce()).sendResponse(argument.capture(), any(), any());
    // 3. Verify error code is correct
    assertEquals("Mismatch in response type", argument.getValue().getRequestType(),
        RequestOrResponseType.DeleteResponse);
    assertEquals("Mismatch in response correlation id", argument.getValue().getCorrelationId(), correlationId);
    assertEquals("Mismatch in response client id", argument.getValue().getClientId(), clientId);
    assertEquals("Mismatch in error code", argument.getValue().getError(), ServerErrorCode.RetryAfterBackoff);
  }
Test send and receive requests
@throws Exception
ambry-network/src/test/java/com/github/ambry/network/NettyServerRequestResponseChannelTest.java