Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public suspend fun ByteReadChannel.copyTo(channel: ByteWriteChannel): Long {
channel.flush()
}

rethrowCloseCauseIfNeeded()
return result
}

Expand Down Expand Up @@ -216,6 +217,7 @@ public suspend fun ByteReadChannel.copyTo(channel: ByteWriteChannel, limit: Long
channel.flush()
}

rethrowCloseCauseIfNeeded()
return limit - remaining
}

Expand Down
36 changes: 36 additions & 0 deletions ktor-io/common/test/ByteReadChannelOperationsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ class ByteReadChannelOperationsTest {
}
}

@Test
fun `copyTo propagates closedCause from cancelled source`() = runTest {
val src = ByteChannel()
val dst = ByteChannel()
src.writeFully(byteArrayOf(1, 2, 3))
src.flush()
src.cancel(IOException("source cancelled"))
assertFailsWith<IOException> {
src.copyTo(dst)
}
assertTrue(src.isClosedForRead)
}

@Test
fun `copyTo with limit propagates closedCause from cancelled source`() = runTest {
val src = ByteChannel()
val dst = ByteChannel()
src.writeFully(byteArrayOf(1, 2, 3))
src.flush()
src.cancel(IOException("source cancelled"))
assertFailsWith<IOException> {
src.copyTo(dst, limit = 1024L)
}
assertTrue(src.isClosedForRead)
}

@Test
fun `copyTo does not throw on normal close`() = runTest {
val src = ByteChannel()
val dst = ByteChannel()
src.writeFully(byteArrayOf(1, 2, 3))
src.flushAndClose()
val copied = src.copyTo(dst)
assertEquals(3, copied)
}

@Test
fun readFully() = runTest {
val expected = ByteArray(10) { it.toByte() }
Expand Down