Skip to content

Commit 088ef53

Browse files
author
Sergey Mashkov
committed
Fix error "Unable to stop reading in state Writing"
1 parent 7b1b563 commit 088ef53

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

kotlinx-coroutines-io/kotlinx-coroutines-io-jvm/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteBufferChannel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,9 @@ internal class ByteBufferChannel(
18261826
if (closed != null || state === ReadWriteBufferState.Terminated) return visitor(TerminatedLookAhead)
18271827
result = visitor(this)
18281828
if (!state.idle && state !== ReadWriteBufferState.Terminated) {
1829-
restoreStateAfterRead()
1829+
if (state is ReadWriteBufferState.Reading || state is ReadWriteBufferState.ReadingWriting) {
1830+
restoreStateAfterRead()
1831+
}
18301832
tryTerminate()
18311833
}
18321834
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package kotlinx.coroutines.experimental.io
2+
3+
import kotlinx.coroutines.io.*
4+
import kotlin.test.*
5+
6+
class ByteBufferChannelLookAheadTest : ByteChannelTestBase() {
7+
@Test
8+
fun testDoNothing() = runTest {
9+
ch.lookAheadSuspend {
10+
}
11+
}
12+
13+
@Test
14+
fun testDoNothingWhileWriting() = runTest {
15+
ch.writeSuspendSession {
16+
ch.lookAheadSuspend {
17+
}
18+
}
19+
}
20+
21+
@Test
22+
fun testDoNothingWhileWriting2() = runTest {
23+
ch.lookAheadSuspend {
24+
ch.writeSuspendSession {
25+
}
26+
}
27+
}
28+
29+
@Test
30+
fun testReadDuringWriting() = runTest {
31+
ch.writeSuspendSession {
32+
ch.lookAheadSuspend {
33+
this@writeSuspendSession.request(1)!!.writeInt(777)
34+
written(4)
35+
flush()
36+
37+
val bb = request(0, 1)
38+
assertNotNull(bb)
39+
assertEquals(777, bb.getInt())
40+
consumed(4)
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)