From 660140130da7742dc05942b1dd2ee63d9f9959ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 3 Sep 2024 11:19:41 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Prevent=20test=20from=20starting=20i?= =?utf8?q?f=20local=20server=20is=20not=20yet=20up?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/test/java/net/pterodactylus/fcp/test/LocalServer.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/net/pterodactylus/fcp/test/LocalServer.java b/src/test/java/net/pterodactylus/fcp/test/LocalServer.java index 9ab5558..62ee38b 100644 --- a/src/test/java/net/pterodactylus/fcp/test/LocalServer.java +++ b/src/test/java/net/pterodactylus/fcp/test/LocalServer.java @@ -22,6 +22,7 @@ import org.junit.rules.ExternalResource; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.concurrent.CountDownLatch; /** * Opens a local {@link ServerSocket} for use in a unit test. @@ -60,6 +61,11 @@ public class LocalServer extends ExternalResource { } public Socket getSocket() { + try { + connectedLatch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } return socket; } @@ -68,6 +74,7 @@ public class LocalServer extends ExternalResource { new Thread(() -> { try { socket = serverSocket.accept(); + connectedLatch.countDown(); } catch (IOException e) { if (!finished) { throw new RuntimeException(e); @@ -95,6 +102,7 @@ public class LocalServer extends ExternalResource { } private final ServerSocket serverSocket; + private final CountDownLatch connectedLatch = new CountDownLatch(1); private volatile boolean finished = false; private volatile Socket socket; -- 2.7.4