From 481db337df8d540ed5ecf39f67e4f934bad430ac Mon Sep 17 00:00:00 2001 From: sksamuel Date: Sun, 8 Oct 2023 06:59:25 -0500 Subject: [PATCH] Updated tests for loading cache and exceptions --- .../sksamuel/aedile/core/LoadingCacheTest.kt | 91 ++++++++++++++++++- 1 file changed, 86 insertions(+), 5 deletions(-) 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 90605d6..61d9716 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,7 @@ package com.sksamuel.aedile.core +import com.github.benmanes.caffeine.cache.Caffeine +import io.kotest.assertions.throwables.shouldThrow import io.kotest.assertions.throwables.shouldThrowAny import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe @@ -52,7 +54,88 @@ class LoadingCacheTest : FunSpec() { } shouldBe "wibble" } - test("LoadingCache should handle exceptions in the build compute function") { + test("foo") { + val cache = Caffeine.newBuilder().build { + error("kaboom") + } + cache.get("foo") { "baz" } shouldBe "baz" + cache.get("bar") { "baz" } shouldBe "baz" + } + + test("get should throw if build compute function throws and key is missing") { + val cache = caffeineBuilder().build { + error("kaput") + } + shouldThrow { + cache.get("foo") + } + } + + test("getIfPresent should return null if build compute function throws and key is missing") { + val cache = caffeineBuilder().build { + error("kaput") + } + cache.getIfPresent("foo") shouldBe null + } + + test("get should use existing value if build compute function throws and key is present") { + val cache = caffeineBuilder().build { + error("kaput") + } + cache.get("foo") { "bar" } shouldBe "bar" + cache.get("foo") shouldBe "bar" + } + + test("get should propagate exceptions if the override throws") { + val cache = caffeineBuilder().buildAll { keys -> + keys.associateWith { "$it-value" } + } + shouldThrow { + cache.get("foo") { error("kapow") } + } + cache.get("bar") { "baz" } shouldBe "baz" + } + + test("getAll should throw if build compute function throws and any key is missing") { + val cache = caffeineBuilder().build { + error("kaput") + } + shouldThrow { + cache.getAll(setOf("foo", "bar")) + } + delay(100) + cache.get("foo") { "baz" } shouldBe "baz" + delay(100) + shouldThrow { + cache.getAll(setOf("bar")) + } + delay(100) + cache.getAll(setOf("foo")) shouldBe mapOf("foo" to "baz") + } + + test("getAll should throw if build compute function override throws and any key is missing") { + val cache = caffeineBuilder().build { + "$it-value" + } + shouldThrow { + cache.getAll(setOf("foo", "bar")) { error("boom") } + } + cache.getAll(setOf("foo")) shouldBe mapOf("foo" to "foo-value") + shouldThrow { + cache.getAll(setOf("foo", "bar")) { error("boom") } + } + } + + test("getAll should return existing values if the build function throws but values are present") { + val cache = caffeineBuilder().build { + error("kaput") + } + cache.get("foo") { "baz" } shouldBe "baz" + cache.get("bar") { "faz" } shouldBe "faz" + cache.getAll(setOf("foo", "bar")) shouldBe mapOf("foo" to "baz", "bar" to "faz") + } + + test("LoadingCache should propagate exceptions in the build compute function override") { val cache = caffeineBuilder().build { delay(1) "bar" @@ -62,12 +145,10 @@ class LoadingCacheTest : FunSpec() { error("kapow") } } - cache.get("bar") { - "baz" - } shouldBe "baz" + cache.get("bar") { "baz" } shouldBe "baz" } - test("LoadingCache should handle exceptions in the per-key buildAll compute function") { + test("LoadingCache should propagate exceptions in the buildAll compute function override") { val cache = caffeineBuilder().buildAll { keys -> keys.associateWith { "$it-value" } }