-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7952 Fix for uninitialized writerJob property
- Loading branch information
Showing
2 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
ktor-client/ktor-client-core/common/test/ByteChannelReplayTest.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,70 @@ | ||
import io.ktor.client.plugins.internal.* | ||
import io.ktor.utils.io.* | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.yield | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
internal class ByteChannelReplayTest { | ||
|
||
private val size = 1024 * 1024 + 1 | ||
private val expectedByte = 'A'.code.toByte() | ||
private val expected = ByteArray(size).apply { fill(expectedByte) } | ||
private lateinit var channelReplay: ByteChannelReplay | ||
|
||
@BeforeTest | ||
fun setup() { | ||
channelReplay = ByteChannelReplay(ByteReadChannel(expected)) | ||
} | ||
|
||
@Test | ||
fun readFirst() = runTest { | ||
val first = channelReplay.replay() | ||
assertRead(first) | ||
val second = channelReplay.replay() | ||
assertRead(second) | ||
assertTrue(second.isClosedForRead) | ||
assertTrue(first.isClosedForRead) | ||
} | ||
|
||
@Test | ||
fun readSecond() = runTest { | ||
val first = channelReplay.replay() | ||
val second = channelReplay.replay() | ||
assertRead(second) | ||
assertTrue(second.isClosedForRead) | ||
assertTrue(first.isClosedForRead) | ||
} | ||
|
||
@Test | ||
fun readABunch() = runTest { | ||
val jobs = (0..10).map { | ||
launch { | ||
val readChannel = channelReplay.replay() | ||
yield() | ||
try { | ||
assertRead(readChannel) | ||
} catch (e: Exception) { | ||
assertEquals("Save body abandoned", e.message) | ||
} | ||
assertTrue(readChannel.isClosedForRead || readChannel.exhausted()) | ||
} | ||
} | ||
joinAll(*jobs.toTypedArray()) | ||
} | ||
|
||
private suspend fun assertRead(readChannel: ByteReadChannel) { | ||
repeat(size) { i -> | ||
assertEquals(expectedByte, readChannel.readByte(), "Incorrect byte at index $i") | ||
} | ||
} | ||
|
||
} |