BAEL-6579: fixing tests
This commit is contained in:
parent
dbbd2a0a12
commit
09c62a2d70
@ -1,8 +1,10 @@
|
|||||||
package com.baeldung.concurrent.completablefuture.threadpool;
|
package com.baeldung.concurrent.completablefuture.threadpool;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.fail;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@ -11,7 +13,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
public class CompletableFutureThreadPoolUnitTest {
|
public class CompletableFutureThreadPoolUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUsingNonAsync_thenUsesMainThread() {
|
void whenUsingNonAsync_thenUsesMainThread() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
||||||
|
|
||||||
CompletableFuture<Integer> nameLength = name.thenApply(value -> {
|
CompletableFuture<Integer> nameLength = name.thenApply(value -> {
|
||||||
@ -19,7 +21,7 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
return value.length();
|
return value.length();
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThat(nameLength).isCompletedWithValue(8);
|
assertThat(nameLength.get()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -32,7 +34,11 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
return value.length();
|
return value.length();
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThat(nameLength).isCompletedWithValue(8);
|
try {
|
||||||
|
assertThat(nameLength.get()).isEqualTo(8);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
new Thread(test, "test-thread").start();
|
new Thread(test, "test-thread").start();
|
||||||
@ -40,7 +46,7 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUsingAsync_thenUsesCommonPool() {
|
void whenUsingAsync_thenUsesCommonPool() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
||||||
|
|
||||||
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
|
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
|
||||||
@ -48,11 +54,11 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
return value.length();
|
return value.length();
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThat(nameLength).isCompletedWithValue(8);
|
assertThat(nameLength.get()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUsingAsync_thenUsesCustomExecutor() {
|
void whenUsingAsync_thenUsesCustomExecutor() throws ExecutionException, InterruptedException {
|
||||||
Executor testExecutor = Executors.newFixedThreadPool(5);
|
Executor testExecutor = Executors.newFixedThreadPool(5);
|
||||||
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
|
||||||
|
|
||||||
@ -61,11 +67,11 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
return value.length();
|
return value.length();
|
||||||
}, testExecutor);
|
}, testExecutor);
|
||||||
|
|
||||||
assertThat(nameLength).isCompletedWithValue(8);
|
assertThat(nameLength.get()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenOverridingDefaultThreadPool_thenUsesCustomExecutor() {
|
void whenOverridingDefaultThreadPool_thenUsesCustomExecutor() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<String> name = CustomCompletableFuture.supplyAsync(() -> "Baeldung");
|
CompletableFuture<String> name = CustomCompletableFuture.supplyAsync(() -> "Baeldung");
|
||||||
|
|
||||||
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
|
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
|
||||||
@ -73,7 +79,7 @@ public class CompletableFutureThreadPoolUnitTest {
|
|||||||
return value.length();
|
return value.length();
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThat(nameLength).isCompletedWithValue(8);
|
assertThat(nameLength.get()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printCurrentThread() {
|
private static void printCurrentThread() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user