Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.channels |
| 2 | |
| 3 | import kotlinx.coroutines.experimental.* |
| 4 | import kotlin.coroutines.experimental.* |
| 5 | import kotlin.test.* |
| 6 | |
| 7 | class ProduceConsumeTest : TestBase() { |
| 8 | |
| 9 | @Test |
| 10 | fun testRendezvous() = runTest { |
| 11 | testProducer(1) |
| 12 | } |
| 13 | |
| 14 | @Test |
| 15 | fun testSmallBuffer() = runTest { |
| 16 | testProducer(1) |
| 17 | } |
| 18 | |
| 19 | @Test |
| 20 | fun testMediumBuffer() = runTest { |
| 21 | testProducer(10) |
| 22 | } |
| 23 | |
| 24 | @Test |
| 25 | fun testLargeMediumBuffer() = runTest { |
| 26 | testProducer(1000) |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | fun testUnlimited() = runTest { |
| 31 | testProducer(Channel.UNLIMITED) |
| 32 | } |
| 33 | |
| 34 | private suspend fun testProducer(producerCapacity: Int) { |
| 35 | testProducer(1, producerCapacity) |
| 36 | testProducer(10, producerCapacity) |
| 37 | testProducer(100, producerCapacity) |
| 38 | } |
| 39 | |
| 40 | private suspend fun testProducer(messages: Int, producerCapacity: Int) { |
| 41 | var sentAll = false |
| 42 | val producer = produce(coroutineContext, capacity = producerCapacity) { |
| 43 | for (i in 1..messages) { |
| 44 | send(i) |
| 45 | } |
| 46 | sentAll = true |
| 47 | } |
| 48 | var consumed = 0 |
| 49 | for (x in producer) { |
| 50 | consumed++ |
| 51 | } |
| 52 | assertTrue(sentAll) |
| 53 | assertEquals(messages, consumed) |
| 54 | } |
| 55 | } |