Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 0f7692e

Browse files
committed
test: Deflake plan refresh deadline test
Change-Id: I1605f65fe7053adee9e88387c8fcfbe60736be07
1 parent 202b211 commit 0f7692e

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/sql/PlanRefreshingCallableTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)