Skip to content

Commit 1ff5c87

Browse files
authored
Fix invariants for expireAfter (#35)
1 parent b27b8c0 commit 1ff5c87

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

aedile-core/src/main/kotlin/com/sksamuel/aedile/core/builders.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import java.util.concurrent.Executor
2020
* If the suspendable computation throws or computes a null value then the
2121
* entry will be automatically removed.
2222
*/
23-
fun <K, V> Caffeine<Any, Any>.asCache(): Cache<K, V> {
23+
fun <K : Any?, V : Any?> Caffeine<in K, in V>.asCache(): Cache<K, V> {
2424
val scope = CoroutineScope(Dispatchers.IO + CoroutineName("Aedile-AsyncLoadingCache-Scope") + SupervisorJob())
2525
return asCache(scope)
2626
}
@@ -34,7 +34,7 @@ fun <K, V> Caffeine<Any, Any>.asCache(): Cache<K, V> {
3434
* If the suspendable computation throws or computes a null value then the
3535
* entry will be automatically removed.
3636
*/
37-
fun <K, V> Caffeine<Any, Any>.asCache(scope: CoroutineScope): Cache<K, V> {
37+
fun <K : Any?, V : Any?> Caffeine<in K, in V>.asCache(scope: CoroutineScope): Cache<K, V> {
3838
return Cache(scope, true, buildAsync())
3939
}
4040

aedile-core/src/test/kotlin/com/sksamuel/aedile/core/AsCacheTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.sksamuel.aedile.core
22

33
import com.github.benmanes.caffeine.cache.Caffeine
4+
import com.github.benmanes.caffeine.cache.Expiry
45
import io.kotest.assertions.throwables.shouldThrow
56
import io.kotest.core.spec.style.FunSpec
67
import io.kotest.matchers.shouldBe
78
import kotlinx.coroutines.delay
9+
import org.checkerframework.checker.index.qual.NonNegative
810

911
class AsCacheTest : FunSpec() {
1012
init {
@@ -110,5 +112,37 @@ class AsCacheTest : FunSpec() {
110112
cache.contains("wibble") shouldBe true
111113
cache.contains("bubble") shouldBe false
112114
}
115+
116+
test("check invariants on expire after") {
117+
val loggerExpiry = object : Expiry<Int, String> {
118+
override fun expireAfterRead(
119+
key: Int?,
120+
value: String?,
121+
currentTime: Long,
122+
currentDuration: @NonNegative Long
123+
): Long {
124+
return 0
125+
}
126+
127+
override fun expireAfterCreate(key: Int?, value: String?, currentTime: Long): Long {
128+
return 0
129+
}
130+
131+
override fun expireAfterUpdate(
132+
key: Int?,
133+
value: String?,
134+
currentTime: Long,
135+
currentDuration: @NonNegative Long
136+
): Long {
137+
return 0
138+
}
139+
}
140+
141+
Caffeine.newBuilder()
142+
.maximumSize(2000)
143+
.initialCapacity(500)
144+
.expireAfter(loggerExpiry)
145+
.asCache<Int, String>()
146+
}
113147
}
114148
}

0 commit comments

Comments
 (0)