Skip to content

Commit

Permalink
Expand Container dependsOn parameters to accept Iterable (#2259)
Browse files Browse the repository at this point in the history
* Expand `dependsOn` parameters to accept `Iterable`
* Reinstate a `dependsOn(List)` method, for binary backwards compatibility
* Reinstate `deepStart(Collection<? extends Startable>)` method
  • Loading branch information
rnorth committed Apr 5, 2020
1 parent 08bafcf commit a09f4d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Expand Up @@ -26,9 +26,6 @@
import lombok.NonNull;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -42,7 +39,6 @@
import org.slf4j.Logger;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.UnstableAPI;
import org.testcontainers.images.ImagePullPolicy;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.startupcheck.IsRunningStartupCheckStrategy;
import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy;
Expand All @@ -51,8 +47,8 @@
import org.testcontainers.containers.wait.Wait;
import org.testcontainers.containers.wait.WaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;
import org.testcontainers.images.ImagePullPolicy;
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.lifecycle.Startable;
import org.testcontainers.lifecycle.Startables;
import org.testcontainers.lifecycle.TestDescription;
Expand All @@ -64,14 +60,9 @@
import org.testcontainers.utility.PathUtils;
import org.testcontainers.utility.ResourceReaper;
import org.testcontainers.utility.TestcontainersConfiguration;
import org.testcontainers.utility.ThrowingFunction;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
Expand All @@ -82,7 +73,6 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -266,22 +256,29 @@ public void setImage(Future<String> image) {
}

/**
* @see #dependsOn(List)
* @see #dependsOn(Iterable)
*/
public SELF dependsOn(Startable... startables) {
Collections.addAll(dependencies, startables);
return self();
}

/**
* @see #dependsOn(Iterable)
*/
public SELF dependsOn(List<? extends Startable> startables) {
return this.dependsOn((Iterable<? extends Startable>) startables);
}

/**
* Delays this container's creation and start until provided {@link Startable}s start first.
* Note that the circular dependencies are not supported.
*
* @param startables a list of {@link Startable} to depend on
* @see Startables#deepStart(Collection)
* @see Startables#deepStart(Iterable)
*/
public SELF dependsOn(List<Startable> startables) {
dependencies.addAll(startables);
public SELF dependsOn(Iterable<? extends Startable> startables) {
startables.forEach(dependencies::add);
return self();
}

Expand Down
15 changes: 13 additions & 2 deletions core/src/main/java/org/testcontainers/lifecycle/Startables.java
Expand Up @@ -5,9 +5,13 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@UtilityClass
public class Startables {
Expand All @@ -28,7 +32,14 @@ public Thread newThread(Runnable r) {
* @see #deepStart(Stream)
*/
public CompletableFuture<Void> deepStart(Collection<? extends Startable> startables) {
return deepStart(startables.stream());
return deepStart((Iterable<? extends Startable>) startables);
}

/**
* @see #deepStart(Stream)
*/
public CompletableFuture<Void> deepStart(Iterable<? extends Startable> startables) {
return deepStart(StreamSupport.stream(startables.spliterator(), false));
}

/**
Expand Down

0 comments on commit a09f4d5

Please sign in to comment.