Skip to content

Commit

Permalink
Add RouterTest#testOrderListenerIsInvoked
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tsegismont committed Jan 9, 2025
1 parent ddad406 commit 1a25ecd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
18 changes: 18 additions & 0 deletions vertx-mutiny-clients/vertx-mutiny-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@
</optionMap>
</configuration>
</execution>
<!-- Run the annotation processor on java test sources -->
<execution>
<id>generate-tests</id>
<goals>
<goal>process-test</goal>
</goals>
<phase>generate-test-sources</phase>
<configuration>
<defaultOutputDirectory>${project.build.directory}/generated-test-sources/apt
</defaultOutputDirectory>
<processors>
<processor>io.vertx.codegen.CodeGenProcessor</processor>
</processors>
<optionMap>
<codegen.generators>mutiny</codegen.generators>
</optionMap>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});
});
Expand All @@ -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<Buffer> request3 = webClient.postAbs("http://localhost:8085/post");
WebClient webClient = WebClient.wrap(client);
HttpRequest<Buffer> request3 = webClient.postAbs(String.format("http://localhost:%d/post", server.actualPort()));
Uni<HttpResponse<Buffer>> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<RoutingContext> {
static TestRouteHandler create() {
return new TestRouteHandlerImpl();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ModuleGen(name = "webtest", groupPackage = "io.vertx")
package io.vertx.mutiny.web;

import io.vertx.codegen.annotations.ModuleGen;

0 comments on commit 1a25ecd

Please sign in to comment.