From 8714830b7f3cae36f355bef483ec1293dde7f87f Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Mon, 14 Oct 2024 12:22:04 +0800 Subject: [PATCH 1/3] feat(SimpleCachePlugin.kt): add error handling for failed calls Add `on(CallFailed)` hook to handle and log failed calls, improving error management within the SimpleCachePlugin. --- .../kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt b/ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt index af41ea7..c73c929 100755 --- a/ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt +++ b/ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt @@ -2,6 +2,7 @@ package com.ucasoft.ktor.simpleCache import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.application.hooks.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.util.* @@ -24,6 +25,10 @@ val SimpleCachePlugin = createRouteScopedPlugin(name = "SimpleCachePlugin", ::Si it.respond(cache) } } + on(CallFailed) { _, e -> + provider.badResponse() + throw e + } onCallRespond { call, body -> if ((call.response.status() ?: HttpStatusCode.OK) >= HttpStatusCode.BadRequest) { provider.badResponse() From a78c8bda7064aaf62495a679192eb9f90d93bc48 Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Mon, 14 Oct 2024 20:47:47 +0800 Subject: [PATCH 2/3] chore: bump version to 0.4.4 --- build.gradle.kts | 2 +- ktor-simple-cache/README.md | 4 ++-- ktor-simple-memory-cache/README.md | 4 ++-- ktor-simple-redis-cache/README.md | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 915325e..f28814b 100755 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ allprojects { group = "com.ucasoft.ktor" - version = "0.4.3" + version = "0.4.4" repositories { mavenCentral() diff --git a/ktor-simple-cache/README.md b/ktor-simple-cache/README.md index 8fbf675..325cc06 100755 --- a/ktor-simple-cache/README.md +++ b/ktor-simple-cache/README.md @@ -1,7 +1,7 @@ # Ktor Simple Cache Base solution which provides the plugin implementation and abstract class for cache providers. -[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.4.3/jar) +[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-cache/0.4.4/jar) ## Setup ### Gradle ```kotlin @@ -9,7 +9,7 @@ repositories { mavenCentral() } -implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.3") +implementation("com.ucasoft.ktor:ktor-simple-cache:0.4.4") ``` ## Usage ```kotlin diff --git a/ktor-simple-memory-cache/README.md b/ktor-simple-memory-cache/README.md index 9a8abc3..78fec5d 100755 --- a/ktor-simple-memory-cache/README.md +++ b/ktor-simple-memory-cache/README.md @@ -1,7 +1,7 @@ # Ktor Simple Memory Cache Memory cache provider for Ktor Simple Cache plugin -[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.3/jar) +[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-memory-cache/0.4.4/jar) ## Setup ### Gradle ```kotlin @@ -9,7 +9,7 @@ repositories { mavenCentral() } -implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.3") +implementation("com.ucasoft.ktor:ktor-simple-memory-cache:0.4.4") ``` ## Usage ```kotlin diff --git a/ktor-simple-redis-cache/README.md b/ktor-simple-redis-cache/README.md index 0368958..f560942 100755 --- a/ktor-simple-redis-cache/README.md +++ b/ktor-simple-redis-cache/README.md @@ -1,7 +1,7 @@ # Ktor Simple Redis Cache Redis cache provider for Ktor Simple Cache plugin -[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.3/jar) +[![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.4?color=blue)](https://search.maven.org/artifact/com.ucasoft.ktor/ktor-simple-redis-cache/0.4.4/jar) ## Setup ### Gradle ```kotlin @@ -9,7 +9,7 @@ repositories { mavenCentral() } -implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.3") +implementation("com.ucasoft.ktor:ktor-simple-redis-cache:0.4.4") ``` ## Usage ```kotlin From f6f9bca14304810ce57eba90e0081af420f52f17 Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Mon, 14 Oct 2024 21:07:47 +0800 Subject: [PATCH 3/3] unit test --- .../ktor/simpleCache/SimpleCacheTests.kt | 23 +++++++++++++++++++ .../ktor/simpleCache/TestApplication.kt | 3 +++ 2 files changed, 26 insertions(+) diff --git a/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/SimpleCacheTests.kt b/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/SimpleCacheTests.kt index 5313c02..04ffaed 100755 --- a/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/SimpleCacheTests.kt +++ b/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/SimpleCacheTests.kt @@ -157,6 +157,29 @@ internal class SimpleCacheTests { } } + @Test + fun `check route thrown exceptions aren not locked`() { + with(buildTestEngine(buildProvider(), Application::testApplication)) { + + runBlocking { + val totalThreads = 100 + val deferred = (1..totalThreads).map { + async { + shouldThrow { + client.get("/exception") + } + } + } + + val result = deferred.awaitAll().map { it.message }.groupBy { it } + .map { it.key to it.value.size } + result.shouldBeSingleton { + it.second.shouldBe(totalThreads) + } + } + } + } + @Test fun `check all parameters`() { val firstCacheKey = "/check?param1=value1¶m2=value2" diff --git a/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/TestApplication.kt b/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/TestApplication.kt index b130191..b7bcf76 100755 --- a/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/TestApplication.kt +++ b/ktor-simple-cache/src/test/kotlin/com/ucasoft/ktor/simpleCache/TestApplication.kt @@ -30,6 +30,9 @@ fun Application.testApplication() { get("/bad") { call.respond(HttpStatusCode.BadRequest, "Bad request") } + get("/exception") { + throw RuntimeException("Just an exception") + } } } }