From eb8ba52289013867bccb354997b906dbe29d3650 Mon Sep 17 00:00:00 2001 From: Ludwig Fritsch Date: Thu, 30 Nov 2023 11:22:18 +0100 Subject: [PATCH] Add test cases for input stream handling #3406 --- .../serialization/PipedInputReaderKtTest.kt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 analysis/model/src/test/kotlin/de/maibornwolff/codecharta/serialization/PipedInputReaderKtTest.kt 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) + } +}