Skip to content

Commit 2c3a0dc

Browse files
author
Sergey Mashkov
committed
IO: add byte channel readRemaining test
1 parent f34175e commit 2c3a0dc

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ internal class ByteBufferChannel(
231231
}
232232
}
233233

234-
private fun readAsMuchAsPossible(dst: ByteBuffer, consumed0: Int = 0): Int {
234+
private tailrec fun readAsMuchAsPossible(dst: ByteBuffer, consumed0: Int = 0): Int {
235235
var consumed = 0
236236

237237
val rc = reading {
@@ -259,7 +259,7 @@ internal class ByteBufferChannel(
259259
else consumed + consumed0
260260
}
261261

262-
private fun readAsMuchAsPossible(dst: ByteArray, offset: Int, length: Int, consumed0: Int = 0): Int {
262+
private tailrec fun readAsMuchAsPossible(dst: ByteArray, offset: Int, length: Int, consumed0: Int = 0): Int {
263263
var consumed = 0
264264

265265
val rc = reading {

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteReadChannel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ suspend fun ByteReadChannel.copyAndClose(dst: ByteWriteChannel): Long {
159159
* Reads all the bytes from receiver channel and builds a packet that is returned unless the specified [limit] exceeded.
160160
* It will simply stop reading and return packet of size [limit] in this case
161161
*/
162-
suspend fun ByteReadChannel.readAll(limit: Int = Int.MAX_VALUE): ByteReadPacket {
162+
suspend fun ByteReadChannel.readRemaining(limit: Int = Int.MAX_VALUE): ByteReadPacket {
163163
val buffer = BufferPool.borrow()
164164
val packet = WritePacket()
165165

core/kotlinx-coroutines-io/src/test/kotlin/kotlinx/coroutines/experimental/io/CopyAndCloseTest.kt

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package kotlinx.coroutines.experimental.io
22

3-
import kotlinx.coroutines.experimental.TestBase
4-
import kotlinx.coroutines.experimental.launch
5-
import kotlinx.coroutines.experimental.runBlocking
6-
import kotlinx.coroutines.experimental.yield
7-
import org.junit.Test
8-
import java.io.IOException
9-
import kotlin.test.assertEquals
10-
import kotlin.test.fail
3+
import kotlinx.coroutines.experimental.*
4+
import kotlinx.coroutines.experimental.io.internal.*
5+
import org.junit.*
6+
import java.io.*
7+
import kotlin.test.*
118

129
class CopyAndCloseTest : TestBase() {
1310
private val from = ByteChannel(true)
1411
private val to = ByteChannel(true)
1512

13+
@After
14+
fun tearDown() {
15+
from.close(CancellationException())
16+
to.close(CancellationException())
17+
}
18+
1619
@Test
1720
fun smokeTest() = runBlocking {
1821
expect(1)
@@ -101,4 +104,50 @@ class CopyAndCloseTest : TestBase() {
101104

102105
finish(6)
103106
}
107+
108+
@Test
109+
fun readRemaining() = runBlocking {
110+
expect(1)
111+
112+
launch(coroutineContext) {
113+
expect(2)
114+
from.writeFully("123".toByteArray())
115+
116+
yield()
117+
expect(3)
118+
from.writeFully("456".toByteArray().asByteBuffer())
119+
120+
yield()
121+
expect(4)
122+
from.close()
123+
}
124+
125+
yield()
126+
assertEquals("123456", from.readRemaining().readText().toString())
127+
128+
yield()
129+
130+
finish(5)
131+
}
132+
133+
@Test
134+
fun readRemainingLimitFailed() = runBlocking {
135+
expect(1)
136+
137+
launch(coroutineContext) {
138+
expect(2)
139+
from.writeFully("123".toByteArray())
140+
141+
yield()
142+
expect(3)
143+
from.writeFully("456".toByteArray().asByteBuffer())
144+
}
145+
146+
yield()
147+
assertEquals("12345", from.readRemaining(5).readText().toString())
148+
149+
finish(4)
150+
}
151+
152+
104153
}

0 commit comments

Comments
 (0)