coordinator: try fix flacky ObservabilityServerTest (#148)

This commit is contained in:
Pedro Novais
2024-10-07 13:35:50 +01:00
committed by GitHub
parent 6ce1066343
commit 3686a726e0
2 changed files with 40 additions and 23 deletions

View File

@@ -19,7 +19,7 @@ class ObservabilityServer(private val config: Config) : AbstractVerticle() {
data class Config(
val applicationName: String,
val port: Int,
val port: Int = 0, // 0 means random port will be assigned by the underlying OS
val livenessPath: String = "/live",
val readinessPath: String = "/ready",
val metricsPath: String = "/metrics",
@@ -29,6 +29,9 @@ class ObservabilityServer(private val config: Config) : AbstractVerticle() {
val healthCheckHandler: HealthCheckHandler? = null
)
private var actualPort: Int? = null
val port: Int
get() = actualPort ?: throw IllegalStateException("Server not started")
private val log: Logger = LogManager.getLogger(this::class.java)
private val okReply = JsonObject().put("status", "OK").encode()
private var started = false
@@ -51,7 +54,8 @@ class ObservabilityServer(private val config: Config) : AbstractVerticle() {
res: AsyncResult<HttpServer> ->
started = res.succeeded()
if (started) {
log.info("Monitoring Server started and listening on port {}", res.result().actualPort())
actualPort = res.result().actualPort()
log.info("Monitoring Server started and listening on port {}", actualPort)
startPromise.complete()
} else {
startPromise.fail(res.cause())

View File

@@ -8,17 +8,15 @@ import io.restassured.specification.RequestSpecification
import io.vertx.core.Vertx
import io.vertx.junit5.VertxExtension
import net.consensys.linea.async.get
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.hamcrest.Matchers
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import kotlin.random.Random
@ExtendWith(VertxExtension::class)
class ObservabilityServerTest {
private var port: Int = 0
private lateinit var monitorRequestSpecification: RequestSpecification
private lateinit var observabilityServerDeploymentId: String
@@ -26,10 +24,9 @@ class ObservabilityServerTest {
fun beforeEach(vertx: Vertx) {
val (newDeploymentId, newPort) = runServerOnARandomPort(vertx)
observabilityServerDeploymentId = newDeploymentId
port = newPort
monitorRequestSpecification =
RequestSpecBuilder()
.setBaseUri("http://localhost:$port/")
.setBaseUri("http://localhost:$newPort/")
.build()
}
@@ -38,6 +35,32 @@ class ObservabilityServerTest {
vertx.undeploy(observabilityServerDeploymentId).get()
}
@Test
fun `when no port is defined it shall start server at random port`(vertx: Vertx) {
val observabilityServer = ObservabilityServer(
ObservabilityServer.Config(
applicationName = "test",
port = 0 // random port assigned by underlying OS
)
)
val deploymentId = vertx.deployVerticle(observabilityServer).get()
assertThat(observabilityServer.port).isGreaterThan(0)
RestAssured.given()
.spec(
RequestSpecBuilder()
.setBaseUri("http://localhost:${(observabilityServer.port)}/")
.build()
)
.When {
get("/live")
}
.then()
.statusCode(200)
vertx.undeploy(deploymentId).get()
}
@Test
fun exposesLiveEndpoint() {
RestAssured.given()
@@ -80,27 +103,17 @@ class ObservabilityServerTest {
.When {
get("/metrics")
}
.also { Assertions.assertThat(it.body.asString()).contains("vertx_http_server_response_bytes_bucket") }
.also { assertThat(it.body.asString()).contains("vertx_http_server_response_bytes_bucket") }
.then()
.statusCode(200)
.contentType(ContentType.TEXT)
}
private fun runServerOnARandomPort(vertx: Vertx): Pair<String, Int> {
val maxAttempts = 3
var attempts = 0
while (attempts < maxAttempts) {
val randomPort = Random.nextInt(1000, UShort.MAX_VALUE.toInt())
try {
val observabilityServer = ObservabilityServer(
ObservabilityServer.Config(applicationName = "test", port = randomPort)
)
val deploymentId = vertx.deployVerticle(observabilityServer).get()
return deploymentId to randomPort
} catch (_: Exception) {
attempts += 1
}
}
throw RuntimeException("Couldn't start observability server on a random port after $maxAttempts attempts!")
val observabilityServer = ObservabilityServer(
ObservabilityServer.Config(applicationName = "test")
)
val deploymentId = vertx.deployVerticle(observabilityServer).get()
return deploymentId to observabilityServer.port
}
}