From 43c9faaee7c8fd58262a66f39ae7e727eeb9f7ee Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Mon, 3 Dec 2018 11:12:23 -0500 Subject: [PATCH] Added test for interruption and task failure Signed-off-by: Emmanuel Bigeon --- .../net/bigeon/gclc/process/ForkTaskTest.java | 94 ++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/gclc-process/src/test/java/net/bigeon/gclc/process/ForkTaskTest.java b/gclc-process/src/test/java/net/bigeon/gclc/process/ForkTaskTest.java index f897b47..1de4894 100644 --- a/gclc-process/src/test/java/net/bigeon/gclc/process/ForkTaskTest.java +++ b/gclc-process/src/test/java/net/bigeon/gclc/process/ForkTaskTest.java @@ -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()); + } }