Skip to content

Commit

Permalink
Rewrite ExecutorPollutionTest to utilize TestFactory (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Sep 15, 2024
1 parent a1f4f69 commit bc1fb1b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 120 deletions.
33 changes: 33 additions & 0 deletions src/test/java/com/pivovarit/collectors/Collectors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pivovarit.collectors;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;

import static com.pivovarit.collectors.ParallelCollectors.Batching.parallel;
import static java.util.stream.Collectors.toList;

public final class Collectors {

private Collectors() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static Stream<Map.Entry<String, BoundedCollectorFactory<Integer>>> boundedCollectors() {
return Stream.of(
Map.entry("parallel()", (f, e, p) -> ParallelCollectors.parallel(f, e, p)),
Map.entry("parallel(toList())", (f, e, p) -> ParallelCollectors.parallel(f, toList(), e, p)),
Map.entry("parallelToStream()", (f, e, p) -> ParallelCollectors.parallelToStream(f, e, p)),
Map.entry("parallelToOrderedStream()", (f, e, p) -> ParallelCollectors.parallelToOrderedStream(f, e, p)),
Map.entry("parallel() (batching)", (f, e, p) -> parallel(f, e, p)),
Map.entry("parallel(toList()) (batching)", (f, e, p) -> parallel(f, toList(), e, p)),
Map.entry("parallelToStream() (batching)", (f, e, p) -> ParallelCollectors.Batching.parallelToStream(f, e, p)),
Map.entry("parallelToOrderedStream() (batching)", (f, e, p) -> ParallelCollectors.Batching.parallelToOrderedStream(f, e, p)));
}

public interface BoundedCollectorFactory<T> {
Collector<T, ?, ?> apply(Function<T, ?> function, Executor executorService, int parallelism);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.pivovarit.collectors.test;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static com.pivovarit.collectors.Collectors.boundedCollectors;

class ExecutorPollutionTest {

@TestFactory
Stream<DynamicTest> shouldNotPolluteExecutorFactory() {
return boundedCollectors().map(e -> DynamicTest.dynamicTest(e.getKey(),
() -> {
try (var e1 = warmedUp(new ThreadPoolExecutor(1, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2)))) {

var result = Stream.generate(() -> 42)
.limit(1000)
.collect(e.getValue().apply(i -> i, e1, 1));

switch (result) {
case CompletableFuture<?> cf -> cf.join();
case Stream<?> s -> s.forEach((__) -> {});
default -> throw new IllegalStateException("can't happen");
}
}
}));
}

@TestFactory
Stream<DynamicTest> shouldNotPolluteExecutorFactoryLimitedParallelism() {
return boundedCollectors().map(e -> DynamicTest.dynamicTest(e.getKey(), () -> {
try (var e1 = warmedUp(new ThreadPoolExecutor(1, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2)))) {

var result = Stream.generate(() -> 42)
.limit(1000)
.collect(e.getValue().apply(i -> i, e1, 2));

switch (result) {
case CompletableFuture<?> cf -> cf.join();
case Stream<?> s -> s.forEach((__) -> {});
default -> throw new IllegalStateException("can't happen");
}
}
}));
}

private static ThreadPoolExecutor warmedUp(ThreadPoolExecutor e) {
for (int i = 0; i < e.getCorePoolSize(); i++) {
CompletableFuture.runAsync(() -> {}).join();
}
return e;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pivovarit.collectors.functional;
package com.pivovarit.collectors.test;

import com.pivovarit.collectors.ParallelCollectors;
import org.junit.jupiter.api.DynamicTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pivovarit.collectors.functional;
package com.pivovarit.collectors.test;

import com.pivovarit.collectors.ParallelCollectors;
import org.junit.jupiter.api.DynamicTest;
Expand Down

0 comments on commit bc1fb1b

Please sign in to comment.