aboutsummaryrefslogtreecommitdiff
path: root/app/src/test/kotlin/com/pitchedapps/frost/utils/CoroutineTest.kt
blob: 2744d0d877cc8cd14d2b8ee7440ffdbc3f78a6cd (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.utils

import com.pitchedapps.frost.kotlin.Flyweight
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
 * Collection of tests around coroutines
 */
@UseExperimental(ExperimentalCoroutinesApi::class)
class CoroutineTest {

    /**
     * Hooks onto the refresh channel for one true -> false cycle.
     * Returns the list of event ids that were emitted
     */
    private suspend fun transition(channel: ReceiveChannel<Pair<Boolean, Int>>): List<Pair<Boolean, Int>> {
        var refreshed = false
        return listen(channel) { (refreshing, _) ->
            if (refreshed && !refreshing)
                return@listen true
            if (refreshing)
                refreshed = true
            return@listen false
        }
    }

    private suspend fun <T> listen(
        channel: ReceiveChannel<T>,
        shouldEnd: suspend (T) -> Boolean = { false }
    ): List<T> =
        withContext(Dispatchers.IO) {
            val data = mutableListOf<T>()
            channel.receiveAsFlow()
            for (c in channel) {
                data.add(c)
                if (shouldEnd(c)) break
            }
            channel.cancel()
            return@withContext data
        }

    /**
     * When refreshing, we have a temporary subscriber that hooks onto a single cycle.
     * The refresh channel only contains booleans, but for the sake of identification,
     * each boolean will have a unique integer attached.
     *
     * Things to note:
     * Subscription should be opened outside of async, since we don't want to miss any events.
     */
    @Test
    fun refreshSubscriptions() {
        val refreshChannel = BroadcastChannel<Pair<Boolean, Int>>(100)
        runBlocking {
            // Listen to all events
            val fullReceiver = refreshChannel.openSubscription()
            val fullDeferred = async { listen(fullReceiver) }

            refreshChannel.send(true to 1)
            refreshChannel.send(false to 2)
            refreshChannel.send(true to 3)

            val partialReceiver = refreshChannel.openSubscription()
            val partialDeferred = async { transition(partialReceiver) }
            refreshChannel.send(false to 4)
            refreshChannel.send(true to 5)
            refreshChannel.send(false to 6)
            refreshChannel.send(true to 7)
            refreshChannel.close()
            val fullStream = fullDeferred.await()
            val partialStream = partialDeferred.await()

            assertEquals(
                7,
                fullStream.size,
                "Full stream should contain all events"
            )
            assertEquals(
                listOf(false to 4, true to 5, false to 6),
                partialStream,
                "Partial stream should include up until first true false pair"
            )
        }
    }

    private fun <T : Any> SharedFlow<T?>.takeUntilNull(): Flow<T> =
        takeWhile { it != null }.filterNotNull()

    /**
     * Sanity check to ensure that contexts are being honoured
     */
    @Test
    fun contextSwitching() {
        val mainTag = "main-test"
        val mainDispatcher = Executors.newSingleThreadExecutor { r ->
            Thread(r, mainTag)
        }.asCoroutineDispatcher()

        val flow = MutableSharedFlow<String?>(100)
        runBlocking(Dispatchers.IO) {
            launch(mainDispatcher) {
                flow.takeUntilNull().collect { thread ->
                    assertTrue(
                        Thread.currentThread().name.startsWith(mainTag),
                        "Channel should be received in main thread"
                    )
                    assertFalse(
                        thread.startsWith(mainTag),
                        "Channel execution should not be in main thread"
                    )
                }
            }
            listOf(EmptyCoroutineContext, Dispatchers.IO, Dispatchers.Default, Dispatchers.IO).map {
                async(it) { flow.emit(Thread.currentThread().name) }
            }.joinAll()
            flow.emit(null)
            val count = flow.takeUntilNull().count()
            assertEquals(4, count, "Not all events received")
        }
    }

    /**
     * Not a true throttle, but for things like fetching header badges, we want to avoid simultaneous fetches.
     * As a result, I want to test that the usage of offer along with a conflated channel will work as I expect.
     * Events should be consumed when there is no pending consumer on previous elements.
     */
    @Test
    @Ignore("Move to flow")
    fun throttledChannel() {
        val channel = Channel<Int>(Channel.CONFLATED)
        runBlocking {
            val deferred = async {
                listen(channel) {
                    // Throttle consumer
                    delay(10)
                    return@listen false
                }
            }
            (0..100).forEach {
                channel.offer(it)
                delay(1)
            }
            channel.close()
            val received = deferred.await()
            assertTrue(
                received.size < 20,
                "Received data should be throttled; expected that around 1/10th of all events are consumed, but received ${received.size}"
            )
            println(received)
        }
    }

    @Test
    fun uniqueOnly() {
        val channel = BroadcastChannel<Int>(100)
        runBlocking {
            val fullReceiver = channel.openSubscription()
            val uniqueReceiver = channel.openSubscription().uniqueOnly(this)

            val fullDeferred = async { listen(fullReceiver) }
            val uniqueDeferred = async { listen(uniqueReceiver) }

            listOf(0, 1, 2, 3, 3, 3, 4, 3, 5, 5, 1).forEach {
                channel.offer(it)
            }
            channel.close()

            val fullData = fullDeferred.await()
            val uniqueData = uniqueDeferred.await()

            assertEquals(
                listOf(0, 1, 2, 3, 3, 3, 4, 3, 5, 5, 1),
                fullData,
                "Full receiver should get all channel events"
            )
            assertEquals(
                listOf(0, 1, 2, 3, 4, 3, 5, 1),
                uniqueData,
                "Unique receiver should not have two consecutive events that are equal"
            )
        }
    }

    /**
     * When using [uniqueOnly] for channels with limited capacity,
     * the duplicates should not count towards the actual capacity
     */
    @Ignore("Not yet working as unique only buffered removes the capacity limitation of the channel")
    @Test
    fun uniqueOnlyBuffer() {
        val channel = Channel<Int>(3)
        runBlocking {

            val deferred = async {
                listen(channel.uniqueOnly(GlobalScope)) {
                    // Throttle consumer
                    delay(50)
                    return@listen false
                }
            }

            listOf(0, 1, 1, 1, 1, 1, 2, 2, 2).forEach {
                delay(10)
                channel.offer(it)
            }

            channel.close()

            val data = deferred.await()

            assertEquals(
                listOf(0, 1, 2),
                data,
                "Unique receiver should not have two consecutive events that are equal"
            )
        }
    }

    class TestException(msg: String) : RuntimeException(msg)

    @Test
    fun exceptionChecks() {
        val mainTag = "main-test"
        val mainDispatcher = Executors.newSingleThreadExecutor { r ->
            Thread(r, mainTag)
        }.asCoroutineDispatcher()
        val channel = Channel<Int>()

        val job = SupervisorJob()

        val flyweight = Flyweight<Int, Int>(GlobalScope, 200L) {
            throw TestException("Flyweight exception")
        }

        suspend fun crash(): Boolean = withContext(Dispatchers.IO) {
            try {
                withContext(Dispatchers.Default) {
                    flyweight.fetch(0).await()
                }
                true
            } catch (e: TestException) {
                false
            }
        }

        runBlocking(mainDispatcher + job) {
            launch {
                val i = channel.receive()
                println("Received $i")
            }
            launch {
                println("A")
                println(crash())
                println("B")
                channel.offer(1)
            }
        }
    }
}