Skip to content

Commit

Permalink
Improve http events test behaviour (Apicurio#3964)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Nov 12, 2023
1 parent c1574ff commit 838249e
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions app/src/test/java/io/apicurio/registry/events/HttpEventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> events;

@BeforeAll
public void setup() throws TimeoutException {
CompletableFuture<HttpServer> serverFuture = new CompletableFuture<>();
List<String> 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 -> {
Expand All @@ -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();
}
}
}

0 comments on commit 838249e

Please sign in to comment.