Skip to content

Commit

Permalink
Attribute non-test thread output to most recent test thread (junit-te…
Browse files Browse the repository at this point in the history
…am#4200)

When using frameworks or running external processes, test output is
often written on other threads than the test. When tests are executed
sequentially that output can be attributed unambiguously to the current
test. If tests are run in parallel, picking the right test to attribute
output to becomes much harder, though.
  • Loading branch information
marcphilipp authored Dec 16, 2024
1 parent 779ee68 commit 3997952
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ JUnit repository on GitHub.
- If <<../user-guide/index.adoc#running-tests-capturing-output, output capturing>> is
enabled, the captured output written to `System.out` and `System.err` is now included
in the XML report.
* Output written to `System.out` and `System.err` from non-test threads is now attributed
to the most recent test or container that was started or has written output.
* Introduced contracts for Kotlin-specific assertion methods.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;

/**
* @since 1.3
*/
class StreamInterceptor extends PrintStream {

private final Deque<RewindableByteArrayOutputStream> mostRecentOutputs = new ConcurrentLinkedDeque<>();

private final PrintStream originalStream;
private final Consumer<PrintStream> unregisterAction;
private final int maxNumberOfBytesPerThread;
Expand Down Expand Up @@ -56,11 +59,18 @@ private StreamInterceptor(PrintStream originalStream, Consumer<PrintStream> unre
}

void capture() {
output.get().mark();
RewindableByteArrayOutputStream out = output.get();
out.mark();
pushToTop(out);
}

String consume() {
return output.get().rewind();
RewindableByteArrayOutputStream out = output.get();
String result = out.rewind();
if (!out.isMarked()) {
mostRecentOutputs.remove(out);
}
return result;
}

void unregister() {
Expand All @@ -69,8 +79,9 @@ void unregister() {

@Override
public void write(int b) {
RewindableByteArrayOutputStream out = output.get();
if (out.isMarked() && out.size() < maxNumberOfBytesPerThread) {
RewindableByteArrayOutputStream out = getOutput();
if (out != null && out.size() < maxNumberOfBytesPerThread) {
pushToTop(out);
out.write(b);
}
super.write(b);
Expand All @@ -83,16 +94,29 @@ public void write(byte[] b) {

@Override
public void write(byte[] buf, int off, int len) {
RewindableByteArrayOutputStream out = output.get();
if (out.isMarked()) {
RewindableByteArrayOutputStream out = getOutput();
if (out != null) {
int actualLength = Math.max(0, Math.min(len, maxNumberOfBytesPerThread - out.size()));
if (actualLength > 0) {
pushToTop(out);
out.write(buf, off, actualLength);
}
}
super.write(buf, off, len);
}

private void pushToTop(RewindableByteArrayOutputStream out) {
if (!out.equals(mostRecentOutputs.peek())) {
mostRecentOutputs.remove(out);
mostRecentOutputs.push(out);
}
}

private RewindableByteArrayOutputStream getOutput() {
RewindableByteArrayOutputStream out = output.get();
return out.isMarked() ? out : mostRecentOutputs.peek();
}

static class RewindableByteArrayOutputStream extends ByteArrayOutputStream {

private final Deque<Integer> markedPositions = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@
import java.io.PrintStream;
import java.util.stream.IntStream;

import org.junit.jupiter.api.AutoClose;
import org.junit.jupiter.api.Test;

/**
* @since 1.3
*/
class StreamInterceptorTests {

private ByteArrayOutputStream originalOut = new ByteArrayOutputStream();
private PrintStream targetStream = new PrintStream(originalOut);
final ByteArrayOutputStream originalOut = new ByteArrayOutputStream();
PrintStream targetStream = new PrintStream(originalOut);

@AutoClose
StreamInterceptor streamInterceptor;

@Test
void interceptsWriteOperationsToStreamPerThread() {
var streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
3).orElseThrow(RuntimeException::new);
// @formatter:off
IntStream.range(0, 1000)
.parallel()
.peek(i -> targetStream.println(i))
.mapToObj(String::valueOf)
.peek(i -> streamInterceptor.capture())
.peek(i -> targetStream.println(i))
Expand All @@ -49,7 +52,7 @@ void interceptsWriteOperationsToStreamPerThread() {
void unregisterRestoresOriginalStream() {
var originalStream = targetStream;

var streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
3).orElseThrow(RuntimeException::new);
assertSame(streamInterceptor, targetStream);

Expand All @@ -61,8 +64,8 @@ void unregisterRestoresOriginalStream() {
void writeForwardsOperationsToOriginalStream() throws IOException {
var originalStream = targetStream;

StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream, 2).orElseThrow(
RuntimeException::new);
streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
2).orElseThrow(RuntimeException::new);
assertNotSame(originalStream, targetStream);

targetStream.write('a');
Expand All @@ -73,7 +76,7 @@ void writeForwardsOperationsToOriginalStream() throws IOException {

@Test
void handlesNestedCaptures() {
var streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
100).orElseThrow(RuntimeException::new);

String outermost, inner, innermost;
Expand All @@ -100,4 +103,19 @@ void handlesNestedCaptures() {
() -> assertEquals("innermost", innermost) //
);
}

@Test
void capturesOutputFromNonTestThreads() throws Exception {
streamInterceptor = StreamInterceptor.register(targetStream, newStream -> this.targetStream = newStream,
100).orElseThrow(RuntimeException::new);

streamInterceptor.capture();
var thread = new Thread(() -> {
targetStream.println("from non-test thread");
});
thread.start();
thread.join();

assertEquals("from non-test thread", streamInterceptor.consume().trim());
}
}

0 comments on commit 3997952

Please sign in to comment.