Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 3 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 4 | import kotlinx.coroutines.experimental.CancellationException |
| 5 | import kotlinx.coroutines.experimental.channels.* |
| 6 | import kotlinx.coroutines.experimental.io.internal.* |
| 7 | import kotlinx.coroutines.experimental.io.packet.* |
Sergey Mashkov | 50a0857 | 2017-10-09 12:56:07 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.io.packet.ByteReadPacket |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 9 | import kotlinx.io.core.* |
| 10 | import kotlinx.io.pool.* |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 11 | import org.junit.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 12 | import org.junit.Test |
| 13 | import org.junit.rules.* |
Roman Elizarov | 96d7a88 | 2017-08-30 15:17:05 +0300 | [diff] [blame] | 14 | import java.nio.CharBuffer |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 15 | import java.util.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 16 | import java.util.concurrent.* |
| 17 | import java.util.concurrent.atomic.* |
| 18 | import kotlin.coroutines.experimental.* |
| 19 | import kotlin.test.* |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 20 | |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 21 | class ByteBufferChannelTest : TestBase() { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 22 | @get:Rule |
Roman Elizarov | e084cf7 | 2018-01-11 10:09:58 +0300 | [diff] [blame] | 23 | val timeout = Timeout(100L * stressTestMultiplier, TimeUnit.SECONDS) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 24 | |
| 25 | @get:Rule |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 26 | private val failures = ErrorCollector() |
| 27 | |
| 28 | @get:Rule |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 29 | internal val pool = VerifyingObjectPool(object : NoPoolImpl<ReadWriteBufferState.Initial>() { |
| 30 | override fun borrow(): ReadWriteBufferState.Initial { |
| 31 | return ReadWriteBufferState.Initial(java.nio.ByteBuffer.allocate(4096)) |
| 32 | } |
| 33 | }) |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 34 | |
| 35 | @get:Rule |
Sergey Mashkov | ccf8dde | 2017-09-29 18:18:00 +0300 | [diff] [blame] | 36 | internal val pktPool = VerifyingObjectPool(BufferView.Pool) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 37 | |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 38 | private val Size = BUFFER_SIZE - RESERVED_SIZE |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 39 | private val ch = ByteBufferChannel(autoFlush = false, pool = pool) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 40 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 41 | @After |
| 42 | fun finish() { |
| 43 | ch.close(InterruptedException()) |
| 44 | } |
| 45 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 46 | @Test |
| 47 | fun testBoolean() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 48 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 49 | ch.writeBoolean(true) |
| 50 | ch.flush() |
| 51 | assertEquals(true, ch.readBoolean()) |
| 52 | |
| 53 | ch.writeBoolean(false) |
| 54 | ch.flush() |
| 55 | assertEquals(false, ch.readBoolean()) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | fun testByte() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 61 | runTest { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 62 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 63 | ch.writeByte(-1) |
| 64 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 65 | assertEquals(1, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 66 | assertEquals(-1, ch.readByte()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 67 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun testShortB() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 73 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 74 | ch.readByteOrder = ByteOrder.BIG_ENDIAN |
| 75 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 76 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 77 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 78 | ch.writeShort(-1) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 79 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 80 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 81 | assertEquals(2, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 82 | assertEquals(-1, ch.readShort()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 83 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | fun testShortL() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 89 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 90 | ch.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 91 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 92 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 93 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 94 | ch.writeShort(-1) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 95 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 96 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 97 | assertEquals(2, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 98 | assertEquals(-1, ch.readShort()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 99 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | fun testShortEdge() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 105 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 106 | ch.writeByte(1) |
| 107 | |
| 108 | for (i in 2 until Size step 2) { |
| 109 | ch.writeShort(0x00ee) |
| 110 | } |
| 111 | |
| 112 | ch.flush() |
| 113 | |
| 114 | ch.readByte() |
| 115 | ch.writeShort(0x1234) |
| 116 | |
| 117 | ch.flush() |
| 118 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 119 | while (ch.availableForRead > 2) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 120 | ch.readShort() |
| 121 | } |
| 122 | |
| 123 | assertEquals(0x1234, ch.readShort()) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | fun testIntB() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 129 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 130 | ch.readByteOrder = ByteOrder.BIG_ENDIAN |
| 131 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 132 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 133 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 134 | ch.writeInt(-1) |
| 135 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 136 | assertEquals(4, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 137 | assertEquals(-1, ch.readInt()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 138 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun testIntL() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 144 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 145 | ch.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 146 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 147 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 148 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 149 | ch.writeInt(-1) |
| 150 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 151 | assertEquals(4, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 152 | assertEquals(-1, ch.readInt()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 153 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | fun testIntEdge() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 159 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 160 | for (shift in 1..3) { |
| 161 | for (i in 1..shift) { |
| 162 | ch.writeByte(1) |
| 163 | } |
| 164 | |
| 165 | repeat(Size / 4 - 1) { |
| 166 | ch.writeInt(0xeeeeeeeeL) |
| 167 | } |
| 168 | |
| 169 | ch.flush() |
| 170 | |
| 171 | for (i in 1..shift) { |
| 172 | ch.readByte() |
| 173 | } |
| 174 | |
| 175 | ch.writeInt(0x12345678) |
| 176 | |
| 177 | ch.flush() |
| 178 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 179 | while (ch.availableForRead > 4) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 180 | ch.readInt() |
| 181 | } |
| 182 | |
| 183 | assertEquals(0x12345678, ch.readInt()) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | @Test |
Sergey Mashkov | a432a82 | 2018-05-21 12:50:14 +0300 | [diff] [blame] | 189 | fun testIntEdge2() { |
| 190 | runTest { |
| 191 | for (shift in 1..3) { |
| 192 | for (i in 1..shift) { |
| 193 | ch.writeByte(1) |
| 194 | } |
| 195 | |
| 196 | repeat(Size / 4 - 1) { |
| 197 | ch.writeInt(0xeeeeeeeeL) |
| 198 | } |
| 199 | |
| 200 | ch.flush() |
| 201 | |
| 202 | for (i in 1..shift) { |
| 203 | ch.readByte() |
| 204 | } |
| 205 | |
| 206 | ch.writeByte(0x12) |
| 207 | ch.writeByte(0x34) |
| 208 | ch.writeByte(0x56) |
| 209 | ch.writeByte(0x78) |
| 210 | |
| 211 | ch.flush() |
| 212 | |
| 213 | while (ch.availableForRead > 4) { |
| 214 | ch.readInt() |
| 215 | } |
| 216 | |
| 217 | assertEquals(0x12345678, ch.readInt()) |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | @Test |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 224 | fun testLongB() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 225 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 226 | ch.readByteOrder = ByteOrder.BIG_ENDIAN |
| 227 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 228 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 229 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 230 | ch.writeLong(Long.MIN_VALUE) |
| 231 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 232 | assertEquals(8, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 233 | assertEquals(Long.MIN_VALUE, ch.readLong()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 234 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | @Test |
| 239 | fun testLongL() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 240 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 241 | ch.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 242 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 243 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 244 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 245 | ch.writeLong(Long.MIN_VALUE) |
| 246 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 247 | assertEquals(8, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 248 | assertEquals(Long.MIN_VALUE, ch.readLong()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 249 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | @Test |
| 254 | fun testLongEdge() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 255 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 256 | for (shift in 1..7) { |
| 257 | for (i in 1..shift) { |
| 258 | ch.writeByte(1) |
| 259 | } |
| 260 | |
| 261 | repeat(Size / 8 - 1) { |
| 262 | ch.writeLong(0x11112222eeeeeeeeL) |
| 263 | } |
| 264 | |
| 265 | ch.flush() |
| 266 | for (i in 1..shift) { |
| 267 | ch.readByte() |
| 268 | } |
| 269 | |
| 270 | ch.writeLong(0x1234567812345678L) |
| 271 | ch.flush() |
| 272 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 273 | while (ch.availableForRead > 8) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 274 | ch.readLong() |
| 275 | } |
| 276 | |
| 277 | assertEquals(0x1234567812345678L, ch.readLong()) |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | @Test |
| 283 | fun testDoubleB() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 284 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 285 | ch.readByteOrder = ByteOrder.BIG_ENDIAN |
| 286 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 287 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 288 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 289 | ch.writeDouble(1.05) |
| 290 | ch.flush() |
| 291 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 292 | assertEquals(8, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 293 | assertEquals(1.05, ch.readDouble()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 294 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | @Test |
| 299 | fun testDoubleL() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 300 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 301 | ch.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 302 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 303 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 304 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 305 | ch.writeDouble(1.05) |
| 306 | ch.flush() |
| 307 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 308 | assertEquals(8, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 309 | assertEquals(1.05, ch.readDouble()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 310 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | @Test |
| 315 | fun testFloatB() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 316 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 317 | ch.readByteOrder = ByteOrder.BIG_ENDIAN |
| 318 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 319 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 320 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 321 | ch.writeFloat(1.05f) |
| 322 | ch.flush() |
| 323 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 324 | assertEquals(4, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 325 | assertEquals(1.05f, ch.readFloat()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 326 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | @Test |
| 331 | fun testFloatL() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 332 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 333 | ch.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 334 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 335 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 336 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 337 | ch.writeFloat(1.05f) |
| 338 | ch.flush() |
| 339 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 340 | assertEquals(4, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 341 | assertEquals(1.05f, ch.readFloat()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 342 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Roman Elizarov | fccac1d | 2017-08-15 11:58:43 +0300 | [diff] [blame] | 346 | |
| 347 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 348 | @Test |
| 349 | fun testEndianMix() { |
Roman Elizarov | fccac1d | 2017-08-15 11:58:43 +0300 | [diff] [blame] | 350 | val byteOrders = listOf(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN) |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 351 | runTest { |
Roman Elizarov | fccac1d | 2017-08-15 11:58:43 +0300 | [diff] [blame] | 352 | for (writeOrder in byteOrders) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 353 | ch.writeByteOrder = writeOrder |
| 354 | |
Roman Elizarov | fccac1d | 2017-08-15 11:58:43 +0300 | [diff] [blame] | 355 | for (readOrder in byteOrders) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 356 | ch.readByteOrder = readOrder |
| 357 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 358 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 359 | ch.writeShort(0x001f) |
| 360 | ch.flush() |
| 361 | if (writeOrder == readOrder) |
| 362 | assertEquals(0x001f, ch.readShort()) |
| 363 | else |
| 364 | assertEquals(0x1f00, ch.readShort()) |
| 365 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 366 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 367 | ch.writeShort(0x001f) |
| 368 | ch.flush() |
| 369 | if (writeOrder == readOrder) |
| 370 | assertEquals(0x001f, ch.readShort()) |
| 371 | else |
| 372 | assertEquals(0x1f00, ch.readShort()) |
| 373 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 374 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 375 | ch.writeInt(0x1f) |
| 376 | ch.flush() |
| 377 | if (writeOrder == readOrder) |
| 378 | assertEquals(0x0000001f, ch.readInt()) |
| 379 | else |
| 380 | assertEquals(0x1f000000, ch.readInt()) |
| 381 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 382 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 383 | ch.writeInt(0x1fL) |
| 384 | ch.flush() |
| 385 | if (writeOrder == readOrder) |
| 386 | assertEquals(0x0000001f, ch.readInt()) |
| 387 | else |
| 388 | assertEquals(0x1f000000, ch.readInt()) |
| 389 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 390 | assertEquals(0, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 391 | ch.writeLong(0x1f) |
| 392 | ch.flush() |
| 393 | if (writeOrder == readOrder) |
| 394 | assertEquals(0x1f, ch.readLong()) |
| 395 | else |
| 396 | assertEquals(0x1f00000000000000L, ch.readLong()) |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | @Test |
| 403 | fun testClose() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 404 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 405 | ch.writeByte(1) |
| 406 | ch.writeByte(2) |
| 407 | ch.writeByte(3) |
| 408 | |
| 409 | ch.flush() |
| 410 | assertEquals(1, ch.readByte()) |
| 411 | ch.close() |
| 412 | |
| 413 | assertEquals(2, ch.readByte()) |
| 414 | assertEquals(3, ch.readByte()) |
| 415 | |
| 416 | try { |
| 417 | ch.readByte() |
| 418 | fail() |
| 419 | } catch (expected: ClosedReceiveChannelException) { |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | @Test |
| 425 | fun testReadAndWriteFully() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 426 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 427 | val bytes = byteArrayOf(1, 2, 3, 4, 5) |
| 428 | val dst = ByteArray(5) |
| 429 | |
| 430 | ch.writeFully(bytes) |
| 431 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 432 | assertEquals(5, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 433 | ch.readFully(dst) |
| 434 | assertTrue { dst.contentEquals(bytes) } |
| 435 | |
| 436 | ch.writeFully(bytes) |
| 437 | ch.flush() |
| 438 | |
| 439 | val dst2 = ByteArray(4) |
| 440 | ch.readFully(dst2) |
| 441 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 442 | assertEquals(1, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 443 | assertEquals(5, ch.readByte()) |
| 444 | |
| 445 | ch.close() |
| 446 | |
| 447 | try { |
| 448 | ch.readFully(dst) |
| 449 | fail("") |
| 450 | } catch (expected: ClosedReceiveChannelException) { |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | @Test |
| 456 | fun testReadAndWriteFullyByteBuffer() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 457 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 458 | val bytes = byteArrayOf(1, 2, 3, 4, 5) |
| 459 | val dst = ByteArray(5) |
| 460 | |
| 461 | ch.writeFully(ByteBuffer.wrap(bytes)) |
| 462 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 463 | assertEquals(5, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 464 | ch.readFully(ByteBuffer.wrap(dst)) |
| 465 | assertTrue { dst.contentEquals(bytes) } |
| 466 | |
| 467 | ch.writeFully(ByteBuffer.wrap(bytes)) |
| 468 | ch.flush() |
| 469 | |
| 470 | val dst2 = ByteArray(4) |
| 471 | ch.readFully(ByteBuffer.wrap(dst2)) |
| 472 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 473 | assertEquals(1, ch.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 474 | assertEquals(5, ch.readByte()) |
| 475 | |
| 476 | ch.close() |
| 477 | |
| 478 | try { |
| 479 | ch.readFully(ByteBuffer.wrap(dst)) |
| 480 | fail("") |
| 481 | } catch (expected: ClosedReceiveChannelException) { |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | @Test |
| 487 | fun testReadAndWritePartially() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 488 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 489 | val bytes = byteArrayOf(1, 2, 3, 4, 5) |
| 490 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 491 | assertEquals(5, ch.writeAvailable(bytes)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 492 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 493 | assertEquals(5, ch.readAvailable(ByteArray(100))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 494 | |
| 495 | repeat(Size / bytes.size) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 496 | assertNotEquals(0, ch.writeAvailable(bytes)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 497 | ch.flush() |
| 498 | } |
| 499 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 500 | ch.readAvailable(ByteArray(ch.availableForRead - 1)) |
| 501 | assertEquals(1, ch.readAvailable(ByteArray(100))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 502 | |
| 503 | ch.close() |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | @Test |
| 508 | fun testReadAndWritePartiallyByteBuffer() { |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 509 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 510 | val bytes = byteArrayOf(1, 2, 3, 4, 5) |
| 511 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 512 | assertEquals(5, ch.writeAvailable(ByteBuffer.wrap(bytes))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 513 | ch.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 514 | assertEquals(5, ch.readAvailable(ByteBuffer.allocate(100))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 515 | |
| 516 | repeat(Size / bytes.size) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 517 | assertNotEquals(0, ch.writeAvailable(ByteBuffer.wrap(bytes))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 518 | ch.flush() |
| 519 | } |
| 520 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 521 | ch.readAvailable(ByteArray(ch.availableForRead - 1)) |
| 522 | assertEquals(1, ch.readAvailable(ByteBuffer.allocate(100))) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 523 | |
| 524 | ch.close() |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | |
| 529 | @Test |
| 530 | fun testReadAndWriteBig() { |
| 531 | val count = 200 |
| 532 | val bytes = ByteArray(65536) |
| 533 | Random().nextBytes(bytes) |
| 534 | |
| 535 | launch(CommonPool + CoroutineName("writer")) { |
| 536 | for (i in 1..count) { |
| 537 | ch.writeFully(bytes) |
| 538 | ch.flush() |
| 539 | } |
| 540 | }.invokeOnCompletion { t -> |
| 541 | if (t != null) { |
| 542 | failures.addError(t) |
| 543 | } |
| 544 | } |
| 545 | |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 546 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 547 | val dst = ByteArray(bytes.size) |
| 548 | for (i in 1..count) { |
| 549 | ch.readFully(dst) |
| 550 | assertTrue { dst.contentEquals(bytes) } |
| 551 | dst.fill(0) |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | @Test |
| 557 | fun testReadAndWriteBigByteBuffer() { |
| 558 | val count = 200 |
| 559 | val bytes = ByteArray(65536) |
| 560 | Random().nextBytes(bytes) |
| 561 | |
| 562 | launch(CommonPool + CoroutineName("writer")) { |
| 563 | for (i in 1..count) { |
| 564 | ch.writeFully(ByteBuffer.wrap(bytes)) |
| 565 | ch.flush() |
| 566 | } |
| 567 | }.invokeOnCompletion { t -> |
| 568 | if (t != null) { |
| 569 | failures.addError(t) |
| 570 | } |
| 571 | } |
| 572 | |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 573 | runTest { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 574 | val dst = ByteArray(bytes.size) |
| 575 | for (i in 1..count) { |
| 576 | ch.readFully(ByteBuffer.wrap(dst)) |
| 577 | assertTrue { dst.contentEquals(bytes) } |
| 578 | dst.fill(0) |
| 579 | } |
| 580 | } |
| 581 | } |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 582 | |
| 583 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 584 | fun testPacket() = runTest { |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 585 | val packet = buildPacket { |
| 586 | writeInt(0xffee) |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 587 | writeStringUtf8("Hello") |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | ch.writeInt(packet.remaining) |
| 591 | ch.writePacket(packet) |
| 592 | |
| 593 | ch.flush() |
| 594 | |
| 595 | val size = ch.readInt() |
| 596 | val readed = ch.readPacket(size) |
| 597 | |
| 598 | assertEquals(0xffee, readed.readInt()) |
| 599 | assertEquals("Hello", readed.readUTF8Line()) |
| 600 | } |
| 601 | |
| 602 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 603 | fun testBigPacket() = runTest { |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 604 | launch(CommonPool + CoroutineName("writer")) { |
| 605 | val packet = buildPacket { |
| 606 | writeInt(0xffee) |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 607 | writeStringUtf8(".".repeat(8192)) |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | ch.writeInt(packet.remaining) |
| 611 | ch.writePacket(packet) |
| 612 | |
| 613 | ch.flush() |
| 614 | } |
| 615 | |
| 616 | val size = ch.readInt() |
| 617 | val readed = ch.readPacket(size) |
| 618 | |
| 619 | assertEquals(0xffee, readed.readInt()) |
| 620 | assertEquals(".".repeat(8192), readed.readUTF8Line()) |
| 621 | } |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 622 | |
| 623 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 624 | fun testWriteString() = runTest { |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 625 | ch.writeStringUtf8("abc") |
| 626 | ch.close() |
| 627 | |
| 628 | assertEquals("abc", ch.readASCIILine()) |
| 629 | } |
| 630 | |
| 631 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 632 | fun testWriteCharSequence() = runTest { |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 633 | ch.writeStringUtf8("abc" as CharSequence) |
| 634 | ch.close() |
| 635 | |
| 636 | assertEquals("abc", ch.readASCIILine()) |
| 637 | } |
| 638 | |
| 639 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 640 | fun testWriteCharBuffer() = runTest { |
Sergey Mashkov | fbcba26 | 2017-08-23 12:26:18 +0300 | [diff] [blame] | 641 | val cb = CharBuffer.allocate(6) |
| 642 | |
| 643 | for (i in 0 until cb.remaining()) { |
| 644 | cb.put(i, ' ') |
| 645 | } |
| 646 | |
| 647 | cb.position(2) |
| 648 | cb.put(2, 'a') |
| 649 | cb.put(3, 'b') |
| 650 | cb.put(4, 'c') |
| 651 | cb.limit(5) |
| 652 | |
| 653 | assertEquals("abc", cb.slice().toString()) |
| 654 | |
| 655 | ch.writeStringUtf8(cb) |
| 656 | ch.close() |
| 657 | |
| 658 | assertEquals("abc", ch.readASCIILine()) |
| 659 | } |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 660 | |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 661 | @Test |
Sergey Mashkov | e7b84bb | 2018-02-12 19:36:43 +0300 | [diff] [blame] | 662 | fun testReadAndWriteLarge() = runTest { |
| 663 | val count = 128L * 1024 * stressTestMultiplier // * 8192 -> 1G * M |
| 664 | val data = ByteBuffer.allocate(8192)!! |
| 665 | Random().nextBytes(data.array()) |
| 666 | |
| 667 | launch("writer") { |
| 668 | repeat(count.toInt()) { |
| 669 | data.clear() |
| 670 | ch.writeFully(data) |
| 671 | } |
| 672 | ch.close() |
| 673 | } |
| 674 | |
| 675 | launch("reader") { |
| 676 | val buffer = ByteBuffer.allocate(8192)!! |
| 677 | var read = 0L |
| 678 | val total = count * 8192 |
| 679 | |
| 680 | while (read < total) { |
| 681 | buffer.clear() |
| 682 | val rc = ch.readFully(buffer) |
| 683 | if (rc == -1) break |
| 684 | read += rc |
| 685 | } |
| 686 | |
| 687 | assertEquals(total, read) |
| 688 | |
| 689 | buffer.clear() |
| 690 | assertEquals(-1, ch.readAvailable(buffer)) |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | @Test |
| 695 | fun testReadAndWriteLargeViaLookAheadSession() = runTest { |
| 696 | val count = 128L * 1024 * stressTestMultiplier // * 8192 -> 1G * M |
| 697 | val data = ByteBuffer.allocate(8192)!! |
| 698 | Random().nextBytes(data.array()) |
| 699 | |
| 700 | launch("writer") { |
| 701 | repeat(count.toInt()) { |
| 702 | data.clear() |
| 703 | ch.writeFully(data) |
| 704 | } |
| 705 | ch.close() |
| 706 | } |
| 707 | |
| 708 | launch("reader") { |
| 709 | var read = 0L |
| 710 | val total = count * 8192 |
| 711 | |
| 712 | ch.lookAheadSuspend { |
| 713 | while (read < total) { |
| 714 | val bb = request(0, 1) |
| 715 | if (bb == null) { |
| 716 | if (!awaitAtLeast(1)) break |
| 717 | continue |
| 718 | } |
| 719 | val rc = bb.remaining() |
| 720 | bb.position(bb.limit()) |
| 721 | read += rc |
| 722 | consumed(rc) |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | assertEquals(total, read) |
| 727 | assertEquals(-1, ch.readAvailable(ByteBuffer.allocate(8192))) |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | @Test |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 732 | fun testCopyLarge() { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 733 | val count = 100 * 256 * stressTestMultiplier // * 8192 |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 734 | |
| 735 | launch { |
| 736 | val bb = ByteBuffer.allocate(8192) |
| 737 | for (i in 0 until bb.capacity()) { |
| 738 | bb.put((i and 0xff).toByte()) |
| 739 | } |
| 740 | |
| 741 | for (i in 1..count) { |
| 742 | bb.clear() |
| 743 | val split = i and 0x1fff |
| 744 | |
| 745 | bb.limit(split) |
| 746 | ch.writeFully(bb) |
| 747 | yield() |
| 748 | bb.limit(bb.capacity()) |
| 749 | ch.writeFully(bb) |
| 750 | } |
| 751 | |
| 752 | ch.close() |
| 753 | } |
| 754 | |
| 755 | val dest = ByteBufferChannel(true, pool) |
| 756 | |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 757 | val joinerJob = launch { |
Sergey Mashkov | b86a003 | 2017-11-08 16:39:03 +0300 | [diff] [blame] | 758 | ch.copyAndClose(dest) |
| 759 | } |
| 760 | |
| 761 | val reader = launch { |
| 762 | val bb = ByteBuffer.allocate(8192) |
| 763 | |
| 764 | for (i in 1..count) { |
| 765 | bb.clear() |
| 766 | dest.readFully(bb) |
| 767 | bb.flip() |
| 768 | |
| 769 | if (i and 0x1fff == 0) { |
| 770 | for (idx in 0 until bb.capacity()) { |
| 771 | assertEquals((idx and 0xff).toByte(), bb.get()) |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | yield() |
| 777 | assertTrue(dest.isClosedForRead) |
| 778 | } |
| 779 | |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 780 | runTest { |
Sergey Mashkov | b86a003 | 2017-11-08 16:39:03 +0300 | [diff] [blame] | 781 | reader.join() |
| 782 | joinerJob.join() |
| 783 | dest.close() |
| 784 | ch.close() |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | @Test |
| 789 | fun testJoinToLarge() { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 790 | val count = 100 * 256 * stressTestMultiplier // * 8192 |
Sergey Mashkov | b86a003 | 2017-11-08 16:39:03 +0300 | [diff] [blame] | 791 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 792 | val writerJob = launch { |
Sergey Mashkov | b86a003 | 2017-11-08 16:39:03 +0300 | [diff] [blame] | 793 | val bb = ByteBuffer.allocate(8192) |
| 794 | for (i in 0 until bb.capacity()) { |
| 795 | bb.put((i and 0xff).toByte()) |
| 796 | } |
| 797 | |
| 798 | for (i in 1..count) { |
| 799 | bb.clear() |
| 800 | val split = i and 0x1fff |
| 801 | |
| 802 | bb.limit(split) |
| 803 | ch.writeFully(bb) |
| 804 | yield() |
| 805 | bb.limit(bb.capacity()) |
| 806 | ch.writeFully(bb) |
| 807 | } |
| 808 | |
| 809 | ch.close() |
| 810 | } |
| 811 | |
| 812 | val dest = ByteBufferChannel(true, pool) |
| 813 | |
| 814 | val joinerJob = launch { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 815 | ch.joinTo(dest, true) |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | val reader = launch { |
| 819 | val bb = ByteBuffer.allocate(8192) |
| 820 | |
| 821 | for (i in 1..count) { |
| 822 | bb.clear() |
| 823 | dest.readFully(bb) |
| 824 | bb.flip() |
| 825 | |
| 826 | if (i and 0x1fff == 0) { |
| 827 | for (idx in 0 until bb.capacity()) { |
| 828 | assertEquals((idx and 0xff).toByte(), bb.get()) |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 833 | bb.clear() |
| 834 | assertEquals(-1, dest.readAvailable(bb)) |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 835 | assertTrue(dest.isClosedForRead) |
| 836 | } |
| 837 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 838 | val latch = CountDownLatch(1) |
| 839 | val r = AtomicInteger(3) |
| 840 | |
| 841 | val handler: CompletionHandler = { t -> |
| 842 | t?.let { failures.addError(it); latch.countDown() } |
| 843 | if (r.decrementAndGet() == 0) latch.countDown() |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 844 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 845 | |
Roman Elizarov | 1f0df4b | 2017-12-18 16:29:52 +0300 | [diff] [blame] | 846 | reader.invokeOnCompletion(onCancelling = true, handler = handler) |
| 847 | writerJob.invokeOnCompletion(onCancelling = true, handler = handler) |
| 848 | joinerJob.invokeOnCompletion(onCancelling = true, handler = handler) |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 849 | |
| 850 | latch.await() |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 851 | } |
| 852 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 853 | private suspend fun launch(name: String = "child", block: suspend () -> Unit): Job { |
Sergey Mashkov | e7b84bb | 2018-02-12 19:36:43 +0300 | [diff] [blame] | 854 | return launch(context = DefaultDispatcher + CoroutineName(name), parent = coroutineContext[Job]) { |
| 855 | block() |
| 856 | }.apply { |
Vsevolod Tolstopyatov | 87f2faa | 2018-04-30 22:53:02 +0300 | [diff] [blame] | 857 | invokeOnCompletion( onCancelling = true) { t -> |
Sergey Mashkov | e7b84bb | 2018-02-12 19:36:43 +0300 | [diff] [blame] | 858 | if (t != null) ch.cancel(t) |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 863 | private fun launch(block: suspend () -> Unit): Job { |
| 864 | return launch(DefaultDispatcher) { |
| 865 | try { |
| 866 | block() |
| 867 | } catch (t: Throwable) { |
| 868 | failures.addError(t) |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 874 | fun testStressReadWriteFully() = runTest { |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 875 | val size = 100 |
| 876 | val data = ByteArray(size) { it.toByte() } |
| 877 | val exec = newFixedThreadPoolContext(8, "testStressReadFully") |
| 878 | val buffers = object : DefaultPool<ByteArray>(10) { |
| 879 | override fun produceInstance(): ByteArray { |
| 880 | return ByteArray(size) |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | try { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 885 | (1..100_000 * stressTestMultiplier).map { |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 886 | async(exec) { |
| 887 | val channel = ByteBufferChannel(autoFlush = false, pool = pool) |
| 888 | val job = launch(exec) { |
| 889 | try { |
| 890 | channel.writeFully(data) |
| 891 | } finally { |
| 892 | channel.close() |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | yield() |
| 897 | val buffer = buffers.borrow() |
| 898 | channel.readFully(buffer) |
| 899 | buffers.recycle(buffer) |
| 900 | job.cancel() |
| 901 | } |
| 902 | }.forEach { |
| 903 | it.await() |
| 904 | } |
| 905 | } finally { |
| 906 | exec.close() |
| 907 | } |
| 908 | } |
| 909 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 910 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 911 | fun testJoinToSmokeTest() = runTest { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 912 | val other = ByteBufferChannel(autoFlush = false, pool = pool) |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 913 | launch(coroutineContext) { |
| 914 | ch.joinTo(other, false) |
| 915 | } |
| 916 | yield() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 917 | |
| 918 | ch.writeInt(0x11223344) |
| 919 | ch.flush() |
| 920 | assertEquals(0x11223344, other.readInt()) |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 921 | |
| 922 | ch.close() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 926 | fun testJoinToChainSmokeTest1() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 927 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 928 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 929 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 930 | |
| 931 | launch(coroutineContext) { |
| 932 | B.joinTo(C, closeOnEnd = true) |
| 933 | } |
| 934 | launch(coroutineContext) { |
| 935 | A.joinTo(B, closeOnEnd = true) |
| 936 | } |
| 937 | |
| 938 | yield() |
| 939 | A.writeStringUtf8("OK") |
| 940 | A.close() |
| 941 | |
| 942 | assertEquals("OK", C.readUTF8Line()) |
| 943 | } |
| 944 | |
| 945 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 946 | fun testJoinToChainSmokeTest2() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 947 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 948 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 949 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 950 | |
| 951 | launch(coroutineContext) { |
| 952 | A.joinTo(B, closeOnEnd = true) |
| 953 | } |
| 954 | launch(coroutineContext) { |
| 955 | B.joinTo(C, closeOnEnd = true) |
| 956 | } |
| 957 | |
| 958 | yield() |
| 959 | A.writeStringUtf8("OK") |
| 960 | A.close() |
| 961 | |
| 962 | assertEquals("OK", C.readUTF8Line()) |
| 963 | } |
| 964 | |
| 965 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 966 | fun testJoinToChainSmokeTest3() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 967 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 968 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 969 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 970 | |
| 971 | launch(coroutineContext + CoroutineName("A->B")) { |
| 972 | A.joinTo(B, closeOnEnd = true) |
| 973 | } |
| 974 | launch(coroutineContext + CoroutineName("B->C")) { |
| 975 | B.joinTo(C, closeOnEnd = true) |
| 976 | } |
| 977 | |
| 978 | A.writeStringUtf8("OK\n") |
| 979 | // A.close() |
| 980 | A.flush() |
| 981 | yield() |
| 982 | yield() |
| 983 | yield() |
| 984 | A.close() |
| 985 | |
| 986 | assertEquals("OK", C.readUTF8Line()) |
| 987 | } |
| 988 | |
| 989 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 990 | fun testJoinToChainSmokeTest4() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 991 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 992 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 993 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 994 | |
| 995 | launch(coroutineContext + CoroutineName("A->B")) { |
| 996 | A.joinTo(B, closeOnEnd = true) |
| 997 | } |
| 998 | launch(coroutineContext + CoroutineName("B->C")) { |
| 999 | B.joinTo(C, closeOnEnd = true) |
| 1000 | } |
| 1001 | |
| 1002 | A.writeStringUtf8("OK\n") |
| 1003 | A.close() |
| 1004 | |
| 1005 | assertEquals("OK", C.readUTF8Line()) |
| 1006 | } |
| 1007 | |
| 1008 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1009 | fun testJoinToFull() = runTest() { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1010 | val D = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1011 | |
| 1012 | var written = 0 |
| 1013 | D.writeByte(1) |
| 1014 | written++ |
| 1015 | while (D.availableForWrite > 0) { |
| 1016 | D.writeByte(1) |
| 1017 | written++ |
| 1018 | } |
| 1019 | |
| 1020 | ch.writeInt(777) |
| 1021 | ch.close() |
| 1022 | |
| 1023 | launch(coroutineContext) { |
| 1024 | ch.joinTo(D, true) |
| 1025 | } |
| 1026 | |
| 1027 | yield() |
| 1028 | |
| 1029 | repeat(written) { |
| 1030 | D.readByte() |
| 1031 | } |
| 1032 | |
| 1033 | assertEquals(777, D.readInt()) |
| 1034 | } |
| 1035 | |
| 1036 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1037 | fun testJoinToChainNonEmpty() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1038 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1039 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1040 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1041 | |
| 1042 | A.writeStringUtf8("1") |
| 1043 | A.flush() |
| 1044 | |
| 1045 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1046 | assertEquals("1OK", C.readUTF8Line()) |
| 1047 | } |
| 1048 | yield() |
| 1049 | |
| 1050 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1051 | A.joinTo(B, closeOnEnd = true) |
| 1052 | } |
| 1053 | |
| 1054 | yield() |
| 1055 | |
| 1056 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1057 | B.joinTo(C, closeOnEnd = true) |
| 1058 | } |
| 1059 | yield() |
| 1060 | |
| 1061 | |
| 1062 | yield() |
| 1063 | yield() |
| 1064 | yield() |
| 1065 | |
| 1066 | A.writeStringUtf8("OK\n") |
| 1067 | A.close() |
| 1068 | } |
| 1069 | |
| 1070 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1071 | fun testCopyToThenJoinTo() = runTest { |
| 1072 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1073 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1074 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1075 | |
| 1076 | // A.writeStringUtf8("1") |
| 1077 | // A.flush() |
| 1078 | |
| 1079 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1080 | A.copyAndClose(B) |
| 1081 | } |
| 1082 | |
| 1083 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1084 | assertEquals("1OK", C.readUTF8Line()) |
| 1085 | } |
| 1086 | |
| 1087 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1088 | B.joinTo(C, closeOnEnd = true) |
| 1089 | } |
| 1090 | |
| 1091 | A.writeStringUtf8("1OK\n") |
| 1092 | A.close() |
| 1093 | } |
| 1094 | |
| 1095 | @Test |
| 1096 | fun testCopyToThenJoinTo2() = runTest { |
| 1097 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1098 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1099 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1100 | |
| 1101 | A.writeStringUtf8("1") |
| 1102 | A.flush() |
| 1103 | |
| 1104 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1105 | A.copyAndClose(B) |
| 1106 | } |
| 1107 | |
| 1108 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1109 | assertEquals("1OK", C.readUTF8Line()) |
| 1110 | } |
| 1111 | |
| 1112 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1113 | B.joinTo(C, closeOnEnd = true) |
| 1114 | } |
| 1115 | |
| 1116 | A.writeStringUtf8("OK\n") |
| 1117 | A.close() |
| 1118 | } |
| 1119 | |
| 1120 | @Test |
| 1121 | fun testCopyToThenJoinTo3() = runTest { |
| 1122 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1123 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1124 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1125 | |
| 1126 | A.writeStringUtf8("1") |
| 1127 | A.flush() |
| 1128 | |
| 1129 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1130 | A.copyAndClose(B) |
| 1131 | } |
| 1132 | |
| 1133 | yield() |
| 1134 | |
| 1135 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1136 | assertEquals("1OK", C.readUTF8Line()) |
| 1137 | } |
| 1138 | |
| 1139 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1140 | B.joinTo(C, closeOnEnd = true) |
| 1141 | } |
| 1142 | |
| 1143 | A.writeStringUtf8("OK\n") |
| 1144 | A.close() |
| 1145 | } |
| 1146 | |
| 1147 | @Test |
| 1148 | fun testCopyToThenJoinTo4() = runTest { |
| 1149 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1150 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1151 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1152 | |
| 1153 | A.writeStringUtf8("1") |
| 1154 | A.flush() |
| 1155 | |
| 1156 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1157 | assertEquals("1OK", C.readUTF8Line()) |
| 1158 | } |
| 1159 | |
| 1160 | yield() |
| 1161 | |
| 1162 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1163 | A.copyAndClose(B) |
| 1164 | } |
| 1165 | |
| 1166 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1167 | B.joinTo(C, closeOnEnd = true) |
| 1168 | } |
| 1169 | |
| 1170 | A.writeStringUtf8("OK\n") |
| 1171 | A.close() |
| 1172 | } |
| 1173 | |
| 1174 | @Test |
| 1175 | fun testCopyToThenJoinTo5() = runTest { |
| 1176 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1177 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1178 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1179 | |
| 1180 | A.writeStringUtf8("1") |
| 1181 | A.flush() |
| 1182 | |
| 1183 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1184 | B.joinTo(C, closeOnEnd = true) |
| 1185 | } |
| 1186 | |
| 1187 | yield() |
| 1188 | |
| 1189 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1190 | assertEquals("1OK", C.readUTF8Line()) |
| 1191 | } |
| 1192 | yield() |
| 1193 | |
| 1194 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1195 | A.copyAndClose(B) |
| 1196 | } |
| 1197 | |
| 1198 | A.writeStringUtf8("OK\n") |
| 1199 | A.close() |
| 1200 | } |
| 1201 | |
| 1202 | @Test |
| 1203 | fun testCopyToThenJoinTo6() = runTest { |
| 1204 | val A = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1205 | val B = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1206 | val C = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1207 | |
| 1208 | A.writeStringUtf8("1") |
| 1209 | A.flush() |
| 1210 | |
| 1211 | launch(coroutineContext + CoroutineName("B->C")) { |
| 1212 | B.joinTo(C, closeOnEnd = true) |
| 1213 | } |
| 1214 | |
| 1215 | yield() |
| 1216 | launch(coroutineContext + CoroutineName("A->B")) { |
| 1217 | A.copyAndClose(B) |
| 1218 | } |
| 1219 | |
| 1220 | yield() |
| 1221 | |
| 1222 | launch(coroutineContext + CoroutineName("Reader")) { |
| 1223 | assertEquals("1OK", C.readUTF8Line()) |
| 1224 | } |
| 1225 | |
| 1226 | A.writeStringUtf8("OK\n") |
| 1227 | A.close() |
| 1228 | } |
| 1229 | |
| 1230 | @Test |
| 1231 | fun testJoinClosed() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1232 | ch.writeInt(777) |
| 1233 | ch.close() |
| 1234 | |
| 1235 | val bc = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1236 | ch.joinTo(bc, closeOnEnd = true) |
| 1237 | |
| 1238 | assertEquals(777, bc.readInt()) |
| 1239 | assertEquals(0, bc.readRemaining().remaining) |
| 1240 | } |
| 1241 | |
| 1242 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1243 | fun testJoinToResumeRead() = runTest { |
Sergey Mashkov | c9d73f5 | 2017-11-09 21:18:33 +0300 | [diff] [blame] | 1244 | val other = ByteBufferChannel(autoFlush = true, pool = pool) |
| 1245 | val result = async(coroutineContext) { |
| 1246 | other.readLong() |
| 1247 | } |
| 1248 | yield() |
| 1249 | |
| 1250 | launch(coroutineContext) { |
| 1251 | ch.joinTo(other, true) |
| 1252 | } |
| 1253 | yield() |
| 1254 | yield() |
| 1255 | |
| 1256 | ch.writeLong(0x1122334455667788L) |
| 1257 | yield() |
| 1258 | assertEquals(0x1122334455667788L, result.await()) |
| 1259 | |
| 1260 | ch.close() |
| 1261 | } |
| 1262 | |
| 1263 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1264 | fun testJoinToAfterWrite() = runTest { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1265 | val other = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1266 | |
| 1267 | ch.writeInt(0x12345678) |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 1268 | launch(coroutineContext) { |
| 1269 | ch.joinTo(other, false) |
| 1270 | } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1271 | |
| 1272 | ch.writeInt(0x11223344) |
| 1273 | ch.flush() |
| 1274 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1275 | yield() |
| 1276 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1277 | assertEquals(0x12345678, other.readInt()) |
| 1278 | assertEquals(0x11223344, other.readInt()) |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 1279 | ch.close() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1280 | } |
| 1281 | |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 1282 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1283 | fun testJoinToClosed() = runTest { |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 1284 | val other = ByteBufferChannel(autoFlush = false, pool = pool) |
| 1285 | |
| 1286 | ch.writeInt(0x11223344) |
| 1287 | ch.close() |
| 1288 | |
| 1289 | ch.joinTo(other, true) |
| 1290 | yield() |
| 1291 | |
| 1292 | assertEquals(0x11223344, other.readInt()) |
| 1293 | assertTrue { other.isClosedForRead } |
| 1294 | } |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1295 | |
| 1296 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1297 | fun testJoinToDifferentEndian() = runTest { |
Sergey Mashkov | 8af8937 | 2017-12-05 12:35:56 +0300 | [diff] [blame] | 1298 | val other = ByteBufferChannel(autoFlush = true, pool = pool) |
| 1299 | other.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 1300 | ch.writeByteOrder = ByteOrder.BIG_ENDIAN |
| 1301 | |
| 1302 | ch.writeInt(0x11223344) // BE |
| 1303 | |
| 1304 | launch(coroutineContext) { |
| 1305 | ch.joinTo(other, true) |
| 1306 | } |
| 1307 | |
| 1308 | yield() |
| 1309 | |
| 1310 | ch.writeInt(0x55667788) // BE |
| 1311 | ch.writeByteOrder = ByteOrder.LITTLE_ENDIAN |
| 1312 | ch.writeInt(0x0abbccdd) // LE |
| 1313 | ch.close() |
| 1314 | |
| 1315 | other.readByteOrder = ByteOrder.BIG_ENDIAN |
| 1316 | assertEquals(0x11223344, other.readInt()) // BE |
| 1317 | assertEquals(0x55667788, other.readInt()) // BE |
| 1318 | other.readByteOrder = ByteOrder.LITTLE_ENDIAN |
| 1319 | assertEquals(0x0abbccdd, other.readInt()) // LE |
| 1320 | } |
| 1321 | |
| 1322 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1323 | fun testReadThenRead() = runTest { |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1324 | val phase = AtomicInteger(0) |
| 1325 | |
| 1326 | val first = launch(coroutineContext) { |
| 1327 | try { |
| 1328 | ch.readInt() |
| 1329 | fail("EOF expected") |
| 1330 | } catch (expected: ClosedReceiveChannelException) { |
| 1331 | assertEquals(1, phase.get()) |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | yield() |
| 1336 | |
| 1337 | val second = launch(coroutineContext) { |
| 1338 | try { |
| 1339 | ch.readInt() |
| 1340 | fail("Should fail with ISE") |
| 1341 | } catch (expected: IllegalStateException) { |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | yield() |
| 1346 | phase.set(1) |
| 1347 | ch.close() |
| 1348 | |
| 1349 | yield() |
| 1350 | |
| 1351 | first.invokeOnCompletion { t -> |
| 1352 | t?.let { throw it } |
| 1353 | } |
| 1354 | second.invokeOnCompletion { t -> |
| 1355 | t?.let { throw it } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1360 | fun writeThenReadStress() = runTest { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1361 | ch.close() |
| 1362 | |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 1363 | for (i in 1..50_000 * stressTestMultiplier) { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1364 | val a = ByteBufferChannel(false, pool) |
| 1365 | |
| 1366 | val w = launch { |
| 1367 | a.writeLong(1) |
| 1368 | a.close() |
| 1369 | } |
| 1370 | val r = launch { |
| 1371 | a.readLong() |
| 1372 | } |
| 1373 | |
| 1374 | w.join() |
| 1375 | r.join() |
| 1376 | } |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1380 | fun joinToEmptyStress() = runTest { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 1381 | for (i in 1..50_000 * stressTestMultiplier) { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1382 | val a = ByteBufferChannel(false, pool) |
| 1383 | |
| 1384 | launch(coroutineContext) { |
| 1385 | a.joinTo(ch, true) |
| 1386 | } |
| 1387 | |
| 1388 | yield() |
| 1389 | |
| 1390 | a.close() |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1395 | fun testJoinToStress() = runTest { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 1396 | for (i in 1..10000 * stressTestMultiplier) { |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1397 | val child = ByteBufferChannel(false, pool) |
| 1398 | val writer = launch { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1399 | child.writeLong(999 + i.toLong()) |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1400 | child.close() |
| 1401 | } |
| 1402 | |
| 1403 | child.joinTo(ch, false) |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1404 | assertEquals(999 + i.toLong(), ch.readLong()) |
| 1405 | writer.join() |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1406 | } |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 1407 | |
| 1408 | assertEquals(0, ch.availableForRead) |
| 1409 | ch.close() |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 1410 | } |
| 1411 | |
Sergey Mashkov | 2fdb5aa | 2017-11-08 16:50:13 +0300 | [diff] [blame] | 1412 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1413 | fun testSequentialJoin() = runTest { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 1414 | val steps = 20_000 * stressTestMultiplier |
Sergey Mashkov | 2fdb5aa | 2017-11-08 16:50:13 +0300 | [diff] [blame] | 1415 | |
| 1416 | val pipeline = launch(coroutineContext) { |
| 1417 | for (i in 1..steps) { |
| 1418 | val child = ByteBufferChannel(false, pool) |
| 1419 | launch(coroutineContext) { |
| 1420 | child.writeInt(i) |
| 1421 | child.close() |
| 1422 | } |
| 1423 | child.joinTo(ch, false) |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | for (i in 1..steps) { |
| 1428 | assertEquals(i, ch.readInt()) |
| 1429 | } |
| 1430 | |
| 1431 | pipeline.join() |
| 1432 | pipeline.invokeOnCompletion { cause -> |
| 1433 | cause?.let { throw it } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1437 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1438 | fun testSequentialJoinYield() = runTest { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 1439 | val steps = 20_000 * stressTestMultiplier |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1440 | |
| 1441 | val pipeline = launch(coroutineContext) { |
| 1442 | for (i in 1..steps) { |
| 1443 | val child = ByteBufferChannel(false, pool) |
| 1444 | launch(coroutineContext) { |
| 1445 | child.writeInt(i) |
| 1446 | child.close() |
| 1447 | } |
| 1448 | yield() |
| 1449 | child.joinTo(ch, false) |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | for (i in 1..steps) { |
| 1454 | assertEquals(i, ch.readInt()) |
| 1455 | } |
| 1456 | |
| 1457 | pipeline.join() |
| 1458 | pipeline.invokeOnCompletion { cause -> |
| 1459 | cause?.let { throw it } |
| 1460 | } |
| 1461 | } |
| 1462 | |
Sergey Mashkov | de8e44f | 2017-12-05 15:41:23 +0300 | [diff] [blame] | 1463 | @Test |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1464 | fun testJoinToNoFlush() = runTest { |
| 1465 | val src = ByteChannel(false) |
| 1466 | launch(coroutineContext) { |
| 1467 | src.joinTo(ch, closeOnEnd = false, flushOnEnd = false) |
| 1468 | assertEquals(0, ch.availableForRead) |
| 1469 | ch.flush() |
| 1470 | assertEquals(4, ch.availableForRead) |
| 1471 | } |
| 1472 | yield() |
| 1473 | |
| 1474 | src.writeInt(777) |
| 1475 | src.close() |
| 1476 | } |
| 1477 | |
| 1478 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1479 | fun testReadBlock() = runTest { |
Sergey Mashkov | de8e44f | 2017-12-05 15:41:23 +0300 | [diff] [blame] | 1480 | var bytesRead = 0L |
| 1481 | |
| 1482 | val r: (ByteBuffer) -> Unit = { bb -> |
| 1483 | bytesRead += bb.remaining() |
| 1484 | bb.position(bb.limit()) |
| 1485 | } |
| 1486 | |
| 1487 | val j = launch(coroutineContext) { |
| 1488 | while (!ch.isClosedForRead) { |
| 1489 | ch.read(0, r) |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | yield() |
| 1494 | |
| 1495 | ch.writeStringUtf8("OK\n") |
| 1496 | ch.close() |
| 1497 | |
| 1498 | j.join() |
| 1499 | j.invokeOnCompletion { |
| 1500 | it?.let { throw it } |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1505 | fun testReadBlock2() = runTest { |
Sergey Mashkov | de8e44f | 2017-12-05 15:41:23 +0300 | [diff] [blame] | 1506 | var bytesRead = 0L |
| 1507 | |
| 1508 | val r: (ByteBuffer) -> Unit = { bb -> |
| 1509 | bytesRead += bb.remaining() |
| 1510 | bb.position(bb.limit()) |
| 1511 | } |
| 1512 | |
| 1513 | val j = launch(coroutineContext) { |
| 1514 | while (!ch.isClosedForRead) { |
| 1515 | ch.read(0, r) |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | ch.writeStringUtf8("OK\n") |
| 1520 | yield() |
| 1521 | ch.close() |
| 1522 | |
| 1523 | j.join() |
| 1524 | j.invokeOnCompletion { |
| 1525 | it?.let { throw it } |
| 1526 | } |
| 1527 | } |
| 1528 | |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 1529 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1530 | fun testCancelWriter() = runTest { |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 1531 | val sub = writer(DefaultDispatcher) { |
| 1532 | delay(1000000L) |
| 1533 | } |
| 1534 | |
| 1535 | sub.channel.cancel() |
| 1536 | sub.join() |
| 1537 | } |
| 1538 | |
| 1539 | @Test |
Sergey Mashkov | 869bde5 | 2018-01-23 16:25:05 +0300 | [diff] [blame] | 1540 | fun testCancelReader() = runTest { |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 1541 | val sub = reader(DefaultDispatcher) { |
| 1542 | delay(10000000L) |
| 1543 | } |
| 1544 | |
| 1545 | sub.channel.close(CancellationException()) |
| 1546 | sub.join() |
| 1547 | } |
Sergey Mashkov | de8e44f | 2017-12-05 15:41:23 +0300 | [diff] [blame] | 1548 | |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1549 | @Test |
| 1550 | fun testWriteSuspendSessionSmokeTest() = runTest { |
| 1551 | ch.writeSuspendSession { |
| 1552 | val buffer = request(1) |
| 1553 | assertNotNull(buffer) |
| 1554 | } |
| 1555 | |
| 1556 | ch.writeSuspendSession { |
| 1557 | val buffer = request(1)!! |
| 1558 | buffer.put(0x11) |
| 1559 | written(1) |
| 1560 | } |
| 1561 | |
| 1562 | assertEquals(0, ch.availableForRead) |
| 1563 | ch.flush() |
| 1564 | assertEquals(1, ch.availableForRead) |
| 1565 | assertEquals(0x11, ch.readByte()) |
| 1566 | } |
| 1567 | |
| 1568 | @Test |
| 1569 | fun testWriteSuspendSessionJoined() = runTest { |
| 1570 | val next = ByteChannel() |
| 1571 | launch(Unconfined) { |
| 1572 | ch.joinTo(next, true) |
| 1573 | } |
| 1574 | |
| 1575 | yield() |
| 1576 | |
| 1577 | ch.writeSuspendSession { |
| 1578 | val buffer = request(1) |
| 1579 | assertNotNull(buffer) |
| 1580 | buffer!!.put(0x11) |
| 1581 | written(1) |
| 1582 | } |
| 1583 | |
| 1584 | assertEquals(0, next.availableForRead) |
| 1585 | ch.flush() |
| 1586 | assertEquals(1, next.availableForRead) |
| 1587 | assertEquals(0x11, next.readByte()) |
| 1588 | } |
| 1589 | |
| 1590 | @Test |
| 1591 | fun testWriteSuspendSessionJoinDuringWrite() = runTest { |
| 1592 | val next = ByteChannel() |
| 1593 | |
| 1594 | ch.writeSuspendSession { |
| 1595 | var buffer = request(1) |
| 1596 | assertNotNull(buffer) |
| 1597 | buffer!!.put(0x11) |
| 1598 | written(1) |
| 1599 | |
| 1600 | launch(Unconfined) { |
| 1601 | ch.joinTo(next, true) |
| 1602 | } |
| 1603 | |
| 1604 | yield() |
| 1605 | |
| 1606 | assertNull(request(1)) |
| 1607 | tryAwait(1) |
| 1608 | buffer = request(1) |
| 1609 | assertNotNull(buffer) |
| 1610 | buffer!!.put(0x22) |
| 1611 | written(1) |
| 1612 | } |
| 1613 | |
| 1614 | ch.flush() |
| 1615 | |
| 1616 | assertEquals(2, next.availableForRead) |
| 1617 | assertEquals(0x11, next.readByte()) |
| 1618 | assertEquals(0x22, next.readByte()) |
| 1619 | } |
| 1620 | |
Sergey Mashkov | 9faa039 | 2018-06-13 19:49:45 +0300 | [diff] [blame] | 1621 | @Test |
| 1622 | fun testJoiningDuringWriteFully() = runTest { |
| 1623 | val bb = ByteArray(65536) |
| 1624 | Random().nextBytes(bb) |
| 1625 | val dest = ByteChannel() |
| 1626 | |
| 1627 | launch(coroutineContext) { |
| 1628 | expect(1) |
| 1629 | ch.writeFully(bb) |
| 1630 | ch.flush() |
| 1631 | } |
| 1632 | yield() |
| 1633 | expect(2) |
| 1634 | |
| 1635 | launch { |
| 1636 | expect(3) |
| 1637 | ch.joinTo(dest, false) |
| 1638 | } |
| 1639 | yield() |
| 1640 | |
| 1641 | val result = ByteArray(bb.size) |
| 1642 | dest.readFully(result) |
| 1643 | |
| 1644 | Assert.assertArrayEquals(bb, result) |
| 1645 | finish(4) |
| 1646 | } |
| 1647 | |
Sergey Mashkov | 50a0857 | 2017-10-09 12:56:07 +0300 | [diff] [blame] | 1648 | private inline fun buildPacket(block: ByteWritePacket.() -> Unit): ByteReadPacket { |
| 1649 | val builder = BytePacketBuilder(0, pktPool) |
| 1650 | try { |
| 1651 | block(builder) |
| 1652 | return builder.build() |
| 1653 | } catch (t: Throwable) { |
| 1654 | builder.release() |
| 1655 | throw t |
| 1656 | } |
| 1657 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1658 | } |