Skip to content

Commit

Permalink
Interrupt Feeder thread not current thread
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Apr 21, 2023
1 parent 587e480 commit 068d20b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Throwable getException() {
}

public void waitUntilDone() {
interrupt();
this.interrupt();
synchronized (lock) {
while (!done) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@

public class StreamFeederTest {
static class BlockingInputStream extends ByteArrayInputStream {
boolean endStream = false;
final Object lock = new Object();

public BlockingInputStream(byte[] buf) {
super(buf);
}
Expand All @@ -61,38 +58,34 @@ public synchronized int read() {
}

// end test data ... block
endStream = true;

try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return -1;
}

public synchronized void waitForEndStream() throws InterruptedException {
while (!endStream) {
wait(100);
}
}
}

@Test
public void waitUntilFeederDone() throws InterruptedException {

BlockingInputStream inputStream = new BlockingInputStream("TestData".getBytes());
String TEST_DATA = "TestData";

BlockingInputStream inputStream = new BlockingInputStream(TEST_DATA.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

StreamFeeder streamFeeder = new StreamFeeder(inputStream, outputStream);

streamFeeder.start();

// wait until input stream will be in block mode
inputStream.waitForEndStream();
// wait until all data from steam will be read
while (outputStream.size()<TEST_DATA.length()) {
Thread.sleep(10);
}

streamFeeder.waitUntilDone(); // wait until process finish

assertEquals("TestData", outputStream.toString());
assertEquals(TEST_DATA, outputStream.toString());
}
}

0 comments on commit 068d20b

Please sign in to comment.