From 832ed16c873b4d672e4540faeccd550ef196b3be Mon Sep 17 00:00:00 2001 From: Inga Date: Wed, 27 Nov 2024 17:15:55 +0100 Subject: [PATCH] =?UTF-8?q?Setter=20ApplicationState=20initially=20til=20f?= =?UTF-8?q?alse,=20for=20=C3=A5=20ikke=20f=C3=A5=20false=20posi=E2=80=A6?= =?UTF-8?q?=20(#279)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Setter ApplicationState initially til false, for å ikke få false positives under oppstart * Setter ApplicationState til ready først ved ServerReady, ved ApplicationStarted er den ikke nødvendigvis klar for å ta imot connections enda. * Tester for readyness- og livenesstester --- src/main/kotlin/no/nav/pdfgen/Application.kt | 4 +- .../no/nav/pdfgen/plugins/LifecycleHooks.kt | 7 ++- .../kotlin/no/nav/pdfgen/ApplicationTest.kt | 56 +++++++++++++++---- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/no/nav/pdfgen/Application.kt b/src/main/kotlin/no/nav/pdfgen/Application.kt index 92de6ea..9c06436 100644 --- a/src/main/kotlin/no/nav/pdfgen/Application.kt +++ b/src/main/kotlin/no/nav/pdfgen/Application.kt @@ -57,6 +57,6 @@ fun Application.module() { } data class ApplicationState( - var alive: Boolean = true, - var ready: Boolean = true, + var alive: Boolean = false, + var ready: Boolean = false, ) diff --git a/src/main/kotlin/no/nav/pdfgen/plugins/LifecycleHooks.kt b/src/main/kotlin/no/nav/pdfgen/plugins/LifecycleHooks.kt index b957a6f..322183d 100644 --- a/src/main/kotlin/no/nav/pdfgen/plugins/LifecycleHooks.kt +++ b/src/main/kotlin/no/nav/pdfgen/plugins/LifecycleHooks.kt @@ -5,7 +5,12 @@ import no.nav.pdfgen.ApplicationState fun Application.configureLifecycleHooks(applicationState: ApplicationState) { - monitor.subscribe(ApplicationStarted) { applicationState.ready = true } + monitor.subscribe(ApplicationStarted) { + applicationState.alive = true + } + monitor.subscribe(ServerReady) { + applicationState.ready = true + } monitor.subscribe(ApplicationStopped) { applicationState.ready = false applicationState.alive = false diff --git a/src/test/kotlin/no/nav/pdfgen/ApplicationTest.kt b/src/test/kotlin/no/nav/pdfgen/ApplicationTest.kt index 3e5e8c7..e6c29df 100644 --- a/src/test/kotlin/no/nav/pdfgen/ApplicationTest.kt +++ b/src/test/kotlin/no/nav/pdfgen/ApplicationTest.kt @@ -2,35 +2,59 @@ package no.nav.pdfgen import io.ktor.client.request.* import io.ktor.client.statement.* +import io.ktor.events.* import io.ktor.http.* +import io.ktor.server.application.* import io.ktor.server.routing.* import io.ktor.server.testing.* import no.nav.pdfgen.application.api.nais.registerNaisApi +import no.nav.pdfgen.plugins.configureLifecycleHooks import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test internal class ApplicationTest { @Test - internal fun `Returns ok on is_alive`() { + internal fun `App is ready only after ServerReady is raised`() { testApplication { + var raiseApplicationStarted: () -> Any + var raiseServerReady: () -> Any application { routing { val applicationState = ApplicationState() - applicationState.ready = true - applicationState.alive = true registerNaisApi(applicationState) + configureLifecycleHooks(applicationState) } + raiseApplicationStarted = { monitor.raise(ApplicationStarted, this) } + raiseServerReady = { monitor.raise(ServerReady, this.environment) } } - val response = client.get("/internal/is_alive") + assertNotReady() + raiseApplicationStarted() + assertNotReady() + raiseServerReady() + assertReady() + } + } + + @Test + internal fun `App is alive after application is started`() { + testApplication { + application { + routing { + val applicationState = ApplicationState() + configureLifecycleHooks(applicationState) + registerNaisApi(applicationState) + } + } + val response = client.get("/internal/is_alive") assertEquals(HttpStatusCode.OK, response.status) assertEquals("I'm alive", response.bodyAsText()) } } @Test - internal fun `Returns ok in is_ready`() { + internal fun `Returns ok on is_alive`() { testApplication { application { routing { @@ -40,10 +64,10 @@ internal class ApplicationTest { registerNaisApi(applicationState) } } - val response = client.get("/internal/is_ready") + val response = client.get("/internal/is_alive") assertEquals(HttpStatusCode.OK, response.status) - assertEquals("I'm ready", response.bodyAsText()) + assertEquals("I'm alive", response.bodyAsText()) } } @@ -76,10 +100,20 @@ internal class ApplicationTest { registerNaisApi(applicationState) } } - val response = client.get("/internal/is_ready") - - assertEquals(HttpStatusCode.InternalServerError, response.status) - assertEquals("Please wait! I'm not ready :(", response.bodyAsText()) + assertNotReady() } + + } + + private suspend fun ApplicationTestBuilder.assertReady() { + val readyResponse = client.get("/internal/is_ready") + assertEquals(HttpStatusCode.OK, readyResponse.status) + assertEquals("I'm ready", readyResponse.bodyAsText()) + } + + private suspend fun ApplicationTestBuilder.assertNotReady() { + val response = client.get("/internal/is_ready") + assertEquals(HttpStatusCode.InternalServerError, response.status) + assertEquals("Please wait! I'm not ready :(", response.bodyAsText()) } }