|
5 | 5 | import org.junit.jupiter.params.ParameterizedTest; |
6 | 6 | import org.junit.jupiter.params.provider.ValueSource; |
7 | 7 |
|
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.lang.foreign.Arena; |
| 11 | +import java.lang.foreign.MemorySegment; |
8 | 12 | import java.nio.charset.StandardCharsets; |
9 | 13 |
|
| 14 | +import static io.github.dfa1.zstd.ZstdTestSupport.bytesOf; |
| 15 | +import static io.github.dfa1.zstd.ZstdTestSupport.segmentOf; |
10 | 16 | import static org.assertj.core.api.Assertions.assertThat; |
11 | 17 |
|
12 | 18 | /// Multithreaded compression via `NB_WORKERS`. zstd engages workers only when |
@@ -77,6 +83,50 @@ void honorsJobSizeAndOverlapLog() { |
77 | 83 | } |
78 | 84 | } |
79 | 85 |
|
| 86 | + @Nested |
| 87 | + class SegmentStreamRoundTrip { |
| 88 | + |
| 89 | + @ParameterizedTest |
| 90 | + @ValueSource(ints = {1, 2}) |
| 91 | + void roundTripsWithWorkers(int nbWorkers) { |
| 92 | + // Given a segment stream configured with worker threads |
| 93 | + byte[] frame; |
| 94 | + try (Arena arena = Arena.ofConfined(); |
| 95 | + ZstdCompressStream sut = new ZstdCompressStream()) { |
| 96 | + sut.parameter(ZstdCompressParameter.NB_WORKERS, nbWorkers); |
| 97 | + |
| 98 | + // When compressing a payload large enough to engage the workers |
| 99 | + MemorySegment src = segmentOf(arena, LARGE_PAYLOAD); |
| 100 | + MemorySegment dst = arena.allocate(Zstd.compressBound(LARGE_PAYLOAD.length)); |
| 101 | + ZstdStreamResult r = sut.compress(dst, src, ZstdEndDirective.END); |
| 102 | + frame = bytesOf(dst, r.bytesProduced()); |
| 103 | + } |
| 104 | + |
| 105 | + // Then the frame decompresses back to the original |
| 106 | + assertThat(Zstd.decompress(frame)).isEqualTo(LARGE_PAYLOAD); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Nested |
| 111 | + class OutputStreamRoundTrip { |
| 112 | + |
| 113 | + @ParameterizedTest |
| 114 | + @ValueSource(ints = {1, 2}) |
| 115 | + void roundTripsWithWorkers(int nbWorkers) throws IOException { |
| 116 | + // Given an output stream configured with worker threads |
| 117 | + ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 118 | + try (ZstdOutputStream sut = new ZstdOutputStream(sink)) { |
| 119 | + sut.parameter(ZstdCompressParameter.NB_WORKERS, nbWorkers); |
| 120 | + |
| 121 | + // When writing a payload large enough to engage the workers |
| 122 | + sut.write(LARGE_PAYLOAD); |
| 123 | + } |
| 124 | + |
| 125 | + // Then the frame decompresses back to the original |
| 126 | + assertThat(Zstd.decompress(sink.toByteArray(), LARGE_PAYLOAD.length)).isEqualTo(LARGE_PAYLOAD); |
| 127 | + } |
| 128 | + } |
| 129 | + |
80 | 130 | @Nested |
81 | 131 | class Lifecycle { |
82 | 132 |
|
|
0 commit comments