diff --git a/test-infra/camel-test-infra-ftp/src/main/java/org/apache/camel/test/infra/ftp/services/embedded/SftpEmbeddedInfraService.java b/test-infra/camel-test-infra-ftp/src/main/java/org/apache/camel/test/infra/ftp/services/embedded/SftpEmbeddedInfraService.java index 5fafd778f1b0e..35b754ddd1cc0 100644 --- a/test-infra/camel-test-infra-ftp/src/main/java/org/apache/camel/test/infra/ftp/services/embedded/SftpEmbeddedInfraService.java +++ b/test-infra/camel-test-infra-ftp/src/main/java/org/apache/camel/test/infra/ftp/services/embedded/SftpEmbeddedInfraService.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.Socket; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -27,6 +28,7 @@ import java.security.PublicKey; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import org.apache.camel.spi.annotations.InfraService; @@ -136,6 +138,23 @@ public void setUpServer() throws Exception { sshd.start(); port = ((InetSocketAddress) sshd.getBoundAddresses().iterator().next()).getPort(); + + waitForServerReady(); + } + + private void waitForServerReady() throws IOException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + IOException lastException = null; + while (System.nanoTime() < deadline) { + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress("localhost", port), 1000); + return; + } catch (IOException e) { + lastException = e; + Thread.onSpinWait(); + } + } + throw new IOException("SFTP server not ready after 30 seconds on port " + port, lastException); } protected PublickeyAuthenticator getPublickeyAuthenticator() {