blob: 0c98528f8babc4549c534f86fe716fcdf3c346f7 [file] [log] [blame]
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03001/*
2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +03005package kotlinx.coroutines.experimental.channels
6
7import kotlinx.coroutines.experimental.*
8import kotlin.coroutines.experimental.*
9import kotlin.test.*
10
11class 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}