[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,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();

View File

@ -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) {

View File

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

View File

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