-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
SharingStartedWhileSubscribedTest.kt
77 lines (69 loc) · 2.82 KB
/
SharingStartedWhileSubscribedTest.kt
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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlin.test.*
import kotlin.time.*
class SharingStartedWhileSubscribedTest : TestBase() {
@Test // make sure equals works properly, or otherwise other tests don't make sense
fun testEqualsAndHashcode() {
val params = listOf(0L, 1L, 10L, 100L, 213L, Long.MAX_VALUE)
// HashMap will simultaneously test equals, hashcode and their consistency
val map = HashMap<SharingStarted, Pair<Long, Long>>()
for (i in params) {
for (j in params) {
map[SharingStarted.WhileSubscribed(i, j)] = i to j
}
}
for (i in params) {
for (j in params) {
assertEquals(i to j, map[SharingStarted.WhileSubscribed(i, j)])
}
}
}
@OptIn(ExperimentalTime::class)
@Test
fun testDurationParams() {
assertEquals(SharingStarted.WhileSubscribed(0), SharingStarted.WhileSubscribed(Duration.ZERO))
assertEquals(SharingStarted.WhileSubscribed(10), SharingStarted.WhileSubscribed(Duration.milliseconds(10)))
assertEquals(SharingStarted.WhileSubscribed(1000), SharingStarted.WhileSubscribed(1.seconds))
assertEquals(SharingStarted.WhileSubscribed(Long.MAX_VALUE), SharingStarted.WhileSubscribed(Duration.INFINITE))
assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 0), SharingStarted.WhileSubscribed(replayExpiration = Duration.ZERO))
assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 3), SharingStarted.WhileSubscribed(
replayExpiration = Duration.milliseconds(3)
))
assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 7000), SharingStarted.WhileSubscribed(replayExpiration = 7.seconds))
assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = Long.MAX_VALUE), SharingStarted.WhileSubscribed(replayExpiration = Duration.INFINITE))
}
@Test
fun testShouldRestart() = runTest {
var started = 0
val flow = flow {
expect(1 + ++started)
emit(1)
hang { }
}.shareIn(this, SharingStarted.WhileSubscribed(100 /* ms */))
expect(1)
flow.first()
delay(200)
flow.first()
finish(4)
coroutineContext.job.cancelChildren()
}
@Test
fun testImmediateUnsubscribe() = runTest {
val flow = flow {
expect(2)
emit(1)
hang { finish(4) }
}.shareIn(this, SharingStarted.WhileSubscribed(400, 0 /* ms */), 1)
expect(1)
repeat(5) {
flow.first()
delay(100)
}
expect(3)
coroutineContext.job.cancelChildren()
}
}