Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class StreamPollFeeder extends Thread {
private final InputStream input;
private final OutputStream output;

private Throwable exception;
private volatile Throwable exception;

private boolean done;
private volatile boolean done;
private final Object lock = new Object();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,43 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

public class StreamPollFeederTest {

@Test
public void waitUntilFeederDoneWithContinuousData() {
// Simulates a stream with continuous data where the feeder thread
// never enters the synchronized(lock) wait block, so the done
// flag must be visible without relying on lock's happens-before.
InputStream continuousInput = new InputStream() {
@Override
public int available() {
return 1;
}

@Override
public int read() throws IOException {
return 0;
}
};

assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
StreamPollFeeder feeder = new StreamPollFeeder(continuousInput, new ByteArrayOutputStream());
feeder.start();
feeder.waitUntilDone();
assertNull(feeder.getException());
});
}

@Test
public void waitUntilFeederDoneOnInputStream() throws Exception {

Expand Down
Loading