Skip to content

Commit e8aeec7

Browse files
authored
Rewrite invalid parallelism tests (#942)
1 parent 2d5eaad commit e8aeec7

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

src/test/java/com/pivovarit/collectors/FunctionalTest.java

+15-31
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ Stream<DynamicTest> collectors() {
6868
virtualThreadsTests((m, e, p) -> adapt(parallel(m)), "ParallelCollectors.parallel() [virtual]"),
6969
virtualThreadsTests((m, e, p) -> adapt(parallel(m, p)), "ParallelCollectors.parallel() [virtual]"),
7070
// platform threads
71-
tests((m, e, p) -> parallel(m, toList(), e, p), format("ParallelCollectors.parallel(toList(), p=%d)", PARALLELISM), true),
72-
tests((m, e, p) -> parallel(m, toSet(), e, p), format("ParallelCollectors.parallel(toSet(), p=%d)", PARALLELISM), true),
73-
tests((m, e, p) -> parallel(m, toList(), e), "ParallelCollectors.parallel(toList(), p=inf)", false),
74-
tests((m, e, p) -> parallel(m, toSet(), e), "ParallelCollectors.parallel(toSet(), p=inf)", false),
75-
tests((m, e, p) -> parallel(m, toCollection(LinkedList::new), e, p), format("ParallelCollectors.parallel(toCollection(), p=%d)", PARALLELISM), true),
76-
tests((m, e, p) -> adapt(parallel(m, e, p)), format("ParallelCollectors.parallel(p=%d)", PARALLELISM), true),
71+
tests((m, e, p) -> parallel(m, toList(), e, p), format("ParallelCollectors.parallel(toList(), p=%d)", PARALLELISM)),
72+
tests((m, e, p) -> parallel(m, toSet(), e, p), format("ParallelCollectors.parallel(toSet(), p=%d)", PARALLELISM)),
73+
tests((m, e, p) -> parallel(m, toList(), e), "ParallelCollectors.parallel(toList(), p=inf)"),
74+
tests((m, e, p) -> parallel(m, toSet(), e), "ParallelCollectors.parallel(toSet(), p=inf)"),
75+
tests((m, e, p) -> parallel(m, toCollection(LinkedList::new), e, p), format("ParallelCollectors.parallel(toCollection(), p=%d)", PARALLELISM)),
76+
tests((m, e, p) -> adapt(parallel(m, e, p)), format("ParallelCollectors.parallel(p=%d)", PARALLELISM)),
7777
// platform threads, with batching
78-
batchTests((m, e, p) -> Batching.parallel(m, toList(), e, p), format("ParallelCollectors.Batching.parallel(toList(), p=%d)", PARALLELISM), true),
79-
batchTests((m, e, p) -> Batching.parallel(m, toSet(), e, p), format("ParallelCollectors.Batching.parallel(toSet(), p=%d)", PARALLELISM), true),
80-
batchTests((m, e, p) -> Batching.parallel(m, toCollection(LinkedList::new), e, p), format("ParallelCollectors.Batching.parallel(toCollection(), p=%d)", PARALLELISM), true),
81-
batchTests((m, e, p) -> adapt(Batching.parallel(m, e, p)), format("ParallelCollectors.Batching.parallel(p=%d)", PARALLELISM), true)
78+
batchTests((m, e, p) -> Batching.parallel(m, toList(), e, p), format("ParallelCollectors.Batching.parallel(toList(), p=%d)", PARALLELISM)),
79+
batchTests((m, e, p) -> Batching.parallel(m, toSet(), e, p), format("ParallelCollectors.Batching.parallel(toSet(), p=%d)", PARALLELISM)),
80+
batchTests((m, e, p) -> Batching.parallel(m, toCollection(LinkedList::new), e, p), format("ParallelCollectors.Batching.parallel(toCollection(), p=%d)", PARALLELISM)),
81+
batchTests((m, e, p) -> adapt(Batching.parallel(m, e, p)), format("ParallelCollectors.Batching.parallel(p=%d)", PARALLELISM))
8282
).flatMap(i -> i);
8383
}
8484

@@ -167,16 +167,12 @@ private static <R extends Collection<Integer>> Stream<DynamicTest> virtualThread
167167
);
168168
}
169169

