1
+ /*
2
+ * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines
6
+
7
+ import kotlin.test.*
8
+ import kotlin.native.concurrent.*
9
+
10
+ class FreezingTest : TestBase () {
11
+ @Test
12
+ fun testFreezeWithContextOther () = runTest {
13
+ // create a mutable object referenced by this lambda
14
+ val mutable = mutableListOf<Int >()
15
+ // run a child coroutine in another thread
16
+ val result = withContext(Dispatchers .Default ) { " OK" }
17
+ assertEquals(" OK" , result)
18
+ // ensure that objects referenced by this lambda were not frozen
19
+ assertFalse(mutable.isFrozen)
20
+ mutable.add(42 ) // just to be 100% sure
21
+ }
22
+
23
+ @Test
24
+ fun testNoFreezeLaunchSame () = runTest {
25
+ // create a mutable object referenced by this lambda
26
+ val mutable1 = mutableListOf<Int >()
27
+ // this one will get captured into the other thread's lambda
28
+ val mutable2 = mutableListOf<Int >()
29
+ val job = launch { // launch into the same context --> should not freeze
30
+ assertEquals(mutable1.isFrozen, false )
31
+ assertEquals(mutable2.isFrozen, false )
32
+ val result = withContext(Dispatchers .Default ) {
33
+ assertEquals(mutable2.isFrozen, true ) // was frozen now
34
+ " OK"
35
+ }
36
+ assertEquals(" OK" , result)
37
+ assertEquals(mutable1.isFrozen, false )
38
+ }
39
+ job.join()
40
+ assertEquals(mutable1.isFrozen, false )
41
+ mutable1.add(42 ) // just to be 100% sure
42
+ }
43
+ }
0 commit comments