From 0e528df38c94ee487d9c96db39d7c224710c3414 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 23 Jul 2020 16:06:52 +0300 Subject: [PATCH] Unregister Vert.x codecs in dev-mode This is now needed because of Vert.x is no longer reloaded Fixes: #10928 --- .../ConsumeUuidEventHotReloadTest.java | 57 +++++++++++++++++++ .../vertx/devmode/UuidMessageConsumer.java | 16 ++++++ .../quarkus/vertx/runtime/VertxRecorder.java | 8 +++ 3 files changed, 81 insertions(+) create mode 100644 extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/ConsumeUuidEventHotReloadTest.java create mode 100644 extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/UuidMessageConsumer.java diff --git a/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/ConsumeUuidEventHotReloadTest.java b/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/ConsumeUuidEventHotReloadTest.java new file mode 100644 index 0000000000000..10f3d0378c4f4 --- /dev/null +++ b/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/ConsumeUuidEventHotReloadTest.java @@ -0,0 +1,57 @@ +package io.quarkus.vertx.devmode; + +import java.util.UUID; +import java.util.concurrent.CountDownLatch; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusDevModeTest; +import io.restassured.RestAssured; +import io.vertx.core.Vertx; +import io.vertx.ext.web.Router; + +public class ConsumeUuidEventHotReloadTest { + + @RegisterExtension + static final QuarkusDevModeTest test = new QuarkusDevModeTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClasses(UuidMessageConsumer.class, BeanDeployingAVerticleFromInstance.class)); + + private static final String SAMPLE_UUID = "38400000-8cf0-11bd-b23e-10b96e4ef00d"; + + @Test + public void testUuidMessageConsumption() { + String resp = RestAssured.get("/").asString(); + Assertions.assertEquals("test-" + SAMPLE_UUID, resp); + test.modifySourceFile(UuidMessageConsumer.class, data -> data.replace("test-", "other-")); + resp = RestAssured.get("/").asString(); + Assertions.assertEquals("other-" + SAMPLE_UUID, resp); + String resp2 = RestAssured.get("/").asString(); + Assertions.assertEquals(resp, resp2); + + } + + @ApplicationScoped + public static class BeanDeployingAVerticleFromInstance { + @Inject + Vertx vertx; + + public void init(@Observes Router router) throws InterruptedException { + CountDownLatch latch = new CountDownLatch(1); + vertx.deployVerticle(new MyVerticle(), + ar -> latch.countDown()); + router.get("/").handler(rc -> vertx.eventBus(). request("event", UUID.fromString(SAMPLE_UUID), + ar -> rc.response().end(ar.result().body()))); + latch.await(); + } + } + +} diff --git a/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/UuidMessageConsumer.java b/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/UuidMessageConsumer.java new file mode 100644 index 0000000000000..0731334050483 --- /dev/null +++ b/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/devmode/UuidMessageConsumer.java @@ -0,0 +1,16 @@ +package io.quarkus.vertx.devmode; + +import java.util.UUID; + +import javax.enterprise.context.ApplicationScoped; + +import io.quarkus.vertx.ConsumeEvent; + +@ApplicationScoped +public class UuidMessageConsumer { + + @ConsumeEvent(value = "event", blocking = true) + public String handleUuid(UUID uuid) { + return "test-" + uuid.toString(); + } +} diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java index 2aac64cb60ea8..466b423bcbf8e 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java @@ -14,6 +14,7 @@ import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.ShutdownContext; import io.quarkus.runtime.annotations.Recorder; +import io.quarkus.runtime.configuration.ProfileManager; import io.quarkus.vertx.ConsumeEvent; import io.vertx.core.AsyncResult; import io.vertx.core.Handler; @@ -144,12 +145,19 @@ private EventConsumerInvoker createInvoker(String invokerClassName) { @SuppressWarnings("unchecked") private void registerCodecs(Map, Class> codecByClass) { EventBus eventBus = vertx.eventBus(); + boolean isDevMode = ProfileManager.getLaunchMode() == LaunchMode.DEVELOPMENT; for (Map.Entry, Class> codecEntry : codecByClass.entrySet()) { Class target = codecEntry.getKey(); Class codec = codecEntry.getValue(); try { if (MessageCodec.class.isAssignableFrom(codec)) { MessageCodec messageCodec = (MessageCodec) codec.newInstance(); + if (isDevMode) { + // we need to unregister the codecs because in dev mode vert.x is not reloaded + // which means that if we don't unregister, we get an exception mentioning that the + // codec has already been registered + eventBus.unregisterDefaultCodec(target); + } eventBus.registerDefaultCodec(target, messageCodec); } else { LOGGER.error(String.format("The codec %s does not inherit from MessageCodec ", target.toString()));