@@ -244,7 +244,7 @@ public void testIsPlanRefreshError() {
244244 }
245245
246246 @ Test
247- public void planRefreshDelayIsFactoredIntoExecuteTimeout () throws InterruptedException {
247+ public void planRefreshDelayIsFactoredIntoExecuteTimeout () throws InterruptedException , ExecutionException {
248248 MockServerStreamingCallable <ExecuteQueryRequest , ExecuteQueryResponse > innerCallable =
249249 new MockServerStreamingCallable <>();
250250 RequestContext requestContext = RequestContext .create ("project" , "instance" , "profile" );
@@ -258,7 +258,8 @@ public void planRefreshDelayIsFactoredIntoExecuteTimeout() throws InterruptedExc
258258 ExecuteQueryCallContext callContext =
259259 ExecuteQueryCallContext .create (preparedStatement .bind ().build (), metadataFuture );
260260
261- Duration originalAttemptTimeout = Duration .ofMillis (100 );
261+ // Simulate delay for prepare response
262+ int prepareDelayMs = 50 ;
262263 scheduler .schedule (
263264 () -> {
264265 prepareFuture .set (
@@ -267,21 +268,30 @@ public void planRefreshDelayIsFactoredIntoExecuteTimeout() throws InterruptedExc
267268 ByteString .copyFromUtf8 ("initialPlan" ),
268269 metadata (columnMetadata ("strCol" , stringType ())))));
269270 },
270- 50 ,
271+ prepareDelayMs ,
271272 TimeUnit .MILLISECONDS );
273+
274+ Duration originalAttemptTimeout = Duration .ofMillis (500 );
272275 ApiCallContext context =
273276 GrpcCallContext .createDefault ().withTimeoutDuration (originalAttemptTimeout );
274- // prepare takes 50 ms to resolve. Despite that the execute timeout should be around 100ms from
275- // now (w padding)
276- Deadline paddedDeadlineAtStartOfCall =
277- Deadline .after (originalAttemptTimeout .toMillis () + 5 , TimeUnit .MILLISECONDS );
277+ Instant startOfCall = Instant .now ();
278278 callable .call (callContext , outerObserver , context );
279279 scheduler .shutdown ();
280280 scheduler .awaitTermination (30 , TimeUnit .SECONDS );
281281 GrpcCallContext grpcCallContext =
282282 (GrpcCallContext ) innerCallable .popLastCall ().getApiCallContext ();
283+
284+ // Deadline should be around (attemptDeadline - prepareDelay)
285+ // add some padding to avoid flakiness.
286+ int paddingMs = 5 ;
287+ Instant estimatedDeadlineTs =
288+ startOfCall
289+ .plus (originalAttemptTimeout )
290+ .minus (Duration .ofMillis (prepareDelayMs - paddingMs ));
291+ Deadline deadlineUpperBound =
292+ Deadline .after (estimatedDeadlineTs .toEpochMilli (), TimeUnit .MILLISECONDS );
283293 Deadline executeDeadline = grpcCallContext .getCallOptions ().getDeadline ();
284- assertThat (executeDeadline .isBefore (paddedDeadlineAtStartOfCall )).isTrue ();
294+ assertThat (executeDeadline .isBefore (deadlineUpperBound )).isTrue ();
285295 }
286296
287297 @ Test
0 commit comments