Skip to content

Commit

Permalink
fix: With KTor mock server, block until the start coroutine is done
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Dec 2, 2024
1 parent 5c7c78b commit aac59a6
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit aac59a6

Please sign in to comment.