From 66d25697a81eaf89519d366ee4ae9412b171be3d Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Fri, 12 Nov 2021 09:50:51 +0100 Subject: [PATCH] [fix] Set waits inside loops Signed-off-by: Emmanuel Bigeon --- .../net/bigeon/gclc/process/ForkTask.java | 11 ++- .../process/io/ConnectingConsoleInput.java | 6 +- .../io/ConnectingConsoleInputTest.java | 98 ++++++++----------- .../gclc/socket/PluggableConsoleInput.java | 4 +- 4 files changed, 56 insertions(+), 63 deletions(-) diff --git a/gclc-process/src/main/java/net/bigeon/gclc/process/ForkTask.java b/gclc-process/src/main/java/net/bigeon/gclc/process/ForkTask.java index c8d57cb..bf1daba 100644 --- a/gclc-process/src/main/java/net/bigeon/gclc/process/ForkTask.java +++ b/gclc-process/src/main/java/net/bigeon/gclc/process/ForkTask.java @@ -142,15 +142,18 @@ public abstract class ForkTask implements Task { * @param timeout the maximal time to join for (0 for ever) */ public final void join(final ConsoleOutput out, final ConsoleInput in, final long timeout) { + final long tic = System.currentTimeMillis(); synchronized (runLock) { this.out.connect(out); this.in.connect(in); - try { - if (running) { + long tac = System.currentTimeMillis() - tic; + while (running && tac < timeout) { + try { runLock.wait(timeout); + } catch (final InterruptedException e) { + Thread.currentThread().interrupt(); } - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); + tac = System.currentTimeMillis() - tic; } this.out.disconnect(); this.in.disconnect(); diff --git a/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java b/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java index 39cc520..d9c4280 100644 --- a/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java +++ b/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java @@ -286,10 +286,10 @@ public final class ConnectingConsoleInput implements ConsoleInput { } private void getConnection(final long timeout) throws InterruptedIOException { - boolean connect; + final long tic = System.currentTimeMillis(); synchronized (connectionLock) { - connect = connected != null; - if (!connect) { + while ((connected == null || !interrupting) + && (tic + timeout) > System.currentTimeMillis()) { try { connectionLock.wait(timeout); } catch (final InterruptedException e) { diff --git a/gclc-process/src/test/java/net/bigeon/gclc/process/io/ConnectingConsoleInputTest.java b/gclc-process/src/test/java/net/bigeon/gclc/process/io/ConnectingConsoleInputTest.java index eb88b4a..bdbe313 100644 --- a/gclc-process/src/test/java/net/bigeon/gclc/process/io/ConnectingConsoleInputTest.java +++ b/gclc-process/src/test/java/net/bigeon/gclc/process/io/ConnectingConsoleInputTest.java @@ -75,51 +75,43 @@ public class ConnectingConsoleInputTest { final ConnectingConsoleInput in = new ConnectingConsoleInput(); // Unconnected final AtomicBoolean ended = new AtomicBoolean(false); - final TestFunction one = new TestFunction() { - - @Override - public void apply() throws Exception { + final TestFunction one = () -> { + try { + final String res1 = in.prompt("m1", -1); + fail("interruption of infinite waiting prompt should cause error, but was " + + res1); + } catch (final InterruptedIOException e1) { + // ok + } + try { + final String res2 = in.prompt("m2", 25000); + fail("interruption of finite waiting prompt should cause error, but was " + + res2); + } catch (final InterruptedIOException e2) { + // ok + } + synchronized (ended) { + ended.set(true); try { - final String res = in.prompt("m1", -1); - fail("interruption of infinite waiting prompt should cause error, but was " - + res); - } catch (final InterruptedIOException e) { - // ok - } - try { - final String res = in.prompt("m2", 25000); - fail("interruption of finite waiting prompt should cause error, but was " - + res); - } catch (final InterruptedIOException e) { - // ok - } - synchronized (ended) { - ended.set(true); - try { - assertNull("Overtime should return null", in.prompt("m3", 200)); - } catch (final InterruptedIOException e) { - fail("Unexpected interruption error in overtime"); - } + assertNull("Overtime should return null", in.prompt("m3", 200)); + } catch (final InterruptedIOException e3) { + fail("Unexpected interruption error in overtime"); } } }; final ATestRunnable runnable = new FunctionalTestRunnable(one); - final Thread th = new Thread(runnable); - final Thread inter = new Thread(new Runnable() { - - @Override - public void run() { - while (!ended.get()) { - try { - th.join(100); - } catch (final InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - synchronized (ended) { - if (!ended.get()) { - in.interruptPrompt(); - } + final Thread th = new Thread(runnable, "TestPromptSequence"); + final Thread inter = new Thread(() -> { + while (!ended.get()) { + try { + th.join(100); + } catch (final InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + synchronized (ended) { + if (!ended.get()) { + in.interruptPrompt(); } } } @@ -135,21 +127,17 @@ public class ConnectingConsoleInputTest { in.connect(new StreamConsoleInput(null, pis, StandardCharsets.UTF_8)); final ATestRunnable runnable2 = new FunctionalTestRunnable(one); final Thread th2 = new Thread(runnable2); - final Thread inter2 = new Thread(new Runnable() { - - @Override - public void run() { - while (!ended.get()) { - try { - th2.join(100); - } catch (final InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - synchronized (ended) { - if (!ended.get()) { - in.interruptPrompt(); - } + final Thread inter2 = new Thread(() -> { + while (!ended.get()) { + try { + th2.join(100); + } catch (final InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + synchronized (ended) { + if (!ended.get()) { + in.interruptPrompt(); } } } diff --git a/gclc-socket/src/main/java/net/bigeon/gclc/socket/PluggableConsoleInput.java b/gclc-socket/src/main/java/net/bigeon/gclc/socket/PluggableConsoleInput.java index ef1f086..1e63e4f 100644 --- a/gclc-socket/src/main/java/net/bigeon/gclc/socket/PluggableConsoleInput.java +++ b/gclc-socket/src/main/java/net/bigeon/gclc/socket/PluggableConsoleInput.java @@ -294,7 +294,9 @@ public final class PluggableConsoleInput implements ConsoleInput { brutalDisconnection(); } } - connexionLock.wait(connexionTimeout); + while (!connected) { + connexionLock.wait(connexionTimeout); + } } return null; }