-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/test/initialcapacity/streaming/waitfor/WaitForTest.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,42 @@ | ||
package test.initialcapacity.streaming.waitfor | ||
|
||
import io.initialcapacity.streaming.waitfor.waitFor | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.Writer | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class WaitForTest { | ||
@Test | ||
fun testWaitFor() = runBlocking { | ||
val messages = Channel<String>() | ||
val message = async { messages.receive() } | ||
val writer = TestWriter() | ||
|
||
writer.write("Start") | ||
val waited = async { writer.write(writer.waitFor(message)) } | ||
|
||
messages.send("End") | ||
waited.await() | ||
assertEquals(listOf("Start", "flush", "End"), writer.actions) | ||
} | ||
} | ||
|
||
class TestWriter : Writer() { | ||
val actions = mutableListOf<String>() | ||
|
||
override fun close() { | ||
actions.addLast("close") | ||
} | ||
|
||
override fun flush() { | ||
actions.addLast("flush") | ||
} | ||
|
||
override fun write(cbuf: CharArray, off: Int, len: Int) { | ||
actions.addLast(String(cbuf, off, len)) | ||
} | ||
} |