-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
102 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
99 changes: 0 additions & 99 deletions
99
src/test/java/com/pivovarit/collectors/FunctionalTest.java
This file was deleted.
Oops, something went wrong.
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
63 changes: 63 additions & 0 deletions
63
src/test/java/com/pivovarit/collectors/test/BatchingTest.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,63 @@ | ||
package com.pivovarit.collectors.test; | ||
|
||
import com.pivovarit.collectors.ParallelCollectors; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentSkipListSet; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
import java.util.function.Function; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Stream; | ||
|
||
import static com.pivovarit.collectors.test.BatchingTest.CollectorDefinition.collector; | ||
import static java.util.stream.Collectors.collectingAndThen; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class BatchingTest { | ||
private static Stream<BatchingTest.CollectorDefinition<Integer, Integer>> allBatching() { | ||
return Stream.of( | ||
collector("parallel(e, p) [batching]", (f, p) -> collectingAndThen(ParallelCollectors.Batching.parallel(f, e(), p), c -> c.thenApply(Stream::toList).join())), | ||
collector("parallel(toList(), e, p) [batching]", (f, p) -> collectingAndThen(ParallelCollectors.Batching.parallel(f, toList(), e(), p), CompletableFuture::join)), | ||
collector("parallelToStream(e, p) [batching]", (f, p) -> collectingAndThen(ParallelCollectors.Batching.parallelToStream(f, e(), p), Stream::toList)), | ||
collector("parallelToOrderedStream(e, p) [batching]", (f, p) -> collectingAndThen(ParallelCollectors.Batching.parallelToOrderedStream(f, e(), p), Stream::toList)) | ||
); | ||
} | ||
|
||
@TestFactory | ||
Stream<DynamicTest> shouldProcessOnExactlyNThreads() { | ||
return allBatching() | ||
.map(c -> DynamicTest.dynamicTest(c.name(), () -> { | ||
var threads = new ConcurrentSkipListSet<>(); | ||
var parallelism = 4; | ||
|
||
Stream.generate(() -> 42) | ||
.limit(100) | ||
.collect(c.collector().collector(i -> { | ||
threads.add(Thread.currentThread().getName()); | ||
return i; | ||
}, parallelism)); | ||
|
||
assertThat(threads).hasSizeLessThanOrEqualTo(parallelism); | ||
})); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface CollectorFactory<T, R> { | ||
Collector<T, ?, List<R>> collector(Function<T, R> f, Integer p); | ||
} | ||
|
||
record CollectorDefinition<T, R>(String name, CollectorFactory<T, R> collector) { | ||
static <T, R> CollectorDefinition<T, R> collector(String name, CollectorFactory<T, R> collector) { | ||
return new CollectorDefinition<>(name, collector); | ||
} | ||
} | ||
|
||
private static Executor e() { | ||
return Executors.newCachedThreadPool(); | ||
} | ||
} |