@Test
public void testRetries() throws Exception {
// create a sender with retries = 1
int maxRetries = 1;
Sender sender = new Sender(client,
metadata,
this.accumulator,
MAX_REQUEST_SIZE,
ACKS_ALL,
maxRetries,
REQUEST_TIMEOUT_MS,
new Metrics(),
time);
// do a successful retry
Future<RecordMetadata> future = accumulator.append(tp, "key".getBytes(), "value".getBytes(), CompressionType.NONE, null).future;
sender.run(time.milliseconds()); // connect
sender.run(time.milliseconds()); // send produce request
assertEquals(1, client.inFlightRequestCount());
client.disconnect(client.requests().peek().request().destination());
assertEquals(0, client.inFlightRequestCount());
sender.run(time.milliseconds()); // receive error
sender.run(time.milliseconds()); // reconnect
sender.run(time.milliseconds()); // resend
assertEquals(1, client.inFlightRequestCount());
int offset = 0;
client.respond(produceResponse(tp.topic(), tp.partition(), offset, Errors.NONE.code()));
sender.run(time.milliseconds());
assertTrue("Request should have retried and completed", future.isDone());
assertEquals(offset, future.get().offset());
// do an unsuccessful retry
future = accumulator.append(tp, "key".getBytes(), "value".getBytes(), CompressionType.NONE, null).future;
sender.run(time.milliseconds()); // send produce request
for (int i = 0; i < maxRetries + 1; i++) {
client.disconnect(client.requests().peek().request().destination());
sender.run(time.milliseconds()); // receive error
sender.run(time.milliseconds()); // reconnect
sender.run(time.milliseconds()); // resend
}
sender.run(time.milliseconds());
completedWithError(future, Errors.NETWORK_EXCEPTION);
}