-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for input stream handling #3406
- Loading branch information
1 parent
bf8e14a
commit eb8ba52
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
.../model/src/test/kotlin/de/maibornwolff/codecharta/serialization/PipedInputReaderKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String>() | ||
|
||
// 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) | ||
} | ||
} |