-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite ExecutorPollutionTest to utilize TestFactory (#927)
- Loading branch information
Showing
5 changed files
with
94 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
118 changes: 0 additions & 118 deletions
118
src/test/java/com/pivovarit/collectors/functional/ExecutorPollutionTest.java
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
src/test/java/com/pivovarit/collectors/test/ExecutorPollutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...rs/functional/ExecutorValidationTest.java → ...llectors/test/ExecutorValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tional/ImmediateStreamProcessingTest.java → ...s/test/ImmediateStreamProcessingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters