[K/N] Add more tests on associated object handling ^KT-56233
This commit is contained in:
committed by
Space Team
parent
dbe14a0a90
commit
87da670319
@@ -10,3 +10,10 @@ open class DeallocRetainBase
|
||||
fun garbageCollect() = kotlin.native.internal.GC.collect()
|
||||
|
||||
fun createWeakReference(value: Any) = kotlin.native.ref.WeakReference(value)
|
||||
|
||||
fun assertNull(value: Any?) {
|
||||
kotlin.test.assertNull(value)
|
||||
}
|
||||
|
||||
@OptIn(kotlin.ExperimentalStdlibApi::class)
|
||||
fun isExperimentalMM() = kotlin.native.isExperimentalMM()
|
||||
|
||||
@@ -62,12 +62,48 @@ private class DeallocRetain : DeallocRetainBase {
|
||||
}
|
||||
}
|
||||
|
||||
private class DeallocRetainAndAccess : DeallocRetainBase {
|
||||
static var deallocated = false
|
||||
static var retainObject: DeallocRetainAndAccess? = nil
|
||||
|
||||
deinit {
|
||||
DeallocRetainAndAccess.retainObject = self
|
||||
DeallocRetainKt.assertNull(value: DeallocRetainAndAccess.retainObject)
|
||||
DeallocRetainAndAccess.retainObject = nil
|
||||
|
||||
try! assertFalse(DeallocRetainAndAccess.deallocated)
|
||||
DeallocRetainAndAccess.deallocated = true
|
||||
}
|
||||
}
|
||||
|
||||
private func test2() throws {
|
||||
// Attempt to make the state predictable:
|
||||
DeallocRetainKt.garbageCollect()
|
||||
|
||||
DeallocRetainAndAccess.deallocated = false
|
||||
|
||||
autoreleasepool {
|
||||
DeallocRetainAndAccess()
|
||||
}
|
||||
|
||||
// Runs DeallocRetainAndAccess.deinit:
|
||||
DeallocRetainKt.garbageCollect()
|
||||
|
||||
try assertTrue(DeallocRetain.deallocated)
|
||||
|
||||
// Might crash due to double-dispose if the dealloc applied addRef/releaseRef to reclaimed Kotlin object:
|
||||
DeallocRetainKt.garbageCollect()
|
||||
}
|
||||
|
||||
class DeallocRetainTests : SimpleTestProvider {
|
||||
override init() {
|
||||
super.init()
|
||||
|
||||
#if !NOOP_GC
|
||||
test("Test1", test1)
|
||||
if (DeallocRetainKt.isExperimentalMM()) {
|
||||
test("Test2", test2)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,6 +481,8 @@ __attribute__((swift_name("DeallocRetainKt")))
|
||||
@interface KtDeallocRetainKt : KtBase
|
||||
+ (void)garbageCollect __attribute__((swift_name("garbageCollect()")));
|
||||
+ (KtKotlinWeakReference<id> *)createWeakReferenceValue:(id)value __attribute__((swift_name("createWeakReference(value:)")));
|
||||
+ (void)assertNullValue:(id _Nullable)value __attribute__((swift_name("assertNull(value:)")));
|
||||
+ (BOOL)isExperimentalMM __attribute__((swift_name("isExperimentalMM()")));
|
||||
@end
|
||||
|
||||
__attribute__((objc_subclassing_restricted))
|
||||
|
||||
@@ -481,6 +481,8 @@ __attribute__((swift_name("DeallocRetainKt")))
|
||||
@interface KtDeallocRetainKt : KtBase
|
||||
+ (void)garbageCollect __attribute__((swift_name("garbageCollect()")));
|
||||
+ (KtKotlinWeakReference<id> *)createWeakReferenceValue:(id)value __attribute__((swift_name("createWeakReference(value:)")));
|
||||
+ (void)assertNullValue:(id _Nullable)value __attribute__((swift_name("assertNull(value:)")));
|
||||
+ (BOOL)isExperimentalMM __attribute__((swift_name("isExperimentalMM()")));
|
||||
@end
|
||||
|
||||
__attribute__((objc_subclassing_restricted))
|
||||
|
||||
@@ -481,6 +481,8 @@ __attribute__((swift_name("DeallocRetainKt")))
|
||||
@interface KtDeallocRetainKt : KtBase
|
||||
+ (void)garbageCollect __attribute__((swift_name("garbageCollect()")));
|
||||
+ (KtKotlinWeakReference *)createWeakReferenceValue:(id)value __attribute__((swift_name("createWeakReference(value:)")));
|
||||
+ (void)assertNullValue:(id _Nullable)value __attribute__((swift_name("assertNull(value:)")));
|
||||
+ (BOOL)isExperimentalMM __attribute__((swift_name("isExperimentalMM()")));
|
||||
@end
|
||||
|
||||
__attribute__((objc_subclassing_restricted))
|
||||
|
||||
Reference in New Issue
Block a user