Added test for interruption and task failure
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
8436e8926c
commit
43c9faaee7
@ -5,8 +5,11 @@ package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@ -20,8 +23,6 @@ public class ForkTaskTest {
|
||||
|
||||
@Test
|
||||
public void testGenericForkTask() throws IOException {
|
||||
|
||||
// FIXME This test fail to complete on Jenkins.
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
@ -63,4 +64,93 @@ public class ForkTaskTest {
|
||||
task.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTaskInterruption() throws IOException, InterruptedException {
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
while (isRunning()) {
|
||||
String msg;
|
||||
try {
|
||||
msg = in.prompt(1000);
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"Unable to prompt user");
|
||||
}
|
||||
if (msg != null) {
|
||||
out.println(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
final Thread execThread = new Thread(task, "Task");
|
||||
execThread.start();
|
||||
execThread.join(100);
|
||||
|
||||
final AtomicBoolean interrupted = new AtomicBoolean(false);
|
||||
final AtomicBoolean interrupted2 = new AtomicBoolean(false);
|
||||
final InterruptionListener listener = new InterruptionListener() {
|
||||
|
||||
@Override
|
||||
public void interrupted() {
|
||||
interrupted.set(true);
|
||||
}
|
||||
};
|
||||
final InterruptionListener listener2 = new InterruptionListener() {
|
||||
|
||||
@Override
|
||||
public void interrupted() {
|
||||
interrupted2.set(true);
|
||||
}
|
||||
};
|
||||
task.addInterruptionListener(listener);
|
||||
task.addInterruptionListener(listener2);
|
||||
task.rmInterruptionListener(listener2);
|
||||
|
||||
assertFalse("Interruption should not be notified before actual interruption",
|
||||
interrupted.get());
|
||||
assertTrue("Task should be started", task.isStarted());
|
||||
task.setRunning(false);
|
||||
try (PipedConsoleOutput pco = new PipedConsoleOutput();
|
||||
PipedConsoleInput pci = new PipedConsoleInput(null)) {
|
||||
task.join(pco, pci, 2000);
|
||||
}
|
||||
execThread.join();
|
||||
assertTrue("Interruption should be notified to listeners", interrupted.get());
|
||||
|
||||
assertFalse("Running state should be updated by task on its completion",
|
||||
task.isRunning());
|
||||
assertFalse("Interruption should not be notified to removed listeners",
|
||||
interrupted2.get());
|
||||
|
||||
task.rmInterruptionListener(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailingTask() throws InterruptedException {
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
throw new CommandRunException("Error");
|
||||
}
|
||||
};
|
||||
|
||||
final Thread execThread = new Thread(task, "Task");
|
||||
execThread.start();
|
||||
execThread.join();
|
||||
assertNotNull("Exception should be forwarded", task.getException());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user