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;
|
private static final int PORT = 9042;
|
||||||
|
|
||||||
public CassandraContainer() {
|
public CassandraContainer() {
|
||||||
super("cassandra:3.11.1", PORT, (container) -> container
|
super("cassandra:3.11.1", PORT,
|
||||||
.waitingFor(new WaitStrategy(container.getMappedPort(PORT)))
|
(container) -> container.waitingFor(new WaitStrategy(container))
|
||||||
.withStartupAttempts(3).withStartupTimeout(Duration.ofSeconds(60)));
|
.withStartupAttempts(3)
|
||||||
|
.withStartupTimeout(Duration.ofSeconds(60)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class WaitStrategy extends HostPortWaitStrategy {
|
private static final class WaitStrategy extends HostPortWaitStrategy {
|
||||||
|
|
||||||
private final int port;
|
private final GenericContainer<?> container;
|
||||||
|
|
||||||
private WaitStrategy(int port) {
|
private WaitStrategy(GenericContainer<?> container) {
|
||||||
this.port = port;
|
this.container = container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -66,7 +67,8 @@ public class CassandraContainer extends Container {
|
||||||
|
|
||||||
private Callable<Boolean> checkConnection() {
|
private Callable<Boolean> checkConnection() {
|
||||||
return () -> {
|
return () -> {
|
||||||
try (Cluster cluster = Cluster.builder().withPort(this.port)
|
try (Cluster cluster = Cluster.builder()
|
||||||
|
.withPort(this.container.getMappedPort(CassandraContainer.PORT))
|
||||||
.addContactPoint("localhost").build()) {
|
.addContactPoint("localhost").build()) {
|
||||||
cluster.connect();
|
cluster.connect();
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -37,25 +37,25 @@ public class Neo4jContainer extends Container {
|
||||||
private static final int PORT = 7687;
|
private static final int PORT = 7687;
|
||||||
|
|
||||||
public Neo4jContainer() {
|
public Neo4jContainer() {
|
||||||
super("neo4j:3.3.1", PORT,
|
super("neo4j:3.3.1", PORT, (container) -> container
|
||||||
(container) -> container
|
.waitingFor(new WaitStrategy(container)).withEnv("NEO4J_AUTH", "none"));
|
||||||
.waitingFor(new WaitStrategy(container.getMappedPort(PORT)))
|
|
||||||
.withEnv("NEO4J_AUTH", "none"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class WaitStrategy extends HostPortWaitStrategy {
|
private static final class WaitStrategy extends HostPortWaitStrategy {
|
||||||
|
|
||||||
private final int port;
|
private final GenericContainer<?> container;
|
||||||
|
|
||||||
private WaitStrategy(int port) {
|
private WaitStrategy(GenericContainer<?> container) {
|
||||||
this.port = port;
|
this.container = container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void waitUntilReady() {
|
public void waitUntilReady() {
|
||||||
super.waitUntilReady();
|
super.waitUntilReady();
|
||||||
Configuration configuration = new Configuration.Builder()
|
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,
|
SessionFactory sessionFactory = new SessionFactory(configuration,
|
||||||
"org.springframework.boot.test.autoconfigure.data.neo4j");
|
"org.springframework.boot.test.autoconfigure.data.neo4j");
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue