[fix] Set waits inside loops
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
a9c97a7ebc
commit
66d25697a8
@ -142,16 +142,19 @@ 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);
|
||||
long tac = System.currentTimeMillis() - tic;
|
||||
while (running && tac < timeout) {
|
||||
try {
|
||||
if (running) {
|
||||
runLock.wait(timeout);
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
tac = System.currentTimeMillis() - tic;
|
||||
}
|
||||
this.out.disconnect();
|
||||
this.in.disconnect();
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -75,40 +75,33 @@ 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 res = in.prompt("m1", -1);
|
||||
final String res1 = in.prompt("m1", -1);
|
||||
fail("interruption of infinite waiting prompt should cause error, but was "
|
||||
+ res);
|
||||
} catch (final InterruptedIOException e) {
|
||||
+ res1);
|
||||
} catch (final InterruptedIOException e1) {
|
||||
// ok
|
||||
}
|
||||
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 "
|
||||
+ res);
|
||||
} catch (final InterruptedIOException e) {
|
||||
+ res2);
|
||||
} catch (final InterruptedIOException e2) {
|
||||
// ok
|
||||
}
|
||||
synchronized (ended) {
|
||||
ended.set(true);
|
||||
try {
|
||||
assertNull("Overtime should return null", in.prompt("m3", 200));
|
||||
} catch (final InterruptedIOException e) {
|
||||
} 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() {
|
||||
final Thread th = new Thread(runnable, "TestPromptSequence");
|
||||
final Thread inter = new Thread(() -> {
|
||||
while (!ended.get()) {
|
||||
try {
|
||||
th.join(100);
|
||||
@ -122,7 +115,6 @@ public class ConnectingConsoleInputTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
th.start();
|
||||
inter.start();
|
||||
@ -135,10 +127,7 @@ 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() {
|
||||
final Thread inter2 = new Thread(() -> {
|
||||
while (!ended.get()) {
|
||||
try {
|
||||
th2.join(100);
|
||||
@ -152,7 +141,6 @@ public class ConnectingConsoleInputTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
th2.start();
|
||||
inter2.start();
|
||||
|
@ -294,8 +294,10 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
brutalDisconnection();
|
||||
}
|
||||
}
|
||||
while (!connected) {
|
||||
connexionLock.wait(connexionTimeout);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user