Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 3 | import kotlinx.coroutines.experimental.* |
| 4 | import kotlinx.coroutines.experimental.io.internal.* |
| 5 | import org.junit.* |
Roman Elizarov | 75675e6 | 2017-11-30 15:14:44 +0300 | [diff] [blame] | 6 | import org.junit.Test |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import org.junit.rules.* |
| 8 | import kotlin.coroutines.experimental.* |
| 9 | import kotlin.test.* |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 10 | |
| 11 | class CopyAndCloseNoAutoFlushTest : TestBase() { |
| 12 | private val verifyingPool = VerifyingObjectPool(BufferObjectPool) |
| 13 | |
| 14 | private val from = ByteBufferChannel(true, verifyingPool) |
| 15 | private val to = ByteBufferChannel(false, verifyingPool) |
| 16 | |
| 17 | @get:Rule |
| 18 | val pool get() = verifyingPool as TestRule |
| 19 | |
| 20 | @Test |
| 21 | fun smokeTest() = runBlocking { |
| 22 | expect(1) |
| 23 | |
| 24 | launch(coroutineContext) { |
| 25 | expect(2) |
| 26 | val copied = from.copyAndClose(to) // should suspend |
| 27 | |
| 28 | expect(7) |
| 29 | |
| 30 | assertEquals(8, copied) |
| 31 | } |
| 32 | |
| 33 | yield() |
| 34 | |
| 35 | expect(3) |
| 36 | from.writeInt(1) |
| 37 | expect(4) |
| 38 | |
| 39 | yield() |
| 40 | assertEquals(4, to.availableForRead) // 4 bytes need to be copied in spite of that there is no autoFlush |
| 41 | |
| 42 | from.writeInt(2) |
| 43 | expect(5) |
| 44 | |
| 45 | yield() |
| 46 | expect(6) |
| 47 | |
| 48 | from.close() |
| 49 | yield() |
| 50 | |
| 51 | assertTrue { to.isClosedForWrite } |
| 52 | to.readPacket(8).release() |
| 53 | assertTrue { to.isClosedForRead } |
| 54 | |
| 55 | finish(8) |
| 56 | } |
| 57 | } |