-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathCachingTest.kt
80 lines (65 loc) · 2.61 KB
/
CachingTest.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlinx.serialization.internal.*
import kotlinx.serialization.modules.*
import kotlin.reflect.*
import kotlin.test.*
import kotlin.test.Test
class CachingTest {
@Test
fun testCache() {
var factoryCalled = 0
val cache = createCache {
factoryCalled += 1
it.serializerOrNull()
}
repeat(10) {
cache.get(typeOf<String>().kclass())
}
assertEquals(1, factoryCalled)
}
@Test
fun testParameterizedCache() {
var factoryCalled = 0
val cache = createParametrizedCache { clazz, types ->
factoryCalled += 1
val serializers = EmptySerializersModule().serializersForParameters(types, true)!!
clazz.parametrizedSerializerOrNull(serializers) { types[0].classifier }
}
repeat(10) {
cache.get(typeOf<Map<*, *>>().kclass(), listOf(typeOf<String>(), typeOf<String>()))
}
assertEquals(1, factoryCalled)
}
@Serializable
class Target
@Test
fun testJvmIntrinsics() {
val ser1 = Target.serializer()
assertFalse(SERIALIZERS_CACHE.isStored(Target::class), "Cache shouldn't have values before call to serializer<T>()")
val ser2 = serializer<Target>()
assertFalse(
SERIALIZERS_CACHE.isStored(Target::class),
"Serializer for Target::class is stored in the cache, which means that runtime lookup was performed and call to serializer<Target> was not intrinsified." +
"Check that compiler plugin intrinsics are enabled and working correctly."
)
val ser3 = serializer(typeOf<Target>())
assertTrue(SERIALIZERS_CACHE.isStored(Target::class), "Serializer should be stored in cache after typeOf-based lookup")
}
@Serializable
class Target2
inline fun <reified T : Any> indirect(): KSerializer<T> = serializer<T>()
@Test
fun testJvmIntrinsicsIndirect() {
val ser1 = Target2.serializer()
assertFalse(SERIALIZERS_CACHE.isStored(Target2::class), "Cache shouldn't have values before call to serializer<T>()")
val ser2 = indirect<Target2>()
assertFalse(
SERIALIZERS_CACHE.isStored(Target2::class),
"Serializer for Target2::class is stored in the cache, which means that runtime lookup was performed and call to serializer<Target2> was not intrinsified." +
"Check that compiler plugin intrinsics are enabled and working correctly."
)
}
}