diff --git a/analysis/model/src/test/kotlin/de/maibornwolff/codecharta/serialization/PipedInputReaderKtTest.kt b/analysis/model/src/test/kotlin/de/maibornwolff/codecharta/serialization/PipedInputReaderKtTest.kt new file mode 100644 index 00000000000..3680bf44432 --- /dev/null +++ b/analysis/model/src/test/kotlin/de/maibornwolff/codecharta/serialization/PipedInputReaderKtTest.kt @@ -0,0 +1,38 @@ +package de.maibornwolff.codecharta.serialization + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.io.PipedInputStream +import java.io.PipedOutputStream +import java.nio.charset.StandardCharsets + +class PipedInputReaderTest { + @Test + fun `forEachLine should not wait for data when handling blocking input stream`() { + // given + val line1 = "line1" + val line2 = "line2" + val newLine = "\n" + val expectedLines = listOf(line1) + val inputStream = PipedInputStream() + val outputStream = PipedOutputStream(inputStream) + outputStream.write(line1.toByteArray(StandardCharsets.UTF_8)) + outputStream.write(newLine.toByteArray(StandardCharsets.UTF_8)) + val lines = mutableListOf() + + // when + Thread { + Thread.sleep(5000) + outputStream.write(line2.toByteArray(StandardCharsets.UTF_8)) + outputStream.write(newLine.toByteArray(StandardCharsets.UTF_8)) + outputStream.close() + }.start() + + inputStream.forEachLine { + lines.add(it) + } + + // then + assertEquals(expectedLines, lines) + } +}