170-
private static <R extends Collection<Integer>> Stream<DynamicTest> tests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name, boolean limitedParallelism) {
171-
var tests = of(
170+
private static <R extends Collection<Integer>> Stream<DynamicTest> tests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
171+
return of(
172172
shouldStartConsumingImmediately(collector, name),
173173
shouldShortCircuitOnException(collector, name),
174174
shouldInterruptOnException(collector, name)
175175
);
176-
177-
tests = limitedParallelism ? of(shouldRejectInvalidParallelism(collector, name)) : tests;
178-
179-
return tests;
180176
}
181177

182178
private static <R extends Collection<Integer>> Stream<DynamicTest> virtualThreadsStreamingTests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
@@ -190,15 +186,12 @@ private static <R extends Collection<Integer>> Stream<DynamicTest> streamingTest
190186
return of(
191187
shouldStartConsumingImmediately(collector, name),
192188
shouldPushElementsToStreamAsSoonAsPossible(collector, name),
193-
shouldShortCircuitOnException(collector, name),
194-
shouldRejectInvalidParallelism(collector, name)
189+
shouldShortCircuitOnException(collector, name)
195190
);
196191
}
197192

198-
private static <R extends Collection<Integer>> Stream<DynamicTest> batchTests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name, boolean limitedParallelism) {
199-
return Stream.concat(
200-
tests(collector, name, limitedParallelism),
201-
of(shouldProcessOnNThreadsETParallelism(collector, name)));
193+
private static <R extends Collection<Integer>> Stream<DynamicTest> batchTests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
194+
return Stream.concat(tests(collector, name), of(shouldProcessOnNThreadsETParallelism(collector, name)));
202195
}
203196

204197
private static <R extends Collection<Integer>> Stream<DynamicTest> batchStreamingTests(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
@@ -263,15 +256,6 @@ private static <R extends Collection<Integer>> DynamicTest shouldShortCircuitOnE
263256
});
264257
}
265258

266-
private static <R extends Collection<Integer>> DynamicTest shouldRejectInvalidParallelism(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
267-
return dynamicTest(format("%s: should reject invalid parallelism", name), () -> {
268-
withExecutor(e -> {
269-
assertThatThrownBy(() -> collector.apply(i -> i, e, -1))
270-
.isExactlyInstanceOf(IllegalArgumentException.class);
271-
});
272-
});
273-
}
274-
275259
private static <R extends Collection<Integer>> DynamicTest shouldStartConsumingImmediately(CollectorSupplier<Function<Integer, Integer>, Executor, Integer, Collector<Integer, ?, CompletableFuture<R>>> collector, String name) {
276260
return dynamicTest(format("%s: should start consuming immediately", name), () -> {
277261
try (var e = Executors.newCachedThreadPool()) {

src/test/java/com/pivovarit/collectors/test/BasicParallelismTest.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.pivovarit.collectors.ParallelCollectors;
44
import com.pivovarit.collectors.TestUtils;
5-
import org.assertj.core.api.Assertions;
65
import org.junit.jupiter.api.DynamicTest;
76
import org.junit.jupiter.api.TestFactory;
87

@@ -20,6 +19,7 @@
2019
import static com.pivovarit.collectors.test.BasicParallelismTest.CollectorDefinition.collector;
2120
import static java.util.stream.Collectors.collectingAndThen;
2221
import static java.util.stream.Collectors.toList;
22+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2323
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
2424

2525
class BasicParallelismTest {
@@ -61,11 +61,22 @@ Stream<DynamicTest> shouldProcessAllElementsWithMaxParallelism() {
6161
Stream<DynamicTest> shouldRespectMaxParallelism() {
6262
return allBounded()
6363
.map(c -> DynamicTest.dynamicTest(c.name(), () -> {
64-
var duration = timed(() -> IntStream.range(0, 10).boxed().collect(c.factory().collector(i -> TestUtils.returnWithDelay(i, Duration.ofMillis(100)), 2)));
64+
var duration = timed(() -> IntStream.range(0, 10).boxed()
65+
.collect(c.factory().collector(i -> TestUtils.returnWithDelay(i, Duration.ofMillis(100)), 2)));
6566
assertThat(duration).isCloseTo(Duration.ofMillis(500), Duration.ofMillis(100));
6667
}));
6768
}
6869

70+
@TestFactory
71+
Stream<DynamicTest> shouldRejectInvalidParallelism() {
72+
return allBounded()
73+
.flatMap(c -> Stream.of(-1, 0)
74+
.map(p -> DynamicTest.dynamicTest("%s [p=%d]".formatted(c.name(), p), () -> {
75+
assertThatThrownBy(() -> Stream.of(1).collect(c.factory().collector(i -> i, p)))
76+
.isExactlyInstanceOf(IllegalArgumentException.class);
77+
})));
78+
}
79+
6980
protected record CollectorDefinition<T, R>(String name, CollectorFactory<T, R> factory) {
7081
static <T, R> CollectorDefinition<T, R> collector(String name, CollectorFactory<T, R> collector) {
7182
return new CollectorDefinition<>(name, collector);

0 commit comments

Comments
 (0)