Avoid race between container starting and getting mapped port
This commit is contained in:
parent
f6cc1cbd76
commit
c9f04c3977
@ -38,17 +38,18 @@ public class CassandraContainer extends Container {
|
||||
private static final int PORT = 9042;
|
||||
|
||||
public CassandraContainer() {
|
||||
super("cassandra:3.11.1", PORT, (container) -> container
|
||||
.waitingFor(new WaitStrategy(container.getMappedPort(PORT)))
|
||||
.withStartupAttempts(3).withStartupTimeout(Duration.ofSeconds(60)));
|
||||
super("cassandra:3.11.1", PORT,
|
||||
(container) -> container.waitingFor(new WaitStrategy(container))
|
||||
.withStartupAttempts(3)
|
||||
.withStartupTimeout(Duration.ofSeconds(60)));
|
||||
}
|
||||
|
||||
private static final class WaitStrategy extends HostPortWaitStrategy {
|
||||
|
||||
private final int port;
|
||||
private final GenericContainer<?> container;
|
||||
|
||||
private WaitStrategy(int port) {
|
||||
this.port = port;
|
||||
private WaitStrategy(GenericContainer<?> container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,7 +67,8 @@ public class CassandraContainer extends Container {
|
||||
|
||||
private Callable<Boolean> checkConnection() {
|
||||
return () -> {
|
||||
try (Cluster cluster = Cluster.builder().withPort(this.port)
|
||||
try (Cluster cluster = Cluster.builder()
|
||||
.withPort(this.container.getMappedPort(CassandraContainer.PORT))
|
||||
.addContactPoint("localhost").build()) {
|
||||
cluster.connect();
|
||||
return true;
|
||||
|
@ -37,25 +37,25 @@ public class Neo4jContainer extends Container {
|
||||
private static final int PORT = 7687;
|
||||
|
||||
public Neo4jContainer() {
|
||||
super("neo4j:3.3.1", PORT,
|
||||
(container) -> container
|
||||
.waitingFor(new WaitStrategy(container.getMappedPort(PORT)))
|
||||
.withEnv("NEO4J_AUTH", "none"));
|
||||
super("neo4j:3.3.1", PORT, (container) -> container
|
||||
.waitingFor(new WaitStrategy(container)).withEnv("NEO4J_AUTH", "none"));
|
||||
}
|
||||
|
||||
private static final class WaitStrategy extends HostPortWaitStrategy {
|
||||
|
||||
private final int port;
|
||||
private final GenericContainer<?> container;
|
||||
|
||||
private WaitStrategy(int port) {
|
||||
this.port = port;
|
||||
private WaitStrategy(GenericContainer<?> container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitUntilReady() {
|
||||
super.waitUntilReady();
|
||||
Configuration configuration = new Configuration.Builder()
|
||||
.uri("bolt://localhost:" + this.port).build();
|
||||
.uri("bolt://localhost:"
|
||||
+ this.container.getMappedPort(Neo4jContainer.PORT))
|
||||
.build();
|
||||
SessionFactory sessionFactory = new SessionFactory(configuration,
|
||||
"org.springframework.boot.test.autoconfigure.data.neo4j");
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user