-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying to figure out #1745
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import kotlin.test.* | ||
import kotlin.native.concurrent.* | ||
|
||
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 | ||
} | ||
} |