|
4 | 4 | import static org.assertj.core.api.Assertions.assertThat;
|
5 | 5 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
6 | 6 |
|
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
7 | 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | +import java.util.concurrent.CountDownLatch; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
8 | 14 |
|
9 | 15 | import org.junit.jupiter.api.BeforeEach;
|
10 | 16 | import org.junit.jupiter.api.Test;
|
|
16 | 22 | * Unit tests for {@link Futures}.
|
17 | 23 | *
|
18 | 24 | * @author Mark Paluch
|
| 25 | + * @author Tihomir Mateev |
19 | 26 | */
|
20 | 27 | class FuturesUnitTests {
|
21 | 28 |
|
@@ -56,4 +63,33 @@ void awaitAllShouldSetInterruptedBit() {
|
56 | 63 | assertThat(Thread.currentThread().isInterrupted()).isTrue();
|
57 | 64 | }
|
58 | 65 |
|
| 66 | + @Test |
| 67 | + void allOfShouldNotThrow() throws InterruptedException { |
| 68 | + int threadCount = 100; |
| 69 | + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); |
| 70 | + List<Throwable> issues = new ArrayList<>(); |
| 71 | + List<CompletableFuture<Void>> futures = Collections.synchronizedList(new ArrayList<>()); |
| 72 | + // Submit multiple threads to perform concurrent operations |
| 73 | + CountDownLatch latch = new CountDownLatch(threadCount); |
| 74 | + for (int i = 0; i < threadCount; i++) { |
| 75 | + executorService.submit(() -> { |
| 76 | + try { |
| 77 | + for (int y = 0; y < 1000; y++) { |
| 78 | + futures.add(new CompletableFuture<>()); |
| 79 | + } |
| 80 | + |
| 81 | + Futures.allOf(futures); |
| 82 | + } catch (Exception e) { |
| 83 | + issues.add(e); |
| 84 | + } finally { |
| 85 | + latch.countDown(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + // wait for all threads to complete |
| 91 | + latch.await(); |
| 92 | + assertThat(issues).doesNotHaveAnyElementsOfTypes(ArrayIndexOutOfBoundsException.class); |
| 93 | + } |
| 94 | + |
59 | 95 | }
|
0 commit comments