@@ -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,30 +258,39 @@ 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 ;
263+ SettableApiFuture <Instant > prepareResolvedAt = SettableApiFuture .create ();
262264 scheduler .schedule (
263265 () -> {
266+ prepareResolvedAt .set (Instant .now ());
264267 prepareFuture .set (
265268 PrepareResponse .fromProto (
266269 prepareResponse (
267270 ByteString .copyFromUtf8 ("initialPlan" ),
268271 metadata (columnMetadata ("strCol" , stringType ())))));
269272 },
270- 50 ,
273+ prepareDelayMs ,
271274 TimeUnit .MILLISECONDS );
275+
276+ Duration originalAttemptTimeout = Duration .ofMillis (5000 );
272277 ApiCallContext context =
273278 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 );
279+ Instant startOfCall = Instant .now ();
278280 callable .call (callContext , outerObserver , context );
279281 scheduler .shutdown ();
280282 scheduler .awaitTermination (30 , TimeUnit .SECONDS );
281283 GrpcCallContext grpcCallContext =
282284 (GrpcCallContext ) innerCallable .popLastCall ().getApiCallContext ();
285+
286+ // Deadline should be at most (original deadline - prepare delay). Add 1ms to avoid flakiness.
287+ Duration prepareLatency = Duration .between (startOfCall , prepareResolvedAt .get ());
288+ Deadline deadlineUpperBound =
289+ Deadline .after (
290+ originalAttemptTimeout .toMillis () - prepareLatency .toMillis () + 1 ,
291+ TimeUnit .MILLISECONDS );
283292 Deadline executeDeadline = grpcCallContext .getCallOptions ().getDeadline ();
284- assertThat (executeDeadline .isBefore (paddedDeadlineAtStartOfCall )).isTrue ();
293+ assertThat (executeDeadline .isBefore (deadlineUpperBound )).isTrue ();
285294 }
286295
287296 @ Test
0 commit comments