[reflect] Fix flaky tests that depend on Reflection.clearCaches()

* Use ReflectionFactoryImpl as single point of synchronization
* Synchronize all cache-sensitive tests on it in order to be robust in parallel test runners
* Remove redundant cache clear after each test

Merge-request: KT-MR-6842
Merged-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
This commit is contained in:
Vsevolod Tolstopyatov
2022-08-12 14:10:08 +00:00
committed by Space
parent 94abeb64c5
commit e32e5c26a4
8 changed files with 30 additions and 33 deletions
@@ -2,12 +2,15 @@
// FULL_JDK
// WITH_REFLECT
import kotlin.jvm.internal.*
import kotlin.reflect.jvm.internal.*
class A
fun box(): String {
val pckg = Reflection.getOrCreateKotlinPackage(A::class.java)
System.gc()
val pckg2 = Reflection.getOrCreateKotlinPackage(A::class.java)
if (pckg === pckg2) return "OK"
return "Fail"
return synchronized(ReflectionFactoryImpl::class.java) {
val pckg = Reflection.getOrCreateKotlinPackage(A::class.java)
System.gc()
val pckg2 = Reflection.getOrCreateKotlinPackage(A::class.java)
if (pckg === pckg2) return@synchronized "OK"
return@synchronized "Fail"
}
}
@@ -1,13 +1,16 @@
// TARGET_BACKEND: JVM_IR
// FULL_JDK
// WITH_REFLECT
import kotlin.reflect.jvm.internal.*
class A
fun box(): String {
val clz = A::class
System.gc()
val clz2 = A::class
if (clz === clz2) return "OK"
return "Fail"
return synchronized(ReflectionFactoryImpl::class.java) {
val clz = A::class
System.gc()
val clz2 = A::class
if (clz === clz2) return@synchronized "OK"
return@synchronized "Fail"
}
}
@@ -12,7 +12,9 @@ inline fun check(message: String, generate: () -> Any?) {
x1 = generate()
// Force clear the internal maps, as if the weak values in them are garbage-collected.
kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches()
synchronized(kotlin.reflect.jvm.internal.ReflectionFactoryImpl::class.java) {
kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches()
}
x2 = generate()
} catch (e: Throwable) {
@@ -11,7 +11,9 @@ inline fun check(message: String, generate: () -> Any?) {
x1 = generate()
// Force clear the internal maps, as if the weak values in them are garbage-collected.
kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches()
synchronized(kotlin.reflect.jvm.internal.ReflectionFactoryImpl::class.java) {
kotlin.reflect.jvm.internal.ReflectionFactoryImpl.clearCaches()
}
x2 = generate()
} catch (e: Throwable) {
+8 -5
View File
@@ -5,6 +5,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertSame
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.reflect.jvm.internal.*
private inline fun <reified T> check(isNullable: Boolean = false) {
val t1 = typeOf<T>()
@@ -15,10 +16,12 @@ private inline fun <reified T> check(isNullable: Boolean = false) {
}
fun box(): String {
check<Int>()
check<Int?>(true)
check<List<Int>?>(true)
check<List<Int>>()
check<List<Int?>?>(true)
synchronized(ReflectionFactoryImpl::class.java) {
check<Int>()
check<Int?>(true)
check<List<Int>?>(true)
check<List<Int>>()
check<List<Int?>?>(true)
}
return "OK"
}