blob: 6646aa61e49acc03a280ee0a2ae8007c704fc3d5 [file] [log] [blame]
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +03001package kotlinx.coroutines.experimental.channels
2
3import kotlinx.coroutines.experimental.*
4import kotlin.coroutines.experimental.*
5import kotlin.test.*
6
7class 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}