Fixed runnable to allow acces to pending messages
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
5e5cc2a1cd
commit
bcd0faceef
@ -145,6 +145,9 @@ public final class ReadingRunnable implements Runnable {
|
||||
* @throws IOException if the pipe is closed */
|
||||
public String getMessage() throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!messages.isEmpty()) {
|
||||
return messages.poll();
|
||||
}
|
||||
if (!running) {
|
||||
throw new IOException(CLOSED_PIPE);
|
||||
}
|
||||
@ -164,6 +167,9 @@ public final class ReadingRunnable implements Runnable {
|
||||
* @throws IOException if the input was closed */
|
||||
public String getNextMessage(final long timeout) throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!messages.isEmpty()) {
|
||||
return messages.poll();
|
||||
}
|
||||
if (!running) {
|
||||
throw new IOException(CLOSED_PIPE);
|
||||
}
|
||||
|
@ -38,6 +38,7 @@
|
||||
*/
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
@ -75,12 +76,15 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -131,13 +135,13 @@ public class ReadingRunnableTest {
|
||||
final ReadingRunnable runnable = new ReadingRunnable(reader);
|
||||
final Thread th0 = new Thread(runnable, "read");
|
||||
th0.start();
|
||||
Thread th = new Thread(new Runnable() {
|
||||
final Thread th = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runnable.waitForDelivery("msg");
|
||||
} catch (InterruptedException e) {
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -186,4 +190,36 @@ public class ReadingRunnableTest {
|
||||
}
|
||||
}
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.utils.ReadingRunnable#hasMessage()}.
|
||||
*
|
||||
* @throws IOException if an error occurred
|
||||
* @throws InterruptedException if an error occured in the reading thread */
|
||||
@Test
|
||||
public final void testGetPendingMessages() throws IOException, InterruptedException {
|
||||
|
||||
final PipedOutputStream out = new PipedOutputStream();
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(new PipedInputStream(out), StandardCharsets.UTF_8));
|
||||
final ReadingRunnable runnable = new ReadingRunnable(reader);
|
||||
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
||||
writer.write("one");
|
||||
writer.newLine();
|
||||
writer.write("two");
|
||||
writer.close();
|
||||
out.close();
|
||||
|
||||
final Thread th = new Thread(runnable);
|
||||
th.start();
|
||||
th.join();
|
||||
|
||||
assertEquals("Pending messages should be retrievable", "one",
|
||||
runnable.getMessage());
|
||||
assertEquals("Pending messages should be retrievable", "two",
|
||||
runnable.getMessage());
|
||||
try {
|
||||
runnable.getMessage();
|
||||
fail("reading from closed runnable");
|
||||
} catch (final IOException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user