Skip to content

Commit 7c6849d

Browse files
committed
Fix retries that timeout hanging forever. [ Backport to 1.59.x] (grpc#10884)
* Fix retries that timeout hanging forever. (grpc#10855) Fixes grpc#10336
1 parent 455c840 commit 7c6849d

File tree

1 file changed

+19
-6
lines changed
  • interop-testing/src/test/java/io/grpc/testing/integration

1 file changed

+19
-6
lines changed

interop-testing/src/test/java/io/grpc/testing/integration/RetryTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public class RetryTest {
108108
@SuppressWarnings("unchecked")
109109
private ClientCall.Listener<Integer> mockCallListener =
110110
mock(ClientCall.Listener.class, delegatesTo(testCallListener));
111+
private java.util.concurrent.ScheduledFuture<?> activeFuture = null;
111112

112113
private CountDownLatch backoffLatch = new CountDownLatch(1);
113114
private final EventLoopGroup group = new DefaultEventLoopGroup() {
@@ -118,7 +119,7 @@ public ScheduledFuture<?> schedule(
118119
if (!command.getClass().getName().contains("RetryBackoffRunnable")) {
119120
return super.schedule(command, delay, unit);
120121
}
121-
fakeClock.getScheduledExecutorService().schedule(
122+
activeFuture = fakeClock.getScheduledExecutorService().schedule(
122123
new Runnable() {
123124
@Override
124125
public void run() {
@@ -248,15 +249,22 @@ private void assertInboundWireSizeRecorded(long length) throws Exception {
248249

249250
private void assertRpcStatusRecorded(
250251
Status.Code code, long roundtripLatencyMs, long outboundMessages) throws Exception {
252+
assertRpcStatusRecorded(code, roundtripLatencyMs, 0, outboundMessages);
253+
}
254+
255+
private void assertRpcStatusRecorded(
256+
Status.Code code, long roundtripLatencyMs, long toleranceMs, long outboundMessages)
257+
throws Exception {
251258
MetricsRecord record = clientStatsRecorder.pollRecord(7, SECONDS);
252259
assertNotNull(record);
253260
TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
254261
assertNotNull(statusTag);
255262
assertThat(statusTag.asString()).isEqualTo(code.toString());
256263
assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT))
257264
.isEqualTo(1);
258-
assertThat(record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_ROUNDTRIP_LATENCY))
259-
.isEqualTo(roundtripLatencyMs);
265+
long roundtripLatency =
266+
record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_ROUNDTRIP_LATENCY);
267+
assertThat(Math.abs(roundtripLatency - roundtripLatencyMs)).isAtMost(toleranceMs);
260268
assertThat(record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_RPC))
261269
.isEqualTo(outboundMessages);
262270
}
@@ -303,10 +311,12 @@ public void retryUntilBufferLimitExceeded() throws Exception {
303311
call.sendMessage(message);
304312

305313
// let attempt fail
306-
testCallListener.clear();
314+
testCallListener.reset();
307315
serverCall.close(
308316
Status.UNAVAILABLE.withDescription("2nd attempt failed"),
309317
new Metadata());
318+
fakeClock.forwardTime(1, SECONDS);
319+
activeFuture.get(1, SECONDS); // Make sure the close is done.
310320
// no more retry
311321
testCallListener.verifyDescription("2nd attempt failed", 5000);
312322
}
@@ -420,9 +430,12 @@ public void streamClosed(Status status) {
420430
call.cancel("Cancelled before commit", null);
421431
// Let the netty substream listener be closed.
422432
streamClosedLatch.countDown();
433+
assertNotNull("No activeFuture", activeFuture);
434+
fakeClock.forwardTime(1, SECONDS);
435+
activeFuture.get(1, SECONDS);
423436
// The call listener is closed.
424437
verify(mockCallListener, timeout(5000)).onClose(any(Status.class), any(Metadata.class));
425-
assertRpcStatusRecorded(Code.CANCELLED, 17_000, 1);
438+
assertRpcStatusRecorded(Code.CANCELLED, 18_000, 1);
426439
assertRetryStatsRecorded(1, 0, 0);
427440
}
428441

@@ -551,7 +564,7 @@ public void onClose(Status status, Metadata trailers) {
551564
closeLatch.countDown();
552565
}
553566

554-
void clear() {
567+
void reset() {
555568
status = null;
556569
closeLatch = new CountDownLatch(1);
557570
}

0 commit comments

Comments
 (0)