Skip to content

Commit 7db74d7

Browse files
author
Sergey Mashkov
committed
Fix await returned wrong result in sequential implementation (#24)
1 parent 770993d commit 7db74d7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

kotlinx-coroutines-io/common/src/kotlinx/coroutines/io/ByteChannelSequential.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ abstract class ByteChannelSequentialBase(initial: IoBuffer, override val autoFlu
493493
waitingForRead = atLeast
494494
atLeastNBytesAvailableForRead.await { afterRead() }
495495
closedCause?.let { throw it }
496-
return !isClosedForRead
496+
return !isClosedForRead && availableForRead >= atLeast
497497
}
498498

499499
override fun discard(n: Int): Int {

kotlinx-coroutines-io/common/test/kotlinx/coroutines/io/ByteChannelSmokeTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,29 @@ open class ByteChannelSmokeTest : ByteChannelTestBase() {
647647
}
648648
}
649649

650+
@Test
651+
fun testReadSuspendSessionAwait() = runTest {
652+
launch {
653+
expect(2)
654+
ch.writeInt(1)
655+
ch.flush()
656+
expect(3)
657+
ch.close()
658+
}
659+
660+
var result: Boolean? = null
661+
ch.readSuspendableSession {
662+
expect(1)
663+
result = await(100000)
664+
}
665+
666+
expect(4)
667+
assertEquals(false, result)
668+
assertEquals(4, ch.availableForRead)
669+
ch.discard(4)
670+
finish(5)
671+
}
672+
650673
private fun assertEquals(expected: Float, actual: Float) {
651674
if (abs(expected - actual) > 0.000001f) {
652675
kotlin.test.assertEquals(expected, actual)

0 commit comments

Comments
 (0)