|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.vertx; |
| 7 | + |
| 8 | +import io.opentelemetry.instrumentation.api.internal.HttpConstants; |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 10 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; |
| 14 | +import io.vertx.core.AbstractVerticle; |
| 15 | +import io.vertx.core.DeploymentOptions; |
| 16 | +import io.vertx.core.Vertx; |
| 17 | +import io.vertx.core.VertxOptions; |
| 18 | +import io.vertx.core.json.JsonObject; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 25 | + |
| 26 | +abstract class AbstractVertxHttpServerTest extends AbstractHttpServerTest<Vertx> { |
| 27 | + |
| 28 | + @RegisterExtension |
| 29 | + static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent(); |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void configure(HttpServerTestOptions options) { |
| 33 | + super.configure(options); |
| 34 | + options.setTestPathParam(true); |
| 35 | + // server spans are ended inside of the controller spans |
| 36 | + options.setVerifyServerSpanEndTime(false); |
| 37 | + options.setContextPath("/vertx-app"); |
| 38 | + options.setExpectedHttpRoute( |
| 39 | + (endpoint, method) -> { |
| 40 | + if (Objects.equals(method, HttpConstants._OTHER)) { |
| 41 | + return getContextPath() + endpoint.getPath(); |
| 42 | + } |
| 43 | + if (Objects.equals(endpoint, ServerEndpoint.NOT_FOUND)) { |
| 44 | + return getContextPath(); |
| 45 | + } |
| 46 | + return super.expectedHttpRoute(endpoint, method); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Vertx setupServer() |
| 52 | + throws ExecutionException, InterruptedException, TimeoutException, NoSuchMethodException { |
| 53 | + Vertx server = |
| 54 | + Vertx.vertx( |
| 55 | + new VertxOptions() |
| 56 | + // Useful for debugging: |
| 57 | + // .setBlockedThreadCheckInterval(Integer.MAX_VALUE) |
| 58 | + ); |
| 59 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
| 60 | + |
| 61 | + server.deployVerticle( |
| 62 | + verticle().getName(), |
| 63 | + new DeploymentOptions() |
| 64 | + .setConfig( |
| 65 | + new JsonObject().put(AbstractVertxWebServer.CONFIG_HTTP_SERVER_PORT, (Object) port)) |
| 66 | + .setInstances(3), |
| 67 | + res -> { |
| 68 | + if (!res.succeeded()) { |
| 69 | + throw new IllegalStateException("Cannot deploy server Verticle", res.cause()); |
| 70 | + } |
| 71 | + future.complete(null); |
| 72 | + }); |
| 73 | + |
| 74 | + future.get(30, TimeUnit.SECONDS); |
| 75 | + return server; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void stopServer(Vertx server) throws Exception { |
| 80 | + server.close(); |
| 81 | + } |
| 82 | + |
| 83 | + protected abstract Class<? extends AbstractVerticle> verticle(); |
| 84 | +} |
0 commit comments