aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/channels/TickerChannelCommonTest.kt
blob: 5178907875e8ae8c31e0929d9e272896aeb59c00 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * 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 kotlinx.coroutines.selects.*
import org.junit.Test
import org.junit.runner.*
import org.junit.runners.*
import kotlin.test.*

@RunWith(Parameterized::class)
class TickerChannelCommonTest(private val channelFactory: Channel) : TestBase() {
    companion object {
        @Parameterized.Parameters(name = "{0}")
        @JvmStatic
        fun params(): Collection<Array<Any>> =
            Channel.values().map { arrayOf<Any>(it) }
    }

    enum class Channel {
        FIXED_PERIOD {
            override fun invoke(delay: Long, initialDelay: Long) =
                ticker(delay, initialDelayMillis = initialDelay, mode = TickerMode.FIXED_PERIOD)
        },

        FIXED_DELAY {
            override fun invoke(delay: Long, initialDelay: Long) =
                ticker(delay, initialDelayMillis = initialDelay, mode = TickerMode.FIXED_DELAY)
        };

        abstract operator fun invoke(delay: Long, initialDelay: Long = 0): ReceiveChannel<Unit>
    }

    @Test
    fun testDelay() = withVirtualTimeSource {
        runTest {
            val delayChannel = channelFactory(delay = 10000)
            delayChannel.checkNotEmpty()
            delayChannel.checkEmpty()

            delay(5000)
            delayChannel.checkEmpty()
            delay(5100)
            delayChannel.checkNotEmpty()

            delayChannel.cancel()
            delay(5100)
            assertFailsWith<CancellationException> { delayChannel.poll() }
        }
    }

    @Test
    fun testInitialDelay() = withVirtualTimeSource {
        runTest {
            val delayChannel = channelFactory(initialDelay = 750, delay = 1000)
            delayChannel.checkEmpty()
            delay(500)
            delayChannel.checkEmpty()
            delay(300)
            delayChannel.checkNotEmpty()

            // Regular delay
            delay(750)
            delayChannel.checkEmpty()
            delay(260)
            delayChannel.checkNotEmpty()
            delayChannel.cancel()
        }
    }

    @Test
    fun testReceive() = withVirtualTimeSource {
        runTest {
            val delayChannel = channelFactory(delay = 1000)
            delayChannel.checkNotEmpty()
            var value = withTimeoutOrNull(750) {
                delayChannel.receive()
                1
            }

            assertNull(value)
            value = withTimeoutOrNull(260) {
                delayChannel.receive()
                1
            }

            assertNotNull(value)
            delayChannel.cancel()
        }
    }

    @Test
    fun testComplexOperator() = withVirtualTimeSource {
        runTest {
            val producer = GlobalScope.produce {
                for (i in 1..7) {
                    send(i)
                    delay(1000)
                }
            }

            val averages = producer.averageInTimeWindow(3000).toList()
            assertEquals(listOf(2.0, 5.0, 7.0), averages)
        }
    }

    private fun ReceiveChannel<Int>.averageInTimeWindow(timespan: Long) = GlobalScope.produce {
        val delayChannel = channelFactory(delay = timespan, initialDelay = timespan)
        var sum = 0
        var n = 0
        whileSelect {
            this@averageInTimeWindow.onReceiveOrClosed {
                if (it.isClosed) {
                    // Send leftovers and bail out
                    if (n != 0) send(sum / n.toDouble())
                    false
                } else {
                    sum += it.value
                    ++n
                    true
                }
            }

            // Timeout, send aggregated average and reset counters
            delayChannel.onReceive {
                send(sum / n.toDouble())
                sum = 0
                n = 0
                true
            }
        }

        delayChannel.cancel()
    }

    @Test
    fun testStress() = runTest {
        // No OOM/SOE
        val iterations = 100_000 * stressTestMultiplier
        val delayChannel = channelFactory(0)
        repeat(iterations) {
            delayChannel.receive()
        }

        delayChannel.cancel()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testNegativeDelay() {
        channelFactory(-1)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testNegativeInitialDelay() {
        channelFactory(initialDelay = -1, delay = 100)
    }
}

fun ReceiveChannel<Unit>.checkEmpty() = assertNull(poll())

fun ReceiveChannel<Unit>.checkNotEmpty() {
    assertNotNull(poll())
    assertNull(poll())
}