-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathFreezingTest.kt
76 lines (70 loc) · 2.32 KB
/
FreezingTest.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlinx.coroutines.flow.*
import kotlin.native.concurrent.*
import kotlin.test.*
class FreezingTest : TestBase() {
@Test
fun testFreezeWithContextOther() = runTest {
// create a mutable object referenced by this lambda
val mutable = mutableListOf<Int>()
// run a child coroutine in another thread
val result = withContext(Dispatchers.Default) { "OK" }
assertEquals("OK", result)
// ensure that objects referenced by this lambda were not frozen
assertFalse(mutable.isFrozen)
mutable.add(42) // just to be 100% sure
}
@Test
fun testNoFreezeLaunchSame() = runTest {
// create a mutable object referenced by this lambda
val mutable1 = mutableListOf<Int>()
// this one will get captured into the other thread's lambda
val mutable2 = mutableListOf<Int>()
val job = launch { // launch into the same context --> should not freeze
assertEquals(mutable1.isFrozen, false)
assertEquals(mutable2.isFrozen, false)
val result = withContext(Dispatchers.Default) {
assertEquals(mutable2.isFrozen, true) // was frozen now
"OK"
}
assertEquals("OK", result)
assertEquals(mutable1.isFrozen, false)
}
job.join()
assertEquals(mutable1.isFrozen, false)
mutable1.add(42) // just to be 100% sure
}
@Test
fun testFrozenParentJob() {
val parent = Job()
parent.freeze()
val job = Job(parent)
assertTrue(job.isActive)
parent.cancel()
assertTrue(job.isCancelled)
}
// @Test
// fun testStateFlowValue() = runTest {
// val stateFlow = MutableStateFlow(0)
// stateFlow.freeze()
// stateFlow.value = 1
// }
//
// @Test
// @OptIn(ExperimentalStdlibApi::class)
// fun testStateFlowCollector() = runTest {
// val stateFlow = MutableStateFlow(0)
// stateFlow.freeze()
// repeat(10) {
// launch {
// stateFlow.collect {
// if (it == 42) cancel()
// }
// }
// }
// stateFlow.value = 42
// }
}