Skip to content

Commit

Permalink
chore: Fix flaky testCancelIsTraced test (#2039)
Browse files Browse the repository at this point in the history
* chore: Fix flaky testCancelIsTraced test

* chore: Clean up testCancelIsTraced test

* chore: Remove similar test

* Update gax-java/gax/src/test/java/com/google/api/gax/retrying/ScheduledRetryingExecutorTest.java

Co-authored-by: Blake Li <blakeli@google.com>

* chore: Remove unneeded comment

---------

Co-authored-by: Blake Li <blakeli@google.com>
  • Loading branch information
lqiu96 and blakeli0 authored Oct 26, 2023
1 parent 097e37e commit 5654aa6
Showing 1 changed file with 5 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ public void testCancelOuterFutureAfterStart() throws Exception {
boolean res = future.cancel(false);
assertTrue(res);
assertFutureCancel(future);

// Verify that the cancelled future is traced. Every attempt increases the number
// of cancellation attempts from the tracer.
Mockito.verify(tracer, Mockito.times(executionsCount + 1)).attemptCancelled();

// Assert that future has at least been attempted once
// i.e. The future from executor.submit() has been run by the ScheduledExecutor
assertTrue(future.getAttemptSettings().getAttemptCount() > 0);
Expand All @@ -297,34 +302,6 @@ public void testCancelOuterFutureAfterStart() throws Exception {
localExecutor.awaitTermination(10, TimeUnit.SECONDS);
}

@Test
public void testCancelIsTraced() throws Exception {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(4, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setInitialRetryDelay(Duration.ofMillis(1_000L))
.setMaxRetryDelay(Duration.ofMillis(1_000L))
.setTotalTimeout(Duration.ofMillis(10_0000L))
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
callable.setExternalFuture(future);
future.setAttemptFuture(executor.submit(future));

Thread.sleep(30L);

boolean res = future.cancel(false);
assertTrue(res);
assertFutureCancel(future);

Mockito.verify(tracer).attemptCancelled();
localExecutor.shutdownNow();
}

@Test
public void testCancelProxiedFutureAfterStart() throws Exception {
// this is a heavy test, which takes a lot of time, so only few executions.
Expand Down

0 comments on commit 5654aa6

Please sign in to comment.