Skip to content

Commit

Permalink
Handling InterruptedException should throw CompletionException
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Pavlov authored and Konstantin Pavlov committed Jul 17, 2024
1 parent bcb3493 commit b0fc859
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -84,6 +85,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -96,6 +98,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>true</useReleaseProfile>
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/me/kpavlov/await4j/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static <T> T await(Callable<T> block, long millis) {
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted virtual thread", e);
throw new CompletionException("Interrupted virtual thread", e);
}
}

Expand Down Expand Up @@ -145,7 +145,6 @@ public static <T> T await(Future<T> future) {
return await(() -> future.get());
}


/**
* Waits for the completion of a Future and returns its result.
*
Expand Down Expand Up @@ -212,34 +211,33 @@ private static <T> Result<T> callWithErrorHandling(Callable<T> block) {
}
}

@SuppressWarnings("java:S1181")
private static <T> boolean shortCircuitDoneFuture(Future<T> future) {
switch (future.state()) {
case SUCCESS -> {
return true;
}
case FAILED -> {
Throwable throwable = future.exceptionNow();
if (throwable instanceof Error e) {
throw e;
try {
if (future.isDone()) {
if (future.isCancelled()) {
throw new CancellationException("Execution is cancelled");
}
throw toRuntimeException(throwable);
}
case CANCELLED ->
throw new CancellationException("Execution is cancelled");
default ->
{
return false;
future.get();
return true;
}
return false;
} catch (Error e) {
throw e;
} catch (ExecutionException e) {
throw toRuntimeException(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CompletionException("Interrupted while waiting for future", e);
}
}

private static RuntimeException toRuntimeException(Throwable cause) {
if (cause instanceof RuntimeException re) {
// re-throw RuntimeException as it is
return re;
} else {
return new CompletionException("Can't execute async task: exception", cause);
}
return switch (cause) {
case RuntimeException re -> re;
case Error e -> throw e;
default -> new CompletionException("Can't execute async task: exception", cause);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;

import static me.kpavlov.await4j.TestUtils.sleepMillis;
import static me.kpavlov.await4j.TestUtils.sleepOneSecond;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down

0 comments on commit b0fc859

Please sign in to comment.