Skip to content

Commit

Permalink
Use supervisor scope for async execution #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Oct 1, 2022
1 parent 2015058 commit eefc9c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,7 +77,7 @@ fun <K, V> caffeineBuilder(configure: Configuration<K, V>.() -> 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<K, V> { key, value, cause ->
Expand Down
14 changes: 14 additions & 0 deletions aedile-core/src/test/kotlin/com/sksamuel/aedile/core/CacheTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +22,18 @@ class CacheTest : FunSpec() {
} shouldBe "bar"
}

test("cache should handle exceptions in the compute function") {
val cache = caffeineBuilder<String, String>().build()
shouldThrowAny {
cache.get("foo") {
error("kapow")
}
}
cache.get("bar") {
"baz"
} shouldBe "baz"
}

test("Cache should support getAll") {
val cache = caffeineBuilder<String, String>().build()
cache.put("foo") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,6 +34,21 @@ class LoadingCacheTest : FunSpec() {
} shouldBe "wibble"
}

test("LoadingCache should handle exceptions in the compute function") {
val cache = caffeineBuilder<String, String>().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<String, String>().build {
delay(1)
Expand Down

0 comments on commit eefc9c5

Please sign in to comment.