From: David ‘Bombe’ Roden Date: Tue, 3 Sep 2024 09:19:41 +0000 (+0200) Subject: ✅ Prevent test from starting if local server is not yet up X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=660140130da7742dc05942b1dd2ee63d9f9959ed;p=jFCPlib.git ✅ Prevent test from starting if local server is not yet up --- 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;