|
21 | 21 | import org.junit.jupiter.api.Test;
|
22 | 22 |
|
23 | 23 | import java.util.ArrayList;
|
| 24 | +import java.util.Collection; |
24 | 25 | import java.util.List;
|
| 26 | +import java.util.Map; |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
25 | 29 | import java.util.function.Consumer;
|
26 | 30 | import java.util.stream.Stream;
|
27 | 31 |
|
@@ -131,49 +135,54 @@ private List<List<String>> splitToBatchesOf(int batchSize, Stream<String> stream
|
131 | 135 | }
|
132 | 136 |
|
133 | 137 | @Nested
|
134 |
| - @DisplayName("Split a stream into batches partitioning to multiple lists") |
135 |
| - class SplitAStreamPartitioningToLists { |
| 138 | + @DisplayName("Process streamed batches in parallel") |
| 139 | + class ParallelBatches { |
136 | 140 |
|
137 | 141 | @Test
|
138 | 142 | void shouldSplitIntoTwoFullBatches() {
|
139 |
| - assertThat(SplitIntoBatches.toListsOfSize(2, Stream.of("A", "B", "C", "D"))) |
140 |
| - .containsExactly(List.of("A", "B"), List.of("C", "D")); |
| 143 | + assertThat(consumeParallelBatchesOf(2, Stream.of("A", "B", "C", "D"))) |
| 144 | + .containsExactlyInAnyOrder("A", "B", "C", "D"); |
141 | 145 | }
|
142 | 146 |
|
143 | 147 | @Test
|
144 | 148 | void shouldSplitIntoOneFullBatchAndOnePartialBatchLeftOver() {
|
145 |
| - assertThat(SplitIntoBatches.toListsOfSize(2, Stream.of("A", "B", "C"))) |
146 |
| - .containsExactly(List.of("A", "B"), List.of("C")); |
| 149 | + assertThat(consumeParallelBatchesOf(2, Stream.of("A", "B", "C"))) |
| 150 | + .containsExactlyInAnyOrder("A", "B", "C"); |
147 | 151 | }
|
148 | 152 |
|
149 | 153 | @Test
|
150 | 154 | void shouldSplitIntoOneFullBatch() {
|
151 |
| - assertThat(SplitIntoBatches.toListsOfSize(3, Stream.of("A", "B", "C"))) |
152 |
| - .containsExactly(List.of("A", "B", "C")); |
| 155 | + assertThat(consumeParallelBatchesOf(3, Stream.of("A", "B", "C"))) |
| 156 | + .containsExactlyInAnyOrder("A", "B", "C"); |
153 | 157 | }
|
154 | 158 |
|
155 | 159 | @Test
|
156 | 160 | void shouldSplitIntoOnePartialBatch() {
|
157 |
| - assertThat(SplitIntoBatches.toListsOfSize(3, Stream.of("A", "B"))) |
158 |
| - .containsExactly(List.of("A", "B")); |
| 161 | + assertThat(consumeParallelBatchesOf(3, Stream.of("A", "B"))) |
| 162 | + .containsExactlyInAnyOrder("A", "B"); |
159 | 163 | }
|
160 | 164 |
|
161 | 165 | @Test
|
162 | 166 | void shouldSplitEmptyStreamToNoBatches() {
|
163 |
| - assertThat(SplitIntoBatches.toListsOfSize(3, Stream.of())) |
| 167 | + assertThat(consumeParallelBatchesOf(3, Stream.of())) |
164 | 168 | .isEmpty();
|
165 | 169 | }
|
166 | 170 |
|
167 | 171 | @Test
|
168 | 172 | void shouldFailWithBatchSizeLowerThanOne() {
|
169 |
| - assertThatThrownBy(() -> SplitIntoBatches.toListsOfSize(0, Stream.of("A", "B"))) |
| 173 | + Consumer<List<String>> notInvoked = batch -> { |
| 174 | + throw new IllegalStateException("Did not expect operation to be called"); |
| 175 | + }; |
| 176 | + assertThatThrownBy(() -> SplitIntoBatches.inParallelBatchesOf(0, Stream.of("A", "B"), notInvoked)) |
170 | 177 | .isInstanceOf(IllegalArgumentException.class);
|
171 | 178 | }
|
172 | 179 |
|
173 |
| - @Test |
174 |
| - void shouldFailWithParallelStream() { |
175 |
| - assertThatThrownBy(() -> SplitIntoBatches.toListsOfSize(0, Stream.of("A", "B").parallel())) |
176 |
| - .isInstanceOf(IllegalArgumentException.class); |
| 180 | + private Collection<String> consumeParallelBatchesOf(int batchSize, Stream<String> stream) { |
| 181 | + Map<String, String> output = new ConcurrentHashMap<>(); |
| 182 | + SplitIntoBatches.inParallelBatchesOf(batchSize, stream, batch -> { |
| 183 | + batch.forEach(value -> output.put(UUID.randomUUID().toString(), value)); |
| 184 | + }); |
| 185 | + return output.values(); |
177 | 186 | }
|
178 | 187 | }
|
179 | 188 | }
|
0 commit comments