Skip to content

Commit

Permalink
Flush channel to observe cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed May 7, 2024
1 parent 9aeaa24 commit 9f08883
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ktor-utils/jvm/test/io/ktor/tests/utils/DeflaterReadChannelTest.kt
Expand Up @@ -15,7 +15,6 @@ import java.nio.*
import java.util.zip.*
import kotlin.random.*
import kotlin.test.*
import kotlin.test.Test

@CoroutinesTimeout(60_000)
class DeflaterReadChannelTest : CoroutineScope {
Expand Down Expand Up @@ -123,7 +122,17 @@ class DeflaterReadChannelTest : CoroutineScope {
testFaultyWriteChannel(asyncOf(text))
}

private fun asyncOf(text: String): ByteReadChannel = asyncOf(ByteBuffer.wrap(text.toByteArray(Charsets.ISO_8859_1)))
@OptIn(DelicateCoroutinesApi::class)
private fun asyncOf(text: String): ByteReadChannel = GlobalScope.writer {
var current = 0
while (current < text.length) {
val size = Random.nextInt(1, 16)
val end = minOf(current + size, text.length)
channel.writeFully(text.substring(current, end).toByteArray(Charsets.UTF_8))
current = end
}
}.channel

private fun asyncOf(bb: ByteBuffer): ByteReadChannel = ByteReadChannel(bb)

private fun InputStream.ungzip() = GZIPInputStream(this)
Expand Down

0 comments on commit 9f08883

Please sign in to comment.