Skip to content

Commit a3035d4

Browse files
authored
test: parameterize closed stream operation tests (#61)
1 parent fde1a56 commit a3035d4

1 file changed

Lines changed: 35 additions & 37 deletions

File tree

zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.jupiter.api.Nested;
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
79
import org.junit.jupiter.params.provider.ValueSource;
810

911
import java.io.ByteArrayInputStream;
@@ -14,6 +16,7 @@
1416
import java.lang.foreign.MemorySegment;
1517
import java.nio.ByteBuffer;
1618
import java.nio.charset.StandardCharsets;
19+
import java.util.stream.Stream;
1720

1821
import static io.github.dfa1.zstd.ZstdTestSupport.randomBytes;
1922
import static io.github.dfa1.zstd.ZstdTestSupport.sample;
@@ -290,35 +293,20 @@ void closeFlushesAndClosesTheUnderlyingStreamExactlyOnce() throws IOException {
290293
assertThat(streamDecompress(sink.toByteArray())).isEqualTo(original);
291294
}
292295

293-
@Test
294-
void writeAfterCloseThrows() throws IOException {
296+
@ParameterizedTest(name = "{0}")
297+
@MethodSource("io.github.dfa1.zstd.ZstdStreamTest#closedStreamOperations")
298+
void operationAfterCloseThrows(String name, ClosedStreamOperation operation) throws IOException {
295299
// Given a closed stream
296-
ZstdOutputStream zout = new ZstdOutputStream(new ByteArrayOutputStream());
297-
zout.close();
300+
ThrowingCallable closedStreamOperation = operation.closeThenOperate();
298301

299-
// When written to
300-
ThrowingCallable result = () -> zout.write(1);
302+
// When operating on it
303+
ThrowingCallable result = closedStreamOperation;
301304

302305
// Then it refuses with an IOException rather than touching freed native state
303306
assertThatThrownBy(result)
304307
.isInstanceOf(IOException.class)
305308
.hasMessageContaining("closed");
306309
}
307-
308-
@Test
309-
void flushAfterCloseThrows() throws IOException {
310-
// Given a closed stream
311-
ZstdOutputStream zout = new ZstdOutputStream(new ByteArrayOutputStream());
312-
zout.close();
313-
314-
// When flushed
315-
ThrowingCallable result = zout::flush;
316-
317-
// Then it refuses with an IOException
318-
assertThatThrownBy(result)
319-
.isInstanceOf(IOException.class)
320-
.hasMessageContaining("closed");
321-
}
322310
}
323311

324312
@Nested
@@ -357,22 +345,6 @@ void readPastEndOfStreamStaysMinusOne() throws IOException {
357345
}
358346
}
359347

360-
@Test
361-
void readAfterCloseThrows() throws IOException {
362-
// Given a closed input stream
363-
byte[] frame = streamCompress("payload".getBytes(StandardCharsets.UTF_8), 3);
364-
ZstdInputStream zin = new ZstdInputStream(new ByteArrayInputStream(frame));
365-
zin.close();
366-
367-
// When read
368-
ThrowingCallable result = zin::read;
369-
370-
// Then it refuses with an IOException rather than touching freed native state
371-
assertThatThrownBy(result)
372-
.isInstanceOf(IOException.class)
373-
.hasMessageContaining("closed");
374-
}
375-
376348
@Test
377349
void closeClosesTheUnderlyingStreamExactlyOnce() throws IOException {
378350
// Given an input stream over a source that tracks close()
@@ -422,6 +394,32 @@ void readsCorrectlyWhenInputArrivesOneByteAtATime(int size) throws IOException {
422394
}
423395
}
424396

397+
private static Stream<Arguments> closedStreamOperations() {
398+
return Stream.of(
399+
Arguments.of("write after close", (ClosedStreamOperation) () -> {
400+
ZstdOutputStream stream = new ZstdOutputStream(new ByteArrayOutputStream());
401+
stream.close();
402+
return () -> stream.write(1);
403+
}),
404+
Arguments.of("flush after close", (ClosedStreamOperation) () -> {
405+
ZstdOutputStream stream = new ZstdOutputStream(new ByteArrayOutputStream());
406+
stream.close();
407+
return stream::flush;
408+
}),
409+
Arguments.of("read after close", (ClosedStreamOperation) () -> {
410+
byte[] frame = streamCompress("payload".getBytes(StandardCharsets.UTF_8), 3);
411+
ZstdInputStream stream = new ZstdInputStream(new ByteArrayInputStream(frame));
412+
stream.close();
413+
return stream::read;
414+
})
415+
);
416+
}
417+
418+
@FunctionalInterface
419+
private interface ClosedStreamOperation {
420+
ThrowingCallable closeThenOperate() throws IOException;
421+
}
422+
425423
/// A sink that records flush/close calls while retaining the bytes written to it
426424
/// (a [ByteArrayOutputStream] whose close is a no-op, so bytes stay readable).
427425
private static final class CountingOutputStream extends ByteArrayOutputStream {

0 commit comments

Comments
 (0)