Sergey Mashkov | fc84a63 | 2017-08-23 12:11:55 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
Sergey Mashkov | 2c3a0dc | 2017-09-06 22:58:08 +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.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 java.io.* |
| 8 | import kotlin.coroutines.experimental.* |
| 9 | import kotlin.test.* |
Sergey Mashkov | fc84a63 | 2017-08-23 12:11:55 +0300 | [diff] [blame] | 10 | |
| 11 | class CopyAndCloseTest : TestBase() { |
| 12 | private val from = ByteChannel(true) |
| 13 | private val to = ByteChannel(true) |
| 14 | |
Sergey Mashkov | 2c3a0dc | 2017-09-06 22:58:08 +0300 | [diff] [blame] | 15 | @After |
| 16 | fun tearDown() { |
| 17 | from.close(CancellationException()) |
| 18 | to.close(CancellationException()) |
| 19 | } |
| 20 | |
Sergey Mashkov | fc84a63 | 2017-08-23 12:11:55 +0300 | [diff] [blame] | 21 | @Test |
| 22 | fun smokeTest() = runBlocking { |
| 23 | expect(1) |
| 24 | |
| 25 | launch(coroutineContext) { |
| 26 | expect(2) |
| 27 | val copied = from.copyAndClose(to) // should suspend |
| 28 | |
| 29 | expect(7) |
| 30 | |
| 31 | assertEquals(8, copied) |
| 32 | } |
| 33 | |
| 34 | yield() |
| 35 | |
| 36 | expect(3) |
| 37 | from.writeInt(1) |
| 38 | expect(4) |
| 39 | from.writeInt(2) |
| 40 | expect(5) |
| 41 | |
| 42 | yield() |
| 43 | expect(6) |
| 44 | |
| 45 | from.close() |
| 46 | yield() |
| 47 | |
| 48 | finish(8) |
| 49 | } |
| 50 | |
| 51 | @Test |
Sergey Mashkov | 72e9150 | 2017-09-03 00:17:14 +0300 | [diff] [blame] | 52 | fun copyLimitedTest() = runBlocking { |
| 53 | expect(1) |
| 54 | |
| 55 | launch(coroutineContext) { |
| 56 | expect(2) |
| 57 | val copied = from.copyTo(to, 3) // should suspend |
| 58 | |
| 59 | expect(5) |
| 60 | |
| 61 | assertEquals(3, copied) |
| 62 | } |
| 63 | |
| 64 | yield() |
| 65 | |
| 66 | expect(3) |
| 67 | from.writeByte(1) |
| 68 | yield() |
| 69 | |
| 70 | expect(4) |
| 71 | from.writeInt(2) |
| 72 | yield() |
| 73 | |
| 74 | finish(6) |
| 75 | } |
Sergey Mashkov | 2c3a0dc | 2017-09-06 22:58:08 +0300 | [diff] [blame] | 76 | |
| 77 | @Test |
| 78 | fun readRemaining() = runBlocking { |
| 79 | expect(1) |
| 80 | |
| 81 | launch(coroutineContext) { |
| 82 | expect(2) |
| 83 | from.writeFully("123".toByteArray()) |
| 84 | |
| 85 | yield() |
| 86 | expect(3) |
| 87 | from.writeFully("456".toByteArray().asByteBuffer()) |
| 88 | |
| 89 | yield() |
| 90 | expect(4) |
| 91 | from.close() |
| 92 | } |
| 93 | |
| 94 | yield() |
| 95 | assertEquals("123456", from.readRemaining().readText().toString()) |
| 96 | |
| 97 | yield() |
| 98 | |
| 99 | finish(5) |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | fun readRemainingLimitFailed() = runBlocking { |
| 104 | expect(1) |
| 105 | |
| 106 | launch(coroutineContext) { |
| 107 | expect(2) |
| 108 | from.writeFully("123".toByteArray()) |
| 109 | |
| 110 | yield() |
| 111 | expect(3) |
| 112 | from.writeFully("456".toByteArray().asByteBuffer()) |
| 113 | } |
| 114 | |
| 115 | yield() |
| 116 | assertEquals("12345", from.readRemaining(5).readText().toString()) |
| 117 | |
| 118 | finish(4) |
| 119 | } |
| 120 | |
| 121 | |
Sergey Mashkov | fc84a63 | 2017-08-23 12:11:55 +0300 | [diff] [blame] | 122 | } |