From 24e57eca2f55e97878461f451e706681f7488e29 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Mon, 6 May 2019 20:31:37 -0400 Subject: [PATCH] Adding shell start up wait method. Signed-off-by: Emmanuel Bigeon --- .../socket/SocketConsoleApplicationShell.java | 26 ++++++++++++++++++- .../net/bigeon/gclc/socket/TestServer.java | 9 ++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/gclc-socket/src/main/java/net/bigeon/gclc/socket/SocketConsoleApplicationShell.java b/gclc-socket/src/main/java/net/bigeon/gclc/socket/SocketConsoleApplicationShell.java index bdb4e22..65b5a12 100644 --- a/gclc-socket/src/main/java/net/bigeon/gclc/socket/SocketConsoleApplicationShell.java +++ b/gclc-socket/src/main/java/net/bigeon/gclc/socket/SocketConsoleApplicationShell.java @@ -77,6 +77,8 @@ public final class SocketConsoleApplicationShell implements Runnable { /** The application. */ private ConsoleApplication app; + private final Object initLock = new Object(); + /** The server socket. */ private ServerSocket serverSocket; /** THe server address. */ @@ -123,7 +125,10 @@ public final class SocketConsoleApplicationShell implements Runnable { // Create the server try (ServerSocket actualServerSocket = new ServerSocket(port, 1, addr)) { serverSocket = actualServerSocket; - running = true; + synchronized (initLock) { + running = true; + initLock.notifyAll(); + } // Create the streams runSokectServer(); } catch (final IOException e) { @@ -194,4 +199,23 @@ public final class SocketConsoleApplicationShell implements Runnable { LOGGER.log(Level.SEVERE, "Exception in closing socket server", e); //$NON-NLS-1$ } } + + /** A method to wait for the socket server initialization. + * + * @param timeout the timeout */ + public void waitStartUp(final long timeout) { + synchronized (initLock) { + final long tic = System.currentTimeMillis() + timeout; + long tac; + while (!running && (tac = System.currentTimeMillis()) < tic) { + final long idle = tic - tac; + try { + initLock.wait(idle); + } catch (final InterruptedException e) { + LOGGER.log(Level.INFO, "Thread interruption", e); + Thread.currentThread().interrupt(); + } + } + } + } } diff --git a/gclc-socket/src/test/java/net/bigeon/gclc/socket/TestServer.java b/gclc-socket/src/test/java/net/bigeon/gclc/socket/TestServer.java index 5a3b029..05f7b4c 100644 --- a/gclc-socket/src/test/java/net/bigeon/gclc/socket/TestServer.java +++ b/gclc-socket/src/test/java/net/bigeon/gclc/socket/TestServer.java @@ -66,6 +66,7 @@ public class TestServer { if (server == null) { server = new Thread(getShell(), "gclcServer"); server.start(); + getShell().waitStartUp(500); } return server; } @@ -82,13 +83,7 @@ public class TestServer { SHELL.setInterface(new SocketConsoleInterface(input, output)); SHELL.setConnexionManager(manager); SHELL.setApplication(app); - final Thread th = new Thread(new Runnable() { - - @Override - public void run() { - app.start(); - } - }); + final Thread th = new Thread(() -> app.start()); th.start(); try { final Object waiting = new Object();