Skip to content

Commit 34f3298

Browse files
authored
Remove non-threadsafe tests capturing stdout/stderr (#3002)
- [x] Remove tests that capture standard output (flaky in concurrent environments) - [x] Remove unused test utility code
1 parent 1e35ec0 commit 34f3298

File tree

5 files changed

+10
-233
lines changed

5 files changed

+10
-233
lines changed

vavr/generator/Generator.scala

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,6 @@ def generateTestClasses(): Unit = {
26222622
val assertThat = im.getStatic("org.assertj.core.api.Assertions.assertThat")
26232623
val nested = im.getType("org.junit.jupiter.api.Nested")
26242624
val test = im.getType("org.junit.jupiter.api.Test")
2625-
val assertThrows = im.getStatic("org.junit.jupiter.api.Assertions.assertThrows")
26262625

26272626
val API = im.getType("io.vavr.API")
26282627
val AssertionsExtensions = im.getType("io.vavr.AssertionsExtensions")
@@ -2838,7 +2837,6 @@ def generateTestClasses(): Unit = {
28382837
def genShortcutsTests(im: ImportManager, packageName: String, className: String): String = {
28392838

28402839
val fail = im.getStatic("org.junit.jupiter.api.Assertions.fail")
2841-
val captureStdOut = im.getStatic("io.vavr.OutputTester.captureStdOut")
28422840

28432841
xs"""
28442842
@$test
@@ -2861,26 +2859,6 @@ def generateTestClasses(): Unit = {
28612859
assertThat(err.getMessage()).isEqualTo(msg);
28622860
}
28632861
}
2864-
2865-
@$test
2866-
public void shouldCallprint_Object() {
2867-
assertThat($captureStdOut(()->print("ok"))).isEqualTo("ok");
2868-
}
2869-
2870-
@$test
2871-
public void shouldCallprintf() {
2872-
assertThat($captureStdOut(()->printf("%s", "ok"))).isEqualTo("ok");
2873-
}
2874-
2875-
@$test
2876-
public void shouldCallprintln_Object() {
2877-
assertThat($captureStdOut(()->println("ok"))).isEqualTo("ok\\n");
2878-
}
2879-
2880-
@$test
2881-
public void shouldCallprintln() {
2882-
assertThat($captureStdOut(()->println())).isEqualTo("\\n");
2883-
}
28842862
"""
28852863
}
28862864

vavr/src-gen/test/java/io/vavr/APITest.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
\*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
2525

2626
import static io.vavr.API.*;
27-
import static io.vavr.OutputTester.captureStdOut;
2827
import static io.vavr.Patterns.*;
2928
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.junit.jupiter.api.Assertions.assertThrows;
3129
import static org.junit.jupiter.api.Assertions.fail;
3230

3331
import io.vavr.collection.List;
@@ -75,26 +73,6 @@ public void shouldCompileTODOAndThrowGivenMessageAtRuntime() {
7573
}
7674
}
7775

78-
@Test
79-
public void shouldCallprint_Object() {
80-
assertThat(captureStdOut(()->print("ok"))).isEqualTo("ok");
81-
}
82-
83-
@Test
84-
public void shouldCallprintf() {
85-
assertThat(captureStdOut(()->printf("%s", "ok"))).isEqualTo("ok");
86-
}
87-
88-
@Test
89-
public void shouldCallprintln_Object() {
90-
assertThat(captureStdOut(()->println("ok"))).isEqualTo("ok\n");
91-
}
92-
93-
@Test
94-
public void shouldCallprintln() {
95-
assertThat(captureStdOut(()->println())).isEqualTo("\n");
96-
}
97-
9876
}
9977

10078
//
@@ -1448,8 +1426,6 @@ public void shouldCatchClassCastExceptionWhenPredicateHasDifferentType() {
14481426

14491427
}
14501428

1451-
// -- Match patterns
1452-
14531429
@Nested
14541430
class MatchPatternTests {
14551431

vavr/src/test/java/io/vavr/OutputTester.java

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
*/
1919
package io.vavr;
2020

21-
import java.io.*;
22-
import java.nio.charset.Charset;
23-
import org.junit.jupiter.api.Test;
24-
25-
import static org.assertj.core.api.Assertions.assertThat;
26-
import static org.assertj.core.api.Assertions.fail;
21+
import java.io.IOException;
22+
import java.io.OutputStream;
23+
import java.io.PrintStream;
24+
import java.io.PrintWriter;
2725

2826
/**
2927
* Small utility that allows to test code which write to standard error or standard out.
@@ -32,87 +30,6 @@
3230
*/
3331
public class OutputTester {
3432

35-
@Test
36-
public void shouldNormalizeToUnixLineSeparators() {
37-
assertThat(captureStdOut(() -> System.out.print("\r\n"))).isEqualTo("\n");
38-
}
39-
40-
/**
41-
* Encapsulate the standard output and error stream accessors.
42-
*/
43-
private enum Output {
44-
OUT {
45-
@Override
46-
void set(PrintStream stream) {
47-
System.setOut(stream);
48-
}
49-
50-
@Override
51-
PrintStream get() {
52-
return System.out;
53-
}
54-
},
55-
ERR {
56-
@Override
57-
void set(PrintStream stream) {
58-
System.setErr(stream);
59-
}
60-
61-
@Override
62-
PrintStream get() {
63-
return System.err;
64-
}
65-
};
66-
67-
/**
68-
* Modifier for the output slot.
69-
*/
70-
abstract void set(PrintStream stream);
71-
72-
/**
73-
* Accessor for the output slot.
74-
*/
75-
abstract PrintStream get();
76-
77-
/**
78-
* Capture the output written to this standard stream and normalize to unix line endings.
79-
*/
80-
String capture(Runnable action) {
81-
synchronized (this) {
82-
try {
83-
PrintStream orig = get();
84-
try (ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream inmemory = new PrintStream(out) {
85-
}) {
86-
set(inmemory);
87-
action.run();
88-
return new String(out.toByteArray(), Charset.defaultCharset()).replace("\r\n", "\n");
89-
} finally {
90-
set(orig);
91-
}
92-
} catch (IOException e) {
93-
fail("Unexpected IOException", e);
94-
return "UNREACHABLE";
95-
}
96-
}
97-
}
98-
99-
/**
100-
* Each attempt to write the this standard output will fail with an IOException
101-
*/
102-
void failOnWrite(Runnable action) {
103-
synchronized (this) {
104-
final PrintStream original = get();
105-
try (PrintStream failingPrintStream = failingPrintStream()) {
106-
set(failingPrintStream);
107-
action.run();
108-
} finally {
109-
set(original);
110-
}
111-
}
112-
}
113-
114-
}
115-
11633
private static OutputStream failingOutputStream() {
11734
return new OutputStream() {
11835
@Override
@@ -140,46 +57,4 @@ public static PrintWriter failingPrintWriter() {
14057
return new PrintWriter(failingOutputStream());
14158
}
14259

143-
/**
144-
* Execute the given runnable in a context, where each attempt to
145-
* write to stderr will fail with an {@link IOException}
146-
*
147-
* @param runnable the runnable to be executed.
148-
*/
149-
public static void withFailingErrOut(Runnable runnable) {
150-
Output.ERR.failOnWrite(runnable);
151-
}
152-
153-
/**
154-
* Execute the given runnable in a context, where each attempt to
155-
* write to stdout will fail with an {@link IOException}
156-
*
157-
* @param runnable the runnable to be executed.
158-
*/
159-
160-
public static void withFailingStdOut(Runnable runnable) {
161-
Output.OUT.failOnWrite(runnable);
162-
}
163-
164-
/**
165-
* Execute the given runnable and capture everything that written
166-
* to stdout. The written text is normalized to unix line feeds before its returned.
167-
*
168-
* @param runnable the runnable to be executed.
169-
* @return the content written to stdout, normalized to unix line endings.
170-
*/
171-
public static String captureStdOut(Runnable runnable) {
172-
return Output.OUT.capture(runnable);
173-
}
174-
175-
/**
176-
* Execute the given runnable and capture everything that written
177-
* to stderr. The written text is normalized to unix line feeds before its returned.
178-
*
179-
* @param runnable the runnable to be executed.
180-
* @return the content written to stderr, normalized to unix line endings.
181-
*/
182-
public static String captureErrOut(Runnable runnable) {
183-
return Output.ERR.capture(runnable);
184-
}
18560
}

vavr/src/test/java/io/vavr/collection/AbstractTraversableTest.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919
package io.vavr.collection;
2020

21-
import io.vavr.AbstractValueTest;
22-
import io.vavr.PartialFunction;
23-
import io.vavr.Tuple;
24-
import io.vavr.Tuple2;
21+
import io.vavr.*;
2522
import io.vavr.control.Option;
2623
import java.io.ByteArrayOutputStream;
2724
import java.io.PrintStream;
@@ -46,12 +43,8 @@
4643
import static io.vavr.API.Case;
4744
import static io.vavr.API.List;
4845
import static io.vavr.API.Map;
49-
import static io.vavr.OutputTester.captureErrOut;
50-
import static io.vavr.OutputTester.captureStdOut;
5146
import static io.vavr.OutputTester.failingPrintStream;
5247
import static io.vavr.OutputTester.failingPrintWriter;
53-
import static io.vavr.OutputTester.withFailingErrOut;
54-
import static io.vavr.OutputTester.withFailingStdOut;
5548
import static java.lang.System.lineSeparator;
5649
import static java.util.Arrays.asList;
5750
import static java.util.Comparator.comparingInt;
@@ -2115,32 +2108,11 @@ public void shouldHaveImmutableSpliterator() {
21152108
assertThat(of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.IMMUTABLE)).isTrue();
21162109
}
21172110

2118-
// -- stderr
2119-
2120-
@TestTemplate
2121-
public void shouldWriteToStderr() {
2122-
assertThat(captureErrOut(() -> of(1, 2, 3).stderr())).isEqualTo("1\n" +
2123-
"2\n" +
2124-
"3\n");
2125-
}
2126-
2127-
@TestTemplate
2128-
public void shouldHandleStderrIOException() {
2129-
assertThrows(IllegalStateException.class, () -> withFailingErrOut(() -> of(0).stderr()));
2130-
}
2131-
2132-
// -- stdout
2133-
2134-
@TestTemplate
2135-
public void shouldWriteToStdout() {
2136-
assertThat(captureStdOut(() -> of(1, 2, 3).stdout())).isEqualTo("1\n" +
2137-
"2\n" +
2138-
"3\n");
2139-
}
2111+
// -- out
21402112

21412113
@TestTemplate
21422114
public void shouldHandleStdoutIOException() {
2143-
assertThrows(IllegalStateException.class, () -> withFailingStdOut(() -> of(0).stdout()));
2115+
assertThrows(IllegalStateException.class, () -> of(0).out(OutputTester.failingPrintStream()));
21442116
}
21452117

21462118
// -- PrintStream

vavr/src/test/java/io/vavr/collection/CharSeqTest.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package io.vavr.collection;
2020

21+
import io.vavr.OutputTester;
2122
import io.vavr.Serializables;
2223
import io.vavr.Tuple;
2324
import io.vavr.Tuple2;
@@ -42,10 +43,6 @@
4243
import org.assertj.core.api.StringAssert;
4344
import org.junit.jupiter.api.Test;
4445

45-
import static io.vavr.OutputTester.captureErrOut;
46-
import static io.vavr.OutputTester.captureStdOut;
47-
import static io.vavr.OutputTester.withFailingErrOut;
48-
import static io.vavr.OutputTester.withFailingStdOut;
4946
import static java.util.Arrays.asList;
5047
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5148
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -1768,32 +1765,11 @@ public void shouldStartsNonNilOfNonNilWithOffsetCalculate() {
17681765
assertThat(CharSeq.of('a', 'b', 'c').startsWith(CharSeq.of('b', 'd'), 1)).isFalse();
17691766
}
17701767

1771-
// -- stderr
1772-
1773-
@Test
1774-
public void shouldWriteToStderr() {
1775-
assertThat(captureErrOut(() -> CharSeq.of('1', '2', '3').stderr())).isEqualTo("1\n" +
1776-
"2\n" +
1777-
"3\n");
1778-
}
1779-
1780-
@Test
1781-
public void shouldHandleStderrIOException() {
1782-
assertThrows(IllegalStateException.class, () -> withFailingErrOut(() -> CharSeq.of('0').stderr()));
1783-
}
1784-
1785-
// -- stdout
1786-
1787-
@Test
1788-
public void shouldWriteToStdout() {
1789-
assertThat(captureStdOut(() -> CharSeq.of('1', '2', '3').stdout())).isEqualTo("1\n" +
1790-
"2\n" +
1791-
"3\n");
1792-
}
1768+
// -- out
17931769

17941770
@Test
17951771
public void shouldHandleStdoutIOException() {
1796-
assertThrows(IllegalStateException.class, () -> withFailingStdOut(() -> CharSeq.of('0').stdout()));
1772+
assertThrows(IllegalStateException.class, () -> CharSeq.of('0').out(OutputTester.failingPrintStream()));
17971773
}
17981774

17991775
// -- sum

0 commit comments

Comments
 (0)