From aac59a656bd32b501a5b944e0af528b85a0e9a64 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Mon, 2 Dec 2024 16:40:14 +1100 Subject: [PATCH] fix: With KTor mock server, block until the start coroutine is done --- .../au/com/dius/pact/consumer/KTorMockServer.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/KTorMockServer.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/KTorMockServer.kt index 1cb45e8a3..a116c4bf3 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/KTorMockServer.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/KTorMockServer.kt @@ -32,6 +32,7 @@ import io.ktor.server.response.respondBytes import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import java.net.URI import java.util.zip.DeflaterInputStream @@ -163,14 +164,14 @@ class KTorMockServer @JvmOverloads constructor( override fun start() { logger.debug { "Starting mock server" } - - CoroutineScope(server.application.coroutineContext).launch { - localAddress = server.engine.resolvedConnectors().first() + runBlocking { + val startJob = CoroutineScope(server.application.coroutineContext).launch { + localAddress = server.engine.resolvedConnectors().first() + } + server.start() + startJob.join() + logger.debug { "Mock server started: $localAddress" } } - - server.start() - waitForServer() - logger.debug { "Mock server started: $localAddress" } } override fun stop() {