5858import org .junit .Test ;
5959import org .junit .runner .RunWith ;
6060import org .junit .runners .Parameterized ;
61- import org .mockito .Mockito ;
6261
6362@ RunWith (Parameterized .class )
6463public class TestRetryBehavior {
6564
6665 private static final Duration DELAY = Duration .ofMillis (5 );
6766 private static final Duration ATTEMPT_TIMEOUT = Duration .ofMillis (450 );
6867 private static final Duration OPERATION_TIMEOUT = Duration .ofSeconds (2 );
68+ private static final Duration DEADLINE_TOLERANCE = Duration .ofMillis (100 );
6969 private final TestOp testOp ;
7070
7171 private Server fakeServer ;
@@ -127,24 +127,21 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
127127 ServerCall <ReqT , RespT > serverCall ,
128128 Metadata metadata ,
129129 ServerCallHandler <ReqT , RespT > serverCallHandler ) {
130- callCount .incrementAndGet ();
131-
132- // record the deadline
130+ // record the deadline before incrementing callCount
133131 @ Nullable
134132 Duration duration =
135133 Optional .ofNullable (Context .current ().getDeadline ())
136134 .map (d -> Duration .ofMillis (d .timeRemaining (TimeUnit .MILLISECONDS )))
137135 .orElse (null );
138136 remainingDurations .add (duration );
137+ callCount .incrementAndGet ();
139138
140139 // execute test specific callback
141140 rpcRunnable .get ().accept (serverCall );
142141
143142 // Don't really care about client requests, just blackhole them
144143 // this will prevent any invocations of the stub
145- @ SuppressWarnings ("unchecked" )
146- ServerCall .Listener <ReqT > blackhole = Mockito .mock (ServerCall .Listener .class );
147- return blackhole ;
144+ return new ServerCall .Listener <ReqT >() {};
148145 }
149146 })
150147 .addService (new BigtableGrpc .BigtableImplBase () {})
@@ -203,7 +200,9 @@ public void testRpcWillRetryOnAbort() throws Exception {
203200 Duration elapsed = stopwatch .elapsed ();
204201
205202 Status status = extractStatus (e );
206- assertThat (status .getCode ()).isEqualTo (Status .Code .ABORTED );
203+ // Retries can race with the overall operation deadline, surfacing DEADLINE_EXCEEDED
204+ // instead of ABORTED on the final attempt.
205+ assertThat (status .getCode ()).isAnyOf (Status .Code .ABORTED , Status .Code .DEADLINE_EXCEEDED );
207206
208207 // Server immediately errors out, so exponential backoff will be the bottleneck for retries
209208 // Maximum delays will be: 5, 10, 20, 40, 80, 160, 320, 640, 1m, 1m, ...
@@ -218,7 +217,7 @@ public void testRpcWillRetryOnAbort() throws Exception {
218217 // Ignoring the first attempt, because it will be unstable due to connection setup
219218 assertThat (remainingDurations .subList (1 , 9 ))
220219 .comparingElementsUsing (MIN_DURATION_CORRESPONDENCE )
221- .contains (ATTEMPT_TIMEOUT .minus (Duration . ofMillis ( 5 ) ));
220+ .contains (ATTEMPT_TIMEOUT .minus (DEADLINE_TOLERANCE ));
222221
223222 // but should still be limited by the operation timeout with some jitter
224223 assertThat (elapsed ).isAtMost (OPERATION_TIMEOUT .plus (ATTEMPT_TIMEOUT .dividedBy (2 )));
@@ -257,7 +256,7 @@ public void testRpcWillRespectAttemptTimeout() throws IOException {
257256 // Ignoring the first and last attempt, all attempts should be just under attempt timeout
258257 assertThat (remainingDurations .subList (1 , remainingDurations .size () - 1 ))
259258 .comparingElementsUsing (MIN_DURATION_CORRESPONDENCE )
260- .contains (ATTEMPT_TIMEOUT .minus (Duration . ofMillis ( 10 ) ));
259+ .contains (ATTEMPT_TIMEOUT .minus (DEADLINE_TOLERANCE ));
261260
262261 // Altogether should still be limited by the operation timeout with some jitter
263262 assertThat (elapsed ).isAtMost (OPERATION_TIMEOUT .plus (ATTEMPT_TIMEOUT ));
0 commit comments