Fixing a race condition in socket server

- This caused the mocked test cases to sometimes hang.
- Was particularly noticeable when running with code coverage.
This commit is contained in:
Samuel Carlsson 2016-08-02 20:32:59 +02:00
parent a82a850a50
commit ce6f04bdc7

View File

@ -10,6 +10,8 @@ public abstract class SocketServer implements Runnable {
private final int port;
private ServerSocket socket;
private Thread thread;
private boolean isStarted = false;
private final Object lockObject = new Object();
protected SocketServer(int port) {
@ -21,7 +23,9 @@ public abstract class SocketServer implements Runnable {
thread.setDaemon(true);
thread.start();
synchronized (lockObject) {
lockObject.wait();
if (!isStarted) {
lockObject.wait();
}
}
}
@ -37,6 +41,7 @@ public abstract class SocketServer implements Runnable {
synchronized (lockObject) {
lockObject.notify();
isStarted = true;
}
while (true) {