Fixed thread surviving the application lifespan.

+tests in swt.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2016-12-03 20:18:20 -05:00
parent 24f1fba97e
commit 543b1ef605
9 changed files with 179 additions and 144 deletions

View File

@ -51,7 +51,7 @@
<dependency>
<groupId>fr.bigeon</groupId>
<artifactId>gclc</artifactId>
<version>1.3.1</version>
<version>1.3.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>fr.bigeon</groupId>

View File

@ -1,133 +0,0 @@
/*
* Copyright E. Bigeon (2015)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a swt window for console applications.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
/**
* gclc:fr.bigeon.gclc.tools.AOutputForwardRunnable.java
* Created on: Dec 1, 2016
*/
package fr.bigeon.gclc.swt;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** An incomplete implematation used to forward messages from a piped console.
* <p>
* This forwarding can be interrupted without closing the piped manager.
*
* @author Emmanuel Bigeon
* @deprecated since version 1.3.2 of gclc, this class has been integrated in
* the main content. */
@Deprecated
public abstract class AOutputForwardRunnable implements Runnable {
/** The class logger */
private static final Logger LOGGER = Logger
.getLogger(AOutputForwardRunnable.class.getName());
/** The default timeout (one tenth of second). */
private static final long DEFAULT_TIMEOUT = 100;
/** The manager. */
private final PipedConsoleManager manager;
/** The timeout */
private final long timeout;
/** Create a forward runnable with the given timeout.
* <p>
* Short timeout will be very responsive to the application actual messages,
* but may use computation time if the application is not verbose. Long
* timeout will save computation time, but will read batches of messages at
* once if the application is verbose. The right length for the timeout is
* likely to depend on the application and the use of it.
* <p>
* If you do not know what timeout length to use, please use the
* {@link #AOutputForwardRunnable(PipedConsoleManager)} constructor.
*
* @param manager the manager
* @param timeout the timeout between message requests. */
public AOutputForwardRunnable(PipedConsoleManager manager, long timeout) {
super();
this.manager = manager;
this.timeout = timeout;
}
/** Create a forwarding runnable.
*
* @param manager the manager */
public AOutputForwardRunnable(PipedConsoleManager manager) {
super();
this.manager = manager;
timeout = DEFAULT_TIMEOUT;
}
@Override
public void run() {
try {
while (isRunning()) {
while (isRunning() && !manager.available()) {
waitASec();
}
if (!isRunning()) {
return;
}
String m = manager.readNextLine();
forwardLine(m);
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unexpected problem in manager", //$NON-NLS-1$
e);
}
}
/** @param m the line to forward */
protected abstract void forwardLine(String m);
/** @return if the thread should keep running */
protected abstract boolean isRunning();
/** a method to wait some time */
protected void waitASec() {
try {
synchronized (this) {
wait(timeout);
}
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$
e);
return;
}
}
}

View File

