From ce6f04bdc72b998e5ca4231efc39f4e45764876c Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Tue, 2 Aug 2016 20:32:59 +0200 Subject: [PATCH] Fixing a race condition in socket server - This caused the mocked test cases to sometimes hang. - Was particularly noticeable when running with code coverage. --- src/se/vidstige/jadb/server/SocketServer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/se/vidstige/jadb/server/SocketServer.java b/src/se/vidstige/jadb/server/SocketServer.java index a44f832..f0dd880 100644 --- a/src/se/vidstige/jadb/server/SocketServer.java +++ b/src/se/vidstige/jadb/server/SocketServer.java @@ -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) {