[Gradle] Await for ktor initialization in BuildStatisticsWithKtorIT
This commit is contained in:
committed by
Space Team
parent
4f9adf894a
commit
ded0bb760f
+28
-1
@@ -47,6 +47,9 @@ class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
|||||||
val requests = ArrayBlockingQueue<String>(10)
|
val requests = ArrayBlockingQueue<String>(10)
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
|
get("/isReady") {
|
||||||
|
call.respond(HttpStatusCode.OK)
|
||||||
|
}
|
||||||
post("/badRequest") {
|
post("/badRequest") {
|
||||||
call.respond(HttpStatusCode.BadRequest, "Some reason")
|
call.respond(HttpStatusCode.BadRequest, "Some reason")
|
||||||
}
|
}
|
||||||
@@ -65,6 +68,7 @@ class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
|
awaitInitialization(port)
|
||||||
action(port)
|
action(port)
|
||||||
server.stop(1000, 1000)
|
server.stop(1000, 1000)
|
||||||
}
|
}
|
||||||
@@ -85,6 +89,29 @@ class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
|||||||
throw IOException("Failed to find free IP port in range $startPort..$endPort")
|
throw IOException("Failed to find free IP port in range $startPort..$endPort")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun awaitInitialization(port: Int, maxAttempts: Int = 20) {
|
||||||
|
var attempts = 0
|
||||||
|
val waitingTime = 500L
|
||||||
|
while (initCall(port) != HttpStatusCode.OK.value) {
|
||||||
|
attempts += 1
|
||||||
|
if (attempts == maxAttempts) {
|
||||||
|
fail("Failed to await server initialization for ${waitingTime * attempts}ms")
|
||||||
|
}
|
||||||
|
Thread.sleep(waitingTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initCall(port: Int): Int {
|
||||||
|
return try {
|
||||||
|
val connection = URL("http://localhost:$port/isReady").openConnection() as HttpURLConnection
|
||||||
|
connection.requestMethod = "GET"
|
||||||
|
connection.connect()
|
||||||
|
connection.responseCode
|
||||||
|
} catch (e: IOException) {
|
||||||
|
fail("Unable to open connection: ${e.message}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun validateCall(port: Int, validate: (JsonObject) -> Unit) {
|
private fun validateCall(port: Int, validate: (JsonObject) -> Unit) {
|
||||||
try {
|
try {
|
||||||
val connection = URL("http://localhost:$port/validate").openConnection() as HttpURLConnection
|
val connection = URL("http://localhost:$port/validate").openConnection() as HttpURLConnection
|
||||||
@@ -95,7 +122,7 @@ class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
|||||||
val jsonObject = JsonParser.parseString(body).asJsonObject
|
val jsonObject = JsonParser.parseString(body).asJsonObject
|
||||||
validate(jsonObject)
|
validate(jsonObject)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
assertFails { "Unable to open connection : ${e.message}" }
|
fail("Unable to open connection: ${e.message}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user