Skip to content

Commit

Permalink
Make Startables#deepStart always iterate sequentially (#2053)
Browse files Browse the repository at this point in the history
* Reproduce the issue with passing parallel Stream into Startables#deepStart

* Fix the issue by sanitizing the input stream
  • Loading branch information
pivovarit authored and bsideup committed Nov 11, 2019
1 parent 86bfd78 commit c027751
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public CompletableFuture<Void> deepStart(Stream<Startable> startables) {
*/
private CompletableFuture<Void> deepStart(Map<Startable, CompletableFuture<Void>> started, Stream<Startable> startables) {
CompletableFuture[] futures = startables
.sequential()
.map(it -> {
// avoid a recursive update in `computeIfAbsent`
Map<Startable, CompletableFuture<Void>> subStarted = new HashMap<>(started);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.testcontainers.lifecycle.Startables;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DependenciesTest {
Expand Down Expand Up @@ -117,6 +119,19 @@ public void shouldHandleDiamondDependencies() throws Exception {
VisibleAssertions.assertEquals("D started", 1, d.getStartInvocationCount().intValue());
}

@Test
public void shouldHandleParallelStream() throws Exception {
List<Startable> startables = Stream.generate(InvocationCountingStartable::new)
.limit(10)
.collect(Collectors.toList());

for (int i = 1; i < startables.size(); i++) {
startables.get(0).getDependencies().add(startables.get(i));
}

Startables.deepStart(startables.parallelStream()).get(1, TimeUnit.SECONDS);
}

private static class InvocationCountingStartable implements Startable {

@Getter
Expand All @@ -131,7 +146,6 @@ private static class InvocationCountingStartable implements Startable {
@Override
public void start() {
startInvocationCount.getAndIncrement();

}

@Override
Expand Down

0 comments on commit c027751

Please sign in to comment.