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