Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
Roman Elizarov | c45f5fa | 2018-01-11 13:48:19 +0300 | [diff] [blame] | 3 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 4 | import kotlinx.coroutines.experimental.io.internal.ReadWriteBufferState |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 5 | import kotlinx.io.pool.* |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 6 | import org.junit.After |
| 7 | import org.junit.Test |
Roman Elizarov | 96d7a88 | 2017-08-30 15:17:05 +0300 | [diff] [blame] | 8 | import java.io.IOException |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 9 | import java.nio.ByteBuffer |
| 10 | import java.util.concurrent.CopyOnWriteArrayList |
| 11 | import kotlin.test.assertEquals |
| 12 | import kotlin.test.assertTrue |
| 13 | import kotlin.test.fail |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 14 | |
Roman Elizarov | c45f5fa | 2018-01-11 13:48:19 +0300 | [diff] [blame] | 15 | class PooledBufferTest : TestBase() { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 16 | private val allocated = CopyOnWriteArrayList<ByteBuffer>() |
| 17 | |
| 18 | private inner class TestPool : ObjectPool<ReadWriteBufferState.Initial> { |
| 19 | override val capacity: Int get() = 0 |
| 20 | |
| 21 | override fun borrow(): ReadWriteBufferState.Initial { |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 22 | val buffer = ReadWriteBufferState.Initial(ByteBuffer.allocate(4096)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 23 | allocated.add(buffer.backingBuffer) |
| 24 | return buffer |
| 25 | } |
| 26 | |
| 27 | override fun recycle(instance: ReadWriteBufferState.Initial) { |
| 28 | if (!allocated.remove(instance.backingBuffer)) { |
| 29 | fail("Couldn't release buffer from pool") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | override fun dispose() { |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private val channel = ByteBufferChannel(autoFlush = true, pool = TestPool()) |
| 38 | |
| 39 | @After |
| 40 | fun tearDown() { |
| 41 | assertTrue { allocated.isEmpty() } |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | fun testWriteReadClose() { |
| 46 | runBlocking { |
| 47 | channel.writeInt(1) |
| 48 | assertEquals(1, allocated.size) |
| 49 | channel.readInt() |
| 50 | channel.close() |
| 51 | assertEquals(0, allocated.size) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun testWriteCloseRead() { |
| 57 | runBlocking { |
| 58 | channel.writeInt(1) |
| 59 | assertEquals(1, allocated.size) |
| 60 | channel.close() |
| 61 | channel.readInt() |
| 62 | assertEquals(0, allocated.size) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | fun testWriteCloseReadRead() { |
| 68 | runBlocking { |
| 69 | channel.writeInt(1) |
| 70 | assertEquals(1, allocated.size) |
| 71 | channel.close() |
| 72 | channel.readShort() |
| 73 | assertEquals(1, allocated.size) |
| 74 | channel.readShort() |
| 75 | assertEquals(0, allocated.size) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | fun testCloseOnly() { |
| 81 | runBlocking { |
| 82 | channel.close() |
| 83 | assertEquals(0, allocated.size) |
| 84 | } |
| 85 | } |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 86 | |
| 87 | @Test |
Sergey Mashkov | 9a6ca1a | 2017-11-20 11:43:26 +0300 | [diff] [blame] | 88 | fun testCloseWithError() { |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 89 | runBlocking { |
| 90 | channel.writeFully("OK".toByteArray()) |
| 91 | assertEquals(1, allocated.size) |
| 92 | channel.close(IOException()) |
| 93 | assertEquals(0, allocated.size) |
| 94 | } |
| 95 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 96 | } |