Sergey Mashkov | 4ee9cfb | 2017-12-20 18:22:11 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
| 3 | import kotlinx.coroutines.experimental.* |
| 4 | import kotlinx.coroutines.experimental.channels.* |
| 5 | import kotlinx.coroutines.experimental.io.internal.* |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 6 | import org.junit.Ignore |
Sergey Mashkov | 4ee9cfb | 2017-12-20 18:22:11 +0300 | [diff] [blame] | 7 | import org.junit.Test |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import kotlin.coroutines.experimental.* |
Sergey Mashkov | 4ee9cfb | 2017-12-20 18:22:11 +0300 | [diff] [blame] | 9 | import kotlin.test.* |
| 10 | |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 11 | @Ignore |
Sergey Mashkov | 4ee9cfb | 2017-12-20 18:22:11 +0300 | [diff] [blame] | 12 | class InlineRendezvousSwapTest : TestBase() { |
| 13 | @Test |
| 14 | fun smokeTest1() = runTest { |
| 15 | val swap = InlineRendezvousSwap<String>() |
| 16 | |
| 17 | launch(coroutineContext) { |
| 18 | assertEquals("1", swap.receive()) |
| 19 | } |
| 20 | |
| 21 | launch(coroutineContext) { |
| 22 | swap.send("1") |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | @Test |
| 27 | fun smokeTest2() = runTest { |
| 28 | val swap = InlineRendezvousSwap<String>() |
| 29 | |
| 30 | launch(coroutineContext) { |
| 31 | swap.send("1") |
| 32 | } |
| 33 | |
| 34 | launch(coroutineContext) { |
| 35 | assertEquals("1", swap.receive()) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | fun testLoop1() = runTest { |
| 41 | val swap = InlineRendezvousSwap<String>() |
| 42 | val received = Channel<String>(1) |
| 43 | |
| 44 | launch(coroutineContext) { |
| 45 | while (true) { |
| 46 | val s = swap.receive() |
| 47 | if (s.isEmpty()) break |
| 48 | received.send(s) |
| 49 | } |
| 50 | received.close() |
| 51 | } |
| 52 | |
| 53 | launch(coroutineContext) { |
| 54 | for (i in 1..10) { |
| 55 | swap.send(i.toString()) |
| 56 | } |
| 57 | swap.send("") |
| 58 | } |
| 59 | |
| 60 | assertEquals((1..10).map { it.toString() }, received.toList()) |
| 61 | } |
| 62 | |
| 63 | @Test |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 64 | @Ignore |
Sergey Mashkov | 4ee9cfb | 2017-12-20 18:22:11 +0300 | [diff] [blame] | 65 | fun testLoop2() = runTest { |
| 66 | val swap = InlineRendezvousSwap<String>() |
| 67 | val received = Channel<String>(1) |
| 68 | |
| 69 | launch(coroutineContext) { |
| 70 | while (true) { |
| 71 | val s = swap.receive() |
| 72 | if (s.isEmpty()) break |
| 73 | received.send(s) |
| 74 | } |
| 75 | received.close() |
| 76 | } |
| 77 | |
| 78 | launch(coroutineContext) { |
| 79 | for (i in 1..10) { |
| 80 | yield() |
| 81 | swap.send(i.toString()) |
| 82 | } |
| 83 | swap.send("") |
| 84 | } |
| 85 | |
| 86 | assertEquals((1..10).map { it.toString() }, received.toList()) |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun testLoop3() = runTest { |
| 91 | val swap = InlineRendezvousSwap<String>() |
| 92 | val received = Channel<String>(1) |
| 93 | |
| 94 | launch(coroutineContext) { |
| 95 | while (true) { |
| 96 | yield() |
| 97 | val s = swap.receive() |
| 98 | if (s.isEmpty()) break |
| 99 | received.send(s) |
| 100 | } |
| 101 | received.close() |
| 102 | } |
| 103 | |
| 104 | launch(coroutineContext) { |
| 105 | for (i in 1..10) { |
| 106 | swap.send(i.toString()) |
| 107 | } |
| 108 | swap.send("") |
| 109 | } |
| 110 | |
| 111 | assertEquals((1..10).map { it.toString() }, received.toList()) |
| 112 | } |
| 113 | |
| 114 | } |