@ -53,6 +53,7 @@ import org.eclipse.swt.widgets.Text;
import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.manager.PipedConsoleManager;
import fr.bigeon.gclc.tools.AOutputForwardRunnable;
/** A SWT component to connect to gclc {@link ConsoleApplication}
* <p>
@ -62,14 +63,13 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
/** The local implementation of the forwarding runnable
*
* @author Emmanuel Bigeon */
@SuppressWarnings("deprecation")
private final class ToSWTConsoleForwarRunnable
private final class ToSWTConsoleForwardRunnable
extends AOutputForwardRunnable {
/** The running status */
private boolean running = true;
/** @param manager the manager */
public ToSWTConsoleForwarRunnable(PipedConsoleManager manager) {
public ToSWTConsoleForwardRunnable(PipedConsoleManager manager) {
super(manager);
}
@ -99,7 +99,7 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
/** The actual manager */
private PipedConsoleManager manager;
/** The forwarding runnable */
private ToSWTConsoleForwarRunnable forward;
private ToSWTConsoleForwardRunnable forward;
/** Create the composite.
*
@ -135,7 +135,7 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
if (forward != null) {
forward.setRunning(false);
}
forward = new ToSWTConsoleForwarRunnable(manager);
forward = new ToSWTConsoleForwardRunnable(manager);
Thread th = new Thread(forward, "gclcToSWT"); //$NON-NLS-1$
th.start();
}

View File

@ -39,7 +39,9 @@
package fr.bigeon.gclc.swt;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
@ -60,7 +62,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
public class SWTConsoleShellTest {
protected static final long TWO_SECONDS = 2000;
private static final Display DISPLAY = new Display();
private static final Display DISPLAY = Display.getDefault();
@Test
public void testConsoleClose() {
@ -171,6 +173,7 @@ public class SWTConsoleShellTest {
shell.dispose();
}
});
}
});
applThread.start();
@ -180,6 +183,13 @@ public class SWTConsoleShellTest {
DISPLAY.sleep();
}
}
swtConsole.setPrompt(">");
try {
swtConsole.prompt();
fail("Prompting when closed should fail!");
} catch (IOException e) {
assertNotNull(e);
}
// DISPLAY.dispose();
assertTrue(appl.getManager().isClosed());
Thread.sleep(TWO_SECONDS);

View File

@ -0,0 +1,145 @@
/*
* Copyright E. Bigeon (2015)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a swt window for console applications.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
/**
* gclc-swt:fr.bigeon.gclc.swt.SWTConsoleShellTest.java
* Created on: Jun 8, 2016
*/
package fr.bigeon.gclc.swt;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.Test;
import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.command.ExitCommand;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"})
public class SWTConsoleViewTest {
protected static final long TWO_SECONDS = 2000;
private static final Display DISPLAY = Display.getDefault();
@Test
public void test() {
final Shell shell = new Shell(DISPLAY);
final SWTConsoleView swtConsole = new SWTConsoleView(shell, SWT.NONE);
try (PipedConsoleManager manager = new PipedConsoleManager()) {
swtConsole.setManager(manager);
final ConsoleApplication appl = new ConsoleApplication(manager,
"Hello", "See you");
appl.add(new ExitCommand("exit", appl));
appl.add(new Command("long") {
@Override
public String tip() {
return "a long running command";
}
@Override
public void execute(String... args) {
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// shell.pack();
shell.open();
Thread applThread = new Thread(new Runnable() {
@Override
public void run() {
appl.start();
}
});
Thread testThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("test"); //$NON-NLS-1$
swtConsole.validateCommand();
}
});
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
shell.dispose();
}
});
}
});
applThread.start();
testThread.start();
while (!shell.isDisposed()) {
if (!DISPLAY.readAndDispatch()) {
DISPLAY.sleep();
}
}
} catch (InvalidCommandName e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

View File

@ -48,7 +48,7 @@ import java.io.InterruptedIOException;
* a message, in that case, the usual behavior is to prompt normally.
*
* @author Emmanuel BIGEON */
public interface ConsoleManager {
public interface ConsoleManager extends AutoCloseable {
/** @return the prompt prefix */
String getPrompt();
@ -89,6 +89,7 @@ public interface ConsoleManager {
/** Closes the manager.
*
* @throws IOException if the close raised an exception */
@Override
void close() throws IOException;
/** @return if the manager is closed. */

View File

@ -54,7 +54,7 @@ import java.nio.charset.Charset;
*
* @author Emmanuel Bigeon */
public final class PipedConsoleManager
implements ConsoleManager, AutoCloseable {
implements ConsoleManager {
/** The encoding between streams */
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
@ -91,6 +91,7 @@ public final class PipedConsoleManager
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
th.start();
th = new Thread(reading, "read"); //$NON-NLS-1$
th.setDaemon(true);
th.start();
}

View File

@ -88,6 +88,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
this.in = new BufferedReader(new InputStreamReader(in, charset));
reading = new ReadingRunnable(this.in);
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
promptThread.setDaemon(true);
promptThread.start();
}
@ -156,6 +157,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
public void close() throws IOException {
closed = true;
reading.setRunning(false);
promptThread.interrupt();
}
/* (non-Javadoc)

View File

@ -58,7 +58,7 @@ public class AOutputForwardRunnableTest {
* @author Emmanuel Bigeon */
private final class AOutputForwardTestRunnable
extends AOutputForwardRunnable {
private int count = 1;
private int count = 2;
private String message;
/** @param manager */
@ -117,11 +117,20 @@ public class AOutputForwardRunnableTest {
manager);
Thread th = new Thread(runnable, "forward");
th.start();
manager.println("before");
th.start();
assertEquals("before", runnable.getMessage());
synchronized (this) {
wait(3000);
}
manager.println("after");
assertEquals("after", runnable.getMessage());
synchronized (this) {
wait(3000);
}
th.join();
} catch (IOException | InterruptedException e) {