Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AsyncTest execute faster 🚀, and trick 👻 Sonar again #1024

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/test/java/org/kiwiproject/concurrent/AsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.kiwiproject.base.DefaultEnvironment;
import org.kiwiproject.base.KiwiEnvironment;
import org.kiwiproject.concurrent.Async.Mode;

import java.time.Duration;
Expand All @@ -29,6 +31,8 @@

class AsyncTest {

private static final KiwiEnvironment ENV = new DefaultEnvironment();

@AfterEach
void tearDown() {
Async.setUnitTestAsyncMode(Mode.ENABLED);
Expand Down Expand Up @@ -276,9 +280,9 @@ void shouldThrowAsyncException_WhenTimesOut_BeforeTheFutureCompletes() {
var task = new ConcurrentTask();
CompletableFuture<Integer> future = Async.doAsync(task::supply);

assertThatThrownBy(() -> Async.waitFor(future, 5, TimeUnit.MILLISECONDS))
assertThatThrownBy(() -> Async.waitFor(future, 1, TimeUnit.MILLISECONDS))
.isExactlyInstanceOf(AsyncException.class)
.hasMessage("TimeoutException occurred (maximum wait was specified as 5 MILLISECONDS)")
.hasMessage("TimeoutException occurred (maximum wait was specified as 1 MILLISECONDS)")
.hasCauseInstanceOf(TimeoutException.class);

assertThat(task.getCurrentCount()).isZero();
Expand Down Expand Up @@ -323,9 +327,9 @@ void shouldThrowAsyncException_WhenTimesOut_BeforeAllFuturesComplete() {
CompletableFuture<Integer> future3 = Async.doAsync(task3::supply);

var futures = List.of(future1, future2, future3);
assertThatThrownBy(() -> Async.waitForAll(futures, 5, TimeUnit.MILLISECONDS))
assertThatThrownBy(() -> Async.waitForAll(futures, 1, TimeUnit.MILLISECONDS))
.isExactlyInstanceOf(AsyncException.class)
.hasMessage("TimeoutException occurred (maximum wait was specified as 5 MILLISECONDS)")
.hasMessage("TimeoutException occurred (maximum wait was specified as 1 MILLISECONDS)")
.hasCauseInstanceOf(TimeoutException.class);

assertThat(task1.getCurrentCount()).isZero();
Expand Down Expand Up @@ -374,9 +378,9 @@ void shouldThrowAsyncException_WhenTimesOut_BeforeAllFuturesComplete() {
CompletableFuture future3 = Async.doAsync(task3::supply);

var futures = List.of(future1, future2, future3);
assertThatThrownBy(() -> Async.waitForAllIgnoringType(futures, 5, TimeUnit.MILLISECONDS))
assertThatThrownBy(() -> Async.waitForAllIgnoringType(futures, 1, TimeUnit.MILLISECONDS))
.isExactlyInstanceOf(AsyncException.class)
.hasMessage("TimeoutException occurred (maximum wait was specified as 5 MILLISECONDS)")
.hasMessage("TimeoutException occurred (maximum wait was specified as 1 MILLISECONDS)")
.hasCauseInstanceOf(TimeoutException.class);

assertThat(task1.getCurrentCount()).isZero();
Expand Down Expand Up @@ -437,7 +441,7 @@ static class ConcurrentTask {
private RuntimeException exceptionToThrow;

ConcurrentTask() {
this(Duration.ofMillis(100));
this(Duration.ofMillis(10));
}

ConcurrentTask(Duration delay) {
Expand Down Expand Up @@ -485,7 +489,7 @@ Integer getCurrentCount() {
}

private void performWait() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(delayMillis);
ENV.sleep(delayMillis);
}
}
}