You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if i have a container that does a task rather than provide a service, and i know it should exit after it is done with its work, is there a method in the testcontainers API that i can call which returns when, or a returns a promise for when, the container will exit?
perhaps something like this?
diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java
index bc65cb22..28f04602 100644
--- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java+++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.InspectContainerResponse;
+import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.ContainerNetwork;
@@ -954,6 +955,27 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
}
}
+ /**+ * waits until a container is stopped and checks if it worked via inspect+ *+ * @param duration how long to wait for the container to exit gracefully+ * @return if the operation was successful+ */+ protected boolean waitUntilContainerStopped(Duration duration) {+ try {+ dockerClient.waitContainerCmd(getContainerId())+ .start()+ .awaitStatusCode(duration.toMillis(), TimeUnit.MILLISECONDS);+ } catch (DockerClientException ignored) {+ }++ try {+ return Boolean.FALSE.equals(dockerClient.inspectContainerCmd(getContainerId()).exec().getState().getRunning());+ } catch (NotFoundException e) {+ return true;+ }+ }+
/**
* {@inheritDoc}
*/
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
if i have a container that does a task rather than provide a service, and i know it should exit after it is done with its work, is there a method in the testcontainers API that i can call which returns when, or a returns a promise for when, the container will exit?
perhaps something like this?
Beta Was this translation helpful? Give feedback.
All reactions