From 838249e88d9e636be3b208bd7b927f94964b98bf Mon Sep 17 00:00:00 2001 From: Carles Arnal Date: Sun, 12 Nov 2023 12:12:04 +0100 Subject: [PATCH] Improve http events test behaviour (#3964) --- .../registry/events/HttpEventsTest.java | 66 ++++++++++++------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/app/src/test/java/io/apicurio/registry/events/HttpEventsTest.java b/app/src/test/java/io/apicurio/registry/events/HttpEventsTest.java index d1d44c31d8..2c4d85948d 100644 --- a/app/src/test/java/io/apicurio/registry/events/HttpEventsTest.java +++ b/app/src/test/java/io/apicurio/registry/events/HttpEventsTest.java @@ -11,10 +11,14 @@ import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerOptions; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.InputStream; import java.time.Duration; @@ -32,17 +36,21 @@ @Tag(ApicurioTestTags.SLOW) public class HttpEventsTest extends AbstractResourceTestBase { - @Test - @Timeout(value = 65, unit = TimeUnit.SECONDS) - public void testHttpEvents() throws TimeoutException { + private final static Logger logger = LoggerFactory.getLogger(HttpEventsTest.class); + HttpServer server; + List events; + @BeforeAll + public void setup() throws TimeoutException { CompletableFuture serverFuture = new CompletableFuture<>(); - List events = new CopyOnWriteArrayList<>(); - - HttpServer server = Vertx.vertx().createHttpServer(new HttpServerOptions() + events = new CopyOnWriteArrayList<>(); + server = Vertx.vertx().createHttpServer(new HttpServerOptions() .setPort(8976)) .requestHandler(req -> { - events.add(req.headers().get("ce-type")); + if (RegistryEventType.ARTIFACT_CREATED.cloudEventType().equals(req.headers().get("ce-type")) + || RegistryEventType.ARTIFACT_UPDATED.cloudEventType().equals(req.headers().get("ce-type"))) { + events.add(req.headers().get("ce-type")); + } req.response().setStatusCode(200).end(); }) .listen(createdServer -> { @@ -54,30 +62,38 @@ public void testHttpEvents() throws TimeoutException { }); TestUtils.waitFor("proxy is ready", Duration.ofSeconds(1).toMillis(), Duration.ofSeconds(30).toMillis(), serverFuture::isDone); + } + + @Test + @Timeout(value = 65, unit = TimeUnit.SECONDS) + public void testHttpEvents() throws TimeoutException { + InputStream jsonSchema = getClass().getResourceAsStream("/io/apicurio/registry/util/json-schema.json"); + Assertions.assertNotNull(jsonSchema); + String content = IoUtil.toString(jsonSchema); + + String artifactId = TestUtils.generateArtifactId(); try { - InputStream jsonSchema = getClass().getResourceAsStream("/io/apicurio/registry/util/json-schema.json"); - Assertions.assertNotNull(jsonSchema); - String content = IoUtil.toString(jsonSchema); + createArtifact(artifactId, ArtifactType.JSON, content); + createArtifactVersion(artifactId, ArtifactType.JSON, content); + } catch (Exception ex) { + logger.error("Error in http events test", ex); + Assertions.fail(ex); + } + + TestUtils.waitFor("Events to be produced", 200, 60 * 100, () -> events.size() == 2); - String artifactId = TestUtils.generateArtifactId(); + assertLinesMatch( + Arrays.asList(RegistryEventType.ARTIFACT_CREATED.cloudEventType(), RegistryEventType.ARTIFACT_UPDATED.cloudEventType()), + events); - try { - createArtifact(artifactId, ArtifactType.JSON, content); - createArtifactVersion(artifactId, ArtifactType.JSON, content); - } catch (Exception e) { - Assertions.fail(e); - } + } - TestUtils.waitFor("Events to be produced", 200, 60 * 1000, () -> events.size() == 2); - assertLinesMatch( - Arrays.asList(RegistryEventType.ARTIFACT_CREATED.cloudEventType(), RegistryEventType.ARTIFACT_UPDATED.cloudEventType()), - events); - } finally { - if (server != null) { - server.close(); - } + @AfterAll + public void close() { + if (server != null) { + server.close(); } } } \ No newline at end of file