From eefc9c56cf9109a12d13b425f520d2153202c801 Mon Sep 17 00:00:00 2001 From: sksamuel Date: Sat, 1 Oct 2022 08:56:32 -0500 Subject: [PATCH] Use supervisor scope for async execution #1 --- .../kotlin/com/sksamuel/aedile/core/caffeine.kt | 3 ++- .../kotlin/com/sksamuel/aedile/core/CacheTest.kt | 14 ++++++++++++++ .../com/sksamuel/aedile/core/LoadingCacheTest.kt | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/caffeine.kt b/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/caffeine.kt index 9f03045..ed04cdd 100644 --- a/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/caffeine.kt +++ b/aedile-core/src/main/kotlin/com/sksamuel/aedile/core/caffeine.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.future.asCompletableFuture import kotlinx.coroutines.launch @@ -76,7 +77,7 @@ fun caffeineBuilder(configure: Configuration.() -> Unit = {}): Buil c.configure() val caffeine = Caffeine.newBuilder() - val scope = c.scope ?: CoroutineScope(c.dispatcher + CoroutineName("Aedile-Caffeine-Scope")) + val scope = c.scope ?: CoroutineScope(c.dispatcher + CoroutineName("Aedile-Caffeine-Scope") + SupervisorJob()) c.evictionListener.let { listener -> caffeine.evictionListener { key, value, cause -> diff --git a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/CacheTest.kt b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/CacheTest.kt index 9dbd0c0..7083c77 100644 --- a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/CacheTest.kt +++ b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/CacheTest.kt @@ -1,5 +1,7 @@ package com.sksamuel.aedile.core +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.assertions.throwables.shouldThrowAny import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import kotlinx.coroutines.delay @@ -20,6 +22,18 @@ class CacheTest : FunSpec() { } shouldBe "bar" } + test("cache should handle exceptions in the compute function") { + val cache = caffeineBuilder().build() + shouldThrowAny { + cache.get("foo") { + error("kapow") + } + } + cache.get("bar") { + "baz" + } shouldBe "baz" + } + test("Cache should support getAll") { val cache = caffeineBuilder().build() cache.put("foo") { diff --git a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/LoadingCacheTest.kt b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/LoadingCacheTest.kt index e8d3ba6..ca1415d 100644 --- a/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/LoadingCacheTest.kt +++ b/aedile-core/src/test/kotlin/com/sksamuel/aedile/core/LoadingCacheTest.kt @@ -1,5 +1,6 @@ package com.sksamuel.aedile.core +import io.kotest.assertions.throwables.shouldThrowAny import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import kotlinx.coroutines.delay @@ -33,6 +34,21 @@ class LoadingCacheTest : FunSpec() { } shouldBe "wibble" } + test("LoadingCache should handle exceptions in the compute function") { + val cache = caffeineBuilder().build() { + delay(1) + "bar" + } + shouldThrowAny { + cache.get("foo") { + error("kapow") + } + } + cache.get("bar") { + "baz" + } shouldBe "baz" + } + test("LoadingCache should support suspendable put") { val cache = caffeineBuilder().build { delay(1)