Skip to content

Commit

Permalink
Fix/Debug Flaky CancellationTest
Browse files Browse the repository at this point in the history
Motivation
----------
The CancellationTest#cancelOffload() is reportedly flaky and this
PR is a first attempt to understand why / fix the issues.

Modifications
-------------
Investigation found that it looks like on CI the sleeping of the
action races with the cancellation of the service single. Current
working theory is that if the cancellation arrives before the
service is executed, the thread sleep will never be triggered
and so the test completes fine. If the cancellation does not
arrive first, the sleep starts to happen and when the cancellation
arrives afterwards the thread is interrupted as part of the cancellation
which triggers the exception.

Current theory is if this is acceptable behavior, the error
is harmless and can be ignored.
  • Loading branch information
daschl committed Oct 16, 2023
1 parent 8b8d1c5 commit 0b021f7
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void cancel() {
private static final CharSequence TEST_DATA = newLargePayload();

@RegisterExtension
final ExecutorExtension<Executor> execRule = ExecutorExtension.withCachedExecutor();
static final ExecutorExtension<Executor> execRule = ExecutorExtension.withCachedExecutor().setClassLevel(true);

@Mock
private HttpServiceContext ctx;
Expand Down Expand Up @@ -224,8 +224,13 @@ private void testCancelResponseSingle(final StreamingHttpRequest req,
jerseyRouter.handle(ctx, req, HTTP_REQ_RES_FACTORY)
).flatMap(identity())
.beforeOnError((err) -> {
// If subscription cancellation arrives after the resource started to sleep, the cancellation
// will interrupt the sleep which is OK.
boolean sleepInterrupted = err instanceof InterruptedException && err.getMessage()
.contains("sleep interrupted");

// Ignore racy cancellation, it's ordered safely.
if (!(err instanceof IllegalStateException)) {
if (!(err instanceof IllegalStateException || sleepInterrupted)) {
errorRef.compareAndSet(null, err);
}
})
Expand All @@ -249,8 +254,13 @@ public void onSuccess(@Nullable final StreamingHttpResponse result) {

@Override
public void onError(final Throwable t) {
// If subscription cancellation arrives after the resource started to sleep, the cancellation
// will interrupt the sleep which is OK.
boolean sleepInterrupted = t instanceof InterruptedException && t.getMessage()
.contains("sleep interrupted");

// Ignore racy cancellation, it's ordered safely.
if (!(t instanceof IllegalStateException)) {
if (!(t instanceof IllegalStateException || sleepInterrupted)) {
errorRef.compareAndSet(null, t);
}
cancelledLatch.countDown();
Expand Down

0 comments on commit 0b021f7

Please sign in to comment.