Files
kotlin-fork/kotlin-native/backend.native/tests/interop/objc/tests/objcWeakRefs.kt
T
Stanislav Erokhin f624800b84 Move everything under kotlin-native folder
I was forced to manually do update the following files, because otherwise
they would be ignored according .gitignore settings. Probably they
should be deleted from repo.

Interop/.idea/compiler.xml
Interop/.idea/gradle.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml
Interop/.idea/modules.xml
Interop/.idea/modules/Indexer/Indexer.iml
Interop/.idea/modules/Runtime/Runtime.iml
Interop/.idea/modules/StubGenerator/StubGenerator.iml
backend.native/backend.native.iml
backend.native/bc.frontend/bc.frontend.iml
backend.native/cli.bc/cli.bc.iml
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt
backend.native/tests/link/lib/foo.kt
backend.native/tests/link/lib/foo2.kt
backend.native/tests/teamcity-test.property
2020-10-27 21:00:28 +03:00

51 lines
1.8 KiB
Kotlin

import kotlinx.cinterop.*
import kotlin.native.concurrent.*
import kotlin.test.*
import objcTests.*
@Test fun testObjCWeakRef() {
val deallocListener = DeallocListener()
assertFalse(deallocListener.deallocated)
// [deallocListener.deallocExecutorIsNil()] calls deallocExecutor getter, which retains the result and either
// puts it to autoreleasepool or releases it immediately (Obj-C ARC optimization). It seems to depend on the platform.
// Wrap the call to autoreleasepool to ensure the object will be released:
autoreleasepool {
testObjCWeakRef0(deallocListener)
}
kotlin.native.internal.GC.collect()
assertTrue(deallocListener.deallocated)
assertTrue(deallocListener.deallocExecutorIsNil())
}
private fun testObjCWeakRef0(deallocListener: DeallocListener) = withWorker {
assertTrue(deallocListener.deallocExecutorIsNil())
val obj = object : DeallocExecutor() {}
deallocListener.deallocExecutor = obj
obj.deallocListener = deallocListener
assertFalse(deallocListener.deallocExecutorIsNil())
// TODO: can't actually test, Obj-C runtime doesn't expect _tryRetain throwing an exception.
// runInWorker {
// assertFailsWith<IncorrectDereferenceException> {
// deallocListener.deallocExecutorIsNil()
// }
// }
obj.freeze()
runInWorker {
// [deallocListener.deallocExecutorIsNil()] calls deallocExecutor getter, which retains [obj] and either
// puts it to autoreleasepool or releases it immediately (Obj-C ARC optimization).
// Wrap the call to autoreleasepool to ensure [obj] will be released:
autoreleasepool {
assertFalse(deallocListener.deallocExecutorIsNil())
}
// Process release of Kotlin reference to [obj] in any case:
kotlin.native.internal.GC.collect()
}
}