-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSubscribableValueTest.kt
63 lines (58 loc) · 1.83 KB
/
SubscribableValueTest.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
package kotlinx.coroutines.experimental.channels
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.TestBase
import kotlinx.coroutines.experimental.Unconfined
import kotlinx.coroutines.experimental.launch
import kotlin.coroutines.experimental.coroutineContext
import kotlin.test.Test
class SubscribableValueTest : TestBase() {
@Test
fun testBasicScenario() = runTest {
expect(1)
val subscribable = SubscribableValue(42)
val variable by subscribable
check(subscribable.value == 42)
check(variable == 42)
launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
expect(2)
val sub = subscribable.openSubscription()
check(sub.receive() == 42)
expect(3)
try {
sub.receive()
expectUnreached()
} catch (e: ClosedReceiveChannelException) {
// expected exception
}
expect(4)
}
finish(5)
}
@Test
fun testOpenSubscriptionAlwaysStartByTheCurrentValue() {
expect(1)
val subscribable = SubscribableValue(42)
launch(Unconfined) {
expect(2)
check(subscribable.openSubscription().first() == 42)
check(subscribable.openSubscription().first() == 42)
expect(3)
}
finish(4)
}
@Test
fun testObservableValue() {
expect(1)
val subscribable = SubscribableValue(42)
val value by subscribable
check(subscribable.value == 42)
check(value == 42)
launch(Unconfined) {
expect(2)
check(subscribable.openSubscription().first() == 42)
check(subscribable.openSubscription().first() == 42)
expect(3)
}
finish(4)
}
}