[Gradle] Try to adjust port finding logic to fix tests flakiness
This commit is contained in:
committed by
Space Team
parent
bef34e9cbf
commit
5fd17fb233
+42
-27
@@ -15,6 +15,7 @@ import io.ktor.response.*
|
|||||||
import io.ktor.routing.*
|
import io.ktor.routing.*
|
||||||
import io.ktor.server.engine.*
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
|
import io.ktor.util.*
|
||||||
import io.ktor.util.collections.*
|
import io.ktor.util.collections.*
|
||||||
import org.gradle.util.GradleVersion
|
import org.gradle.util.GradleVersion
|
||||||
import org.jetbrains.kotlin.gradle.plugin.stat.*
|
import org.jetbrains.kotlin.gradle.plugin.stat.*
|
||||||
@@ -23,6 +24,7 @@ import org.jetbrains.kotlin.gradle.testbase.*
|
|||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.InetSocketAddress
|
||||||
import java.net.ServerSocket
|
import java.net.ServerSocket
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -35,37 +37,48 @@ import kotlin.test.*
|
|||||||
class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private val portSelectLock = java.util.concurrent.locks.ReentrantLock()
|
||||||
|
|
||||||
fun runWithKtorService(action: (Int) -> Unit) {
|
fun runWithKtorService(action: (Int) -> Unit) {
|
||||||
val port = getEmptyPort().localPort
|
var server: ApplicationEngine? = null
|
||||||
val server = embeddedServer(Netty, host = "localhost", port = port)
|
try {
|
||||||
{
|
portSelectLock.lock()
|
||||||
val requests = ArrayBlockingQueue<String>(10)
|
val port = getEmptyPort().localPort
|
||||||
|
server = embeddedServer(Netty, host = "localhost", port = port)
|
||||||
|
{
|
||||||
|
val requests = ArrayBlockingQueue<String>(10)
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/isReady") {
|
get("/isReady") {
|
||||||
call.respond(HttpStatusCode.OK)
|
call.respond(HttpStatusCode.OK)
|
||||||
}
|
}
|
||||||
post("/badRequest") {
|
post("/badRequest") {
|
||||||
call.respond(HttpStatusCode.BadRequest, "Some reason")
|
call.respond(HttpStatusCode.BadRequest, "Some reason")
|
||||||
}
|
}
|
||||||
post("/put") {
|
post("/put") {
|
||||||
val body = call.receive<String>()
|
val body = call.receive<String>()
|
||||||
requests.add(body)
|
requests.add(body)
|
||||||
call.respond(HttpStatusCode.OK)
|
call.respond(HttpStatusCode.OK)
|
||||||
}
|
}
|
||||||
get("/validate") {
|
get("/validate") {
|
||||||
try {
|
try {
|
||||||
call.respond(status = HttpStatusCode.OK, requests.poll(2, TimeUnit.SECONDS))
|
call.respond(status = HttpStatusCode.OK, requests.poll(2, TimeUnit.SECONDS))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
call.respond(status = HttpStatusCode.NotFound, e.message ?: e::class)
|
call.respond(status = HttpStatusCode.NotFound, e.message ?: e::class)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
awaitInitialization(port)
|
||||||
|
portSelectLock.unlock()
|
||||||
|
action(port)
|
||||||
|
} finally {
|
||||||
|
if (portSelectLock.isLocked) {
|
||||||
|
portSelectLock.unlock()
|
||||||
}
|
}
|
||||||
}.start()
|
server?.stop(1000, 1000)
|
||||||
awaitInitialization(port)
|
}
|
||||||
action(port)
|
|
||||||
server.stop(1000, 1000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEmptyPort(): ServerSocket {
|
private fun getEmptyPort(): ServerSocket {
|
||||||
@@ -73,7 +86,9 @@ class BuildStatisticsWithKtorIT : KGPBaseTest() {
|
|||||||
val endPort = 8180
|
val endPort = 8180
|
||||||
for (port in startPort..endPort) {
|
for (port in startPort..endPort) {
|
||||||
try {
|
try {
|
||||||
return ServerSocket(port).also {
|
return ServerSocket().apply {
|
||||||
|
bind(InetSocketAddress("localhost", port))
|
||||||
|
}.also {
|
||||||
println("Use $port port")
|
println("Use $port port")
|
||||||
it.close()
|
it.close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user