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.
}
public Socket getSocket() {
+ try {
+ connectedLatch.await();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
return socket;
}
new Thread(() -> {
try {
socket = serverSocket.accept();
+ connectedLatch.countDown();
} catch (IOException e) {
if (!finished) {
throw new RuntimeException(e);
}
private final ServerSocket serverSocket;
+ private final CountDownLatch connectedLatch = new CountDownLatch(1);
private volatile boolean finished = false;
private volatile Socket socket;