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