Don't call Startable.start() for already started containers
Add a new `TestcontainersStartup.start` static method and update the existing start methods so that `Startable.start()` is only called when the container is not already running. Prior to this commit, we assumed that `Startable.start()` calls were idempotent and could be safely made multiple times. Whilst this appears to be true for stock `GenericContainer` based startables, users may have their own `start()` method that does not expect to be called multiple times. The implemented detection logic will not be applied if a `Startable` is not also a `Container`. In these cases, the implementation will need to deal directly with multiple `start()` calls. Fixed gh-43253
This commit is contained in:
@@ -97,7 +97,7 @@ class TestcontainersLifecycleBeanPostProcessor
|
||||
}
|
||||
else if (this.startables.get() == Startables.STARTED) {
|
||||
logger.trace(LogMessage.format("Starting container %s", beanName));
|
||||
startableBean.start();
|
||||
TestcontainersStartup.start(startableBean);
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,7 +17,13 @@
|
||||
package org.springframework.boot.testcontainers.lifecycle;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.testcontainers.containers.Container;
|
||||
import org.testcontainers.lifecycle.Startable;
|
||||
import org.testcontainers.lifecycle.Startables;
|
||||
|
||||
@@ -40,7 +46,7 @@ public enum TestcontainersStartup {
|
||||
|
||||
@Override
|
||||
void start(Collection<? extends Startable> startables) {
|
||||
startables.forEach(Startable::start);
|
||||
startables.forEach(TestcontainersStartup::start);
|
||||
}
|
||||
|
||||
},
|
||||
@@ -52,7 +58,8 @@ public enum TestcontainersStartup {
|
||||
|
||||
@Override
|
||||
void start(Collection<? extends Startable> startables) {
|
||||
Startables.deepStart(startables).join();
|
||||
SingleStartables singleStartables = new SingleStartables();
|
||||
Startables.deepStart(startables.stream().map(singleStartables::getOrCreate)).join();
|
||||
}
|
||||
|
||||
};
|
||||
@@ -91,4 +98,69 @@ public enum TestcontainersStartup {
|
||||
return canonicalName.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the given {@link Startable} unless is's detected as already running.
|
||||
* @param startable the startable to start
|
||||
* @since 3.4.1
|
||||
*/
|
||||
public static void start(Startable startable) {
|
||||
if (!isRunning(startable)) {
|
||||
startable.start();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isRunning(Startable startable) {
|
||||
try {
|
||||
return (startable instanceof Container<?> container) && container.isRunning();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks and adapts {@link Startable} instances to use
|
||||
* {@link TestcontainersStartup#start(Startable)} so containers are only started once
|
||||
* even when calling {@link Startables#deepStart(java.util.stream.Stream)}.
|
||||
*/
|
||||
private static final class SingleStartables {
|
||||
|
||||
private final Map<Startable, SingleStartable> adapters = new HashMap<>();
|
||||
|
||||
SingleStartable getOrCreate(Startable startable) {
|
||||
return this.adapters.computeIfAbsent(startable, this::create);
|
||||
}
|
||||
|
||||
private SingleStartable create(Startable startable) {
|
||||
return new SingleStartable(this, startable);
|
||||
}
|
||||
|
||||
record SingleStartable(SingleStartables singleStartables, Startable startable) implements Startable {
|
||||
|
||||
@Override
|
||||
public Set<Startable> getDependencies() {
|
||||
Set<Startable> dependencies = this.startable.getDependencies();
|
||||
if (dependencies.isEmpty()) {
|
||||
return dependencies;
|
||||
}
|
||||
return dependencies.stream()
|
||||
.map(this.singleStartables::getOrCreate)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
TestcontainersStartup.start(this.startable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
this.startable.stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.service.connection.ConnectionDetai
|
||||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory;
|
||||
import org.springframework.boot.origin.Origin;
|
||||
import org.springframework.boot.origin.OriginProvider;
|
||||
import org.springframework.boot.testcontainers.lifecycle.TestcontainersStartup;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.ResolvableType;
|
||||
@@ -188,7 +189,7 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
|
||||
Assert.state(this.container != null,
|
||||
"Container cannot be obtained before the connection details bean has been initialized");
|
||||
if (this.container instanceof Startable startable) {
|
||||
startable.start();
|
||||
TestcontainersStartup.start(startable);
|
||||
}
|
||||
return this.container;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user