Adding shell start up wait method.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2019-05-06 20:31:37 -04:00
parent da8765dd7b
commit 24e57eca2f
2 changed files with 27 additions and 8 deletions

View File

@ -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;
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();
}
}
}
}
}

View File

@ -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();