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. */ /** The application. */
private ConsoleApplication app; private ConsoleApplication app;
private final Object initLock = new Object();
/** The server socket. */ /** The server socket. */
private ServerSocket serverSocket; private ServerSocket serverSocket;
/** THe server address. */ /** THe server address. */
@ -123,7 +125,10 @@ public final class SocketConsoleApplicationShell implements Runnable {
// Create the server // Create the server
try (ServerSocket actualServerSocket = new ServerSocket(port, 1, addr)) { try (ServerSocket actualServerSocket = new ServerSocket(port, 1, addr)) {
serverSocket = actualServerSocket; serverSocket = actualServerSocket;
synchronized (initLock) {
running = true; running = true;
initLock.notifyAll();
}
// Create the streams // Create the streams
runSokectServer(); runSokectServer();
} catch (final IOException e) { } 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$ 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) { if (server == null) {
server = new Thread(getShell(), "gclcServer"); server = new Thread(getShell(), "gclcServer");
server.start(); server.start();
getShell().waitStartUp(500);
} }
return server; return server;
} }
@ -82,13 +83,7 @@ public class TestServer {
SHELL.setInterface(new SocketConsoleInterface(input, output)); SHELL.setInterface(new SocketConsoleInterface(input, output));
SHELL.setConnexionManager(manager); SHELL.setConnexionManager(manager);
SHELL.setApplication(app); SHELL.setApplication(app);
final Thread th = new Thread(new Runnable() { final Thread th = new Thread(() -> app.start());
@Override
public void run() {
app.start();
}
});
th.start(); th.start();
try { try {
final Object waiting = new Object(); final Object waiting = new Object();