Skip to content

Commit

Permalink
Additional freezing tests
Browse files Browse the repository at this point in the history
Trying to figure out #1745
  • Loading branch information
elizarov committed Jan 16, 2020
1 parent afe941e commit 10979a3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions kotlinx-coroutines-core/native/test/FreezingTest.kt
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
}
}

0 comments on commit 10979a3

Please sign in to comment.