[K/N][Tests] Migrate objcSmoke & objcTests tests

^KT-61259
This commit is contained in:
Vladimir Sukharev
2024-01-24 18:04:17 +01:00
committed by Space Team
parent 58f7dd1c83
commit 524f0d335e
98 changed files with 227 additions and 8 deletions
@@ -0,0 +1,27 @@
@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import objcTests.*
@Test
fun testTryRetainGC() {
kotlin.native.runtime.GC.collect()
val weakRefHolder = WeakRefHolder()
createGarbageNSObjects(weakRefHolder)
weakRefHolder.obj = object : NSObject() {}
// Loading weak ref takes a lock. If K/N runtime runs GC while the lock is taken,
// then it releases garbage objects and thus Obj-C runtime might take a recursive lock
// and abort in _os_unfair_lock_recursive_abort.
weakRefHolder.loadManyTimes()
}
private fun createGarbageNSObjects(weakRefHolder: WeakRefHolder) {
autoreleasepool {
repeat(100) {
// Assigning the object to a weak reference so Obj-C would take a lock when deallocating it:
weakRefHolder.obj = NSObject()
}
weakRefHolder.obj = null
}
}