[fix] Set waits inside loops

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2021-11-12 09:50:51 +01:00
parent a9c97a7ebc
commit 66d25697a8
4 changed files with 56 additions and 63 deletions

View File

@ -142,16 +142,19 @@ public abstract class ForkTask implements Task {
* @param timeout the maximal time to join for (0 for ever) */ * @param timeout the maximal time to join for (0 for ever) */
public final void join(final ConsoleOutput out, final ConsoleInput in, public final void join(final ConsoleOutput out, final ConsoleInput in,
final long timeout) { final long timeout) {
final long tic = System.currentTimeMillis();
synchronized (runLock) { synchronized (runLock) {
this.out.connect(out); this.out.connect(out);
this.in.connect(in); this.in.connect(in);
long tac = System.currentTimeMillis() - tic;
while (running && tac < timeout) {
try { try {
if (running) {
runLock.wait(timeout); runLock.wait(timeout);
}
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
tac = System.currentTimeMillis() - tic;
}
this.out.disconnect(); this.out.disconnect();
this.in.disconnect(); this.in.disconnect();
} }

View File

@ -286,10 +286,10 @@ public final class ConnectingConsoleInput implements ConsoleInput {
} }
private void getConnection(final long timeout) throws InterruptedIOException { private void getConnection(final long timeout) throws InterruptedIOException {
boolean connect; final long tic = System.currentTimeMillis();
synchronized (connectionLock) { synchronized (connectionLock) {
connect = connected != null; while ((connected == null || !interrupting)
if (!connect) { && (tic + timeout) > System.currentTimeMillis()) {
try { try {
connectionLock.wait(timeout); connectionLock.wait(timeout);
} catch (final InterruptedException e) { } catch (final InterruptedException e) {

View File

@ -75,40 +75,33 @@ public class ConnectingConsoleInputTest {
final ConnectingConsoleInput in = new ConnectingConsoleInput(); final ConnectingConsoleInput in = new ConnectingConsoleInput();
// Unconnected // Unconnected
final AtomicBoolean ended = new AtomicBoolean(false); final AtomicBoolean ended = new AtomicBoolean(false);
final TestFunction one = new TestFunction() { final TestFunction one = () -> {
@Override
public void apply() throws Exception {
try { try {
final String res = in.prompt("m1", -1); final String res1 = in.prompt("m1", -1);
fail("interruption of infinite waiting prompt should cause error, but was " fail("interruption of infinite waiting prompt should cause error, but was "
+ res); + res1);
} catch (final InterruptedIOException e) { } catch (final InterruptedIOException e1) {
// ok // ok
} }
try { try {
final String res = in.prompt("m2", 25000); final String res2 = in.prompt("m2", 25000);
fail("interruption of finite waiting prompt should cause error, but was " fail("interruption of finite waiting prompt should cause error, but was "
+ res); + res2);
} catch (final InterruptedIOException e) { } catch (final InterruptedIOException e2) {
// ok // ok
} }
synchronized (ended) { synchronized (ended) {
ended.set(true); ended.set(true);
try { try {
assertNull("Overtime should return null", in.prompt("m3", 200)); assertNull("Overtime should return null", in.prompt("m3", 200));
} catch (final InterruptedIOException e) { } catch (final InterruptedIOException e3) {
fail("Unexpected interruption error in overtime"); fail("Unexpected interruption error in overtime");
} }
} }
}
}; };
final ATestRunnable runnable = new FunctionalTestRunnable(one); final ATestRunnable runnable = new FunctionalTestRunnable(one);
final Thread th = new Thread(runnable); final Thread th = new Thread(runnable, "TestPromptSequence");
final Thread inter = new Thread(new Runnable() { final Thread inter = new Thread(() -> {
@Override
public void run() {
while (!ended.get()) { while (!ended.get()) {
try { try {
th.join(100); th.join(100);
@ -122,7 +115,6 @@ public class ConnectingConsoleInputTest {
} }
} }
} }
}
}); });
th.start(); th.start();
inter.start(); inter.start();
@ -135,10 +127,7 @@ public class ConnectingConsoleInputTest {
in.connect(new StreamConsoleInput(null, pis, StandardCharsets.UTF_8)); in.connect(new StreamConsoleInput(null, pis, StandardCharsets.UTF_8));
final ATestRunnable runnable2 = new FunctionalTestRunnable(one); final ATestRunnable runnable2 = new FunctionalTestRunnable(one);
final Thread th2 = new Thread(runnable2); final Thread th2 = new Thread(runnable2);
final Thread inter2 = new Thread(new Runnable() { final Thread inter2 = new Thread(() -> {
@Override
public void run() {
while (!ended.get()) { while (!ended.get()) {
try { try {
th2.join(100); th2.join(100);
@ -152,7 +141,6 @@ public class ConnectingConsoleInputTest {
} }
} }
} }
}
}); });
th2.start(); th2.start();
inter2.start(); inter2.start();

View File

@ -294,8 +294,10 @@ public final class PluggableConsoleInput implements ConsoleInput {
brutalDisconnection(); brutalDisconnection();
} }
} }
while (!connected) {
connexionLock.wait(connexionTimeout); connexionLock.wait(connexionTimeout);
} }
}
return null; return null;
} }