forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoreleaseLeakTest.kt
40 lines (33 loc) · 1.13 KB
/
AutoreleaseLeakTest.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
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlin.native.ref.*
import kotlin.native.concurrent.*
import kotlin.native.internal.*
import platform.Foundation.*
import platform.darwin.NSObject
import kotlinx.cinterop.*
import kotlin.test.*
class AutoreleaseLeakTest : TestBase() {
private val testThread = currentThread()
@Test
fun testObjCAutorelease() {
val weakRef = AtomicReference<WeakReference<NSObject>?>(null)
runTest {
withContext(Dispatchers.Default) {
val job = launch {
assertNotEquals(testThread, currentThread())
val objcObj = NSObject()
weakRef.value = WeakReference(objcObj).freeze()
// Emulate an autorelease return value in native code.
objc_retainAutoreleaseReturnValue(objcObj.objcPtr())
}
job.join()
GC.collect()
}
assertNotNull(weakRef.value)
assertNull(weakRef.value?.value)
}
}
}