From f7b96d406528026734b6b7158f23bb1a06bb6d0d Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Thu, 9 Jan 2025 15:14:41 +0100 Subject: [PATCH] Add RouterTest#testOrderListenerIsInvoked This test verifies that, when a generated Mutiny handler is set, the bare Vert.x handler is provided to Vert.x, instead of a delegating handler applying redundant conversions (bare->Mutiny->bare). Signed-off-by: Thomas Segismont --- vertx-mutiny-clients/vertx-mutiny-web/pom.xml | 18 +++++++ .../java/io/vertx/mutiny/web/RouterTest.java | 47 +++++++++++++------ .../io/vertx/mutiny/web/TestRouteHandler.java | 29 ++++++++++++ .../mutiny/web/impl/TestRouteHandlerImpl.java | 26 ++++++++++ .../io/vertx/mutiny/web/package-info.java | 4 ++ 5 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/TestRouteHandler.java create mode 100644 vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/impl/TestRouteHandlerImpl.java create mode 100644 vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/package-info.java diff --git a/vertx-mutiny-clients/vertx-mutiny-web/pom.xml b/vertx-mutiny-clients/vertx-mutiny-web/pom.xml index fcf8bbb54f..e6e691ae4c 100644 --- a/vertx-mutiny-clients/vertx-mutiny-web/pom.xml +++ b/vertx-mutiny-clients/vertx-mutiny-web/pom.xml @@ -163,6 +163,24 @@ + + + generate-tests + + process-test + + generate-test-sources + + ${project.build.directory}/generated-test-sources/apt + + + io.vertx.codegen.CodeGenProcessor + + + mutiny + + + diff --git a/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/RouterTest.java b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/RouterTest.java index a2c3f0004a..917bdee785 100644 --- a/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/RouterTest.java +++ b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/RouterTest.java @@ -12,55 +12,65 @@ import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; +import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.RequestOptions; import io.vertx.mutiny.core.Vertx; import io.vertx.mutiny.core.buffer.Buffer; import io.vertx.mutiny.core.http.HttpClient; import io.vertx.mutiny.core.http.HttpClientRequest; +import io.vertx.mutiny.core.http.HttpServer; import io.vertx.mutiny.ext.web.Router; import io.vertx.mutiny.ext.web.client.HttpRequest; import io.vertx.mutiny.ext.web.client.HttpResponse; import io.vertx.mutiny.ext.web.client.WebClient; import io.vertx.mutiny.ext.web.handler.BodyHandler; import io.vertx.mutiny.ext.web.handler.StaticHandler; +import io.vertx.mutiny.mutiny.web.TestRouteHandler; public class RouterTest { private Vertx vertx; + private Router router; + private HttpServer server; + private HttpClient client; @Before public void setUp() { vertx = Vertx.vertx(); + router = Router.router(vertx); + server = vertx.createHttpServer() + .requestHandler(router) + .listenAndAwait(0, "localhost"); + client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(server.actualPort())); } @After public void tearDown() { + if (client != null) { + client.close().onFailure().recoverWithNull().await().indefinitely(); + } + if (server != null) { + server.close().onFailure().recoverWithNull().await().indefinitely(); + } vertx.closeAndAwait(); } @Test public void testRouter() throws InterruptedException { - Router router = Router.router(vertx); router.get("/").handler(rc -> { rc.response().endAndForget("hello"); }); router.get("/assets/*").handler(StaticHandler.create("src/test/resources/assets")); router.post().handler(BodyHandler.create()); - router.post("/post").handler(rc -> rc.response().endAndForget(rc.getBodyAsString())); - - vertx.createHttpServer() - .requestHandler(router::handle) - .listenAndAwait(8085); - - HttpClient client = vertx.createHttpClient(); + router.post("/post").handler(rc -> rc.response().endAndForget(rc.body().asString())); CountDownLatch latch1 = new CountDownLatch(1); RequestOptions req1 = new RequestOptions() - .setAbsoluteURI("http://localhost:8085"); + .setAbsoluteURI(String.format("http://localhost:%d", server.actualPort())); HttpClientRequest request1 = client.requestAndAwait(req1); request1.response().subscribe().with( resp -> resp.toMulti().subscribe().with(buffer -> { - assertEquals(buffer.toString(), "hello"); + assertEquals("hello", buffer.toString()); latch1.countDown(); })); request1 @@ -70,12 +80,12 @@ public void testRouter() throws InterruptedException { CountDownLatch latch2 = new CountDownLatch(1); RequestOptions req2 = new RequestOptions() - .setAbsoluteURI("http://localhost:8085/assets/test.txt"); + .setAbsoluteURI(String.format("http://localhost:%d/assets/test.txt", server.actualPort())); HttpClientRequest request2 = client.requestAndAwait(req2); request2.response().subscribe().with( resp -> { resp.toMulti().subscribe().with(buffer -> { - assertEquals(buffer.toString(), "This is a test."); + assertEquals("This is a test.", buffer.toString()); latch2.countDown(); }); }); @@ -85,17 +95,24 @@ public void testRouter() throws InterruptedException { assertTrue(latch2.await(1, TimeUnit.SECONDS)); CountDownLatch latch3 = new CountDownLatch(1); - WebClient webClient = WebClient.create(vertx); - HttpRequest request3 = webClient.postAbs("http://localhost:8085/post"); + WebClient webClient = WebClient.wrap(client); + HttpRequest request3 = webClient.postAbs(String.format("http://localhost:%d/post", server.actualPort())); Uni> uni = request3 .sendStream(Multi.createFrom().items("Hello", " ", "World", "!").map(Buffer::buffer)); uni.subscribe().with(r -> { - assertEquals(r.bodyAsString(), "Hello World!"); + assertEquals("Hello World!", r.bodyAsString()); latch3.countDown(); }); assertTrue(latch3.await(1, TimeUnit.SECONDS)); } + @Test + public void testOrderListenerIsInvoked() { + router.get().handler(TestRouteHandler.create()); + WebClient webClient = WebClient.wrap(client); + int statusCode = webClient.get("/").sendAndAwait().statusCode(); + assertEquals(200, statusCode); + } } diff --git a/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/TestRouteHandler.java b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/TestRouteHandler.java new file mode 100644 index 0000000000..786f23eb8b --- /dev/null +++ b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/TestRouteHandler.java @@ -0,0 +1,29 @@ +/* + * Copyright 2024 Red Hat, Inc. + * + * Red Hat licenses this file to you under the Apache License, version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package io.vertx.mutiny.web; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; +import io.vertx.mutiny.web.impl.TestRouteHandlerImpl; + +@VertxGen +public interface TestRouteHandler extends Handler { + static TestRouteHandler create() { + return new TestRouteHandlerImpl(); + } +} diff --git a/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/impl/TestRouteHandlerImpl.java b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/impl/TestRouteHandlerImpl.java new file mode 100644 index 0000000000..c56459fcdc --- /dev/null +++ b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/impl/TestRouteHandlerImpl.java @@ -0,0 +1,26 @@ +package io.vertx.mutiny.web.impl; + +import java.util.concurrent.atomic.AtomicBoolean; + +import io.vertx.ext.web.RoutingContext; +import io.vertx.ext.web.impl.OrderListener; +import io.vertx.mutiny.web.TestRouteHandler; + +public class TestRouteHandlerImpl implements TestRouteHandler, OrderListener { + + private final AtomicBoolean called = new AtomicBoolean(); + + @Override + public void handle(RoutingContext rc) { + if (called.get()) { + rc.response().end(); + } else { + rc.fail(500); + } + } + + @Override + public void onOrder(int order) { + called.set(true); + } +} diff --git a/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/package-info.java b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/package-info.java new file mode 100644 index 0000000000..7239be4ac1 --- /dev/null +++ b/vertx-mutiny-clients/vertx-mutiny-web/src/test/java/io/vertx/mutiny/web/package-info.java @@ -0,0 +1,4 @@ +@ModuleGen(name = "webtest", groupPackage = "io.vertx") +package io.vertx.mutiny.web; + +import io.vertx.codegen.annotations.ModuleGen;