You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2024/03/06 18:16:06 UTC

(camel) 03/08: CAMEL-20479: allow reusing VM-based broker instances

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c06efa3eee20797d1a384829440a52d36b990948
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Mar 6 10:42:46 2024 +0100

    CAMEL-20479: allow reusing VM-based broker instances
    
    - fixed tests that were incorrectly reusing broker instances
---
 ...tReplyTemporaryRefreshFailureOnStartupTest.java | 51 +++++++++++++++-------
 .../services/AbstractArtemisEmbeddedService.java   | 19 ++++++--
 .../infra/artemis/services/ArtemisVMService.java   | 11 +++++
 3 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java
index cd3c32347f3..6164e47b8f0 100644
--- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/temp/JmsRequestReplyTemporaryRefreshFailureOnStartupTest.java
@@ -26,33 +26,39 @@ import org.apache.camel.ExchangePattern;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.infra.artemis.services.ArtemisService;
-import org.apache.camel.test.infra.artemis.services.ArtemisServiceFactory;
+import org.apache.camel.test.infra.artemis.services.ArtemisVMService;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Tag;
 import org.junit.jupiter.api.Tags;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.TestMethodOrder;
 
 import static org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknowledge;
 
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 @Tags({ @Tag("not-parallel") })
-public class JmsRequestReplyTemporaryRefreshFailureOnStartupTest extends CamelTestSupport {
+public final class JmsRequestReplyTemporaryRefreshFailureOnStartupTest extends CamelTestSupport {
 
-    @RegisterExtension
-    public ArtemisService service = ArtemisServiceFactory.createVMService();
+    public static ArtemisService service = new ArtemisVMService.ReusableArtemisVMService();
 
-    private String brokerName;
     private final Long recoveryInterval = 1000L;
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
-        brokerName = "test-broker-" + System.currentTimeMillis();
+        service.initialize();
+
         CamelContext camelContext = super.createCamelContext();
 
         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(service.serviceAddress());
         camelContext.addComponent("jms", jmsComponentAutoAcknowledge(connectionFactory));
 
+        service.shutdown();
+
         return camelContext;
     }
 
@@ -73,26 +79,39 @@ public class JmsRequestReplyTemporaryRefreshFailureOnStartupTest extends CamelTe
         };
     }
 
+    @DisplayName("Test that throws and exception on connection failure")
     @Test
+    @Order(1)
     public void testTemporaryRefreshFailureOnStartup() throws Exception {
         //the first message will fail
         //the second message must be handled
         MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
-        mockEndpoint.expectedMessageCount(1);
+        mockEndpoint.expectedMessageCount(0);
 
         //the first request will return with an error
         //because the broker is not started yet
-        try {
-            template.requestBody("direct:start", "ping");
-        } catch (Exception exception) {
+        Assertions.assertThrows(Exception.class,
+                () -> template.requestBody("direct:start", "ping"));
+
+        mockEndpoint.assertIsSatisfied();
+    }
 
-        }
+    @DisplayName("Test that reconnects after dealing with an exception on connection failure")
+    @Test
+    @Order(2)
+    public void testTemporaryRefreshFailureOnStartupReconnect() throws Exception {
+        MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
+        mockEndpoint.reset();
+        mockEndpoint.expectedMessageCount(1);
 
-        Awaitility.await().untilAsserted(() -> {
-            template.asyncRequestBody("direct:start", "ping");
+        service.initialize();
 
-            MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
-        });
+        Awaitility.await()
+                .atMost(15, TimeUnit.SECONDS)
+                .untilAsserted(() -> {
+                    Assertions.assertDoesNotThrow(() -> template.requestBody("direct:start", "ping"));
+                });
 
+        mockEndpoint.assertIsSatisfied();
     }
 }
diff --git a/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/AbstractArtemisEmbeddedService.java b/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/AbstractArtemisEmbeddedService.java
index 740f242752b..526877af8f3 100644
--- a/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/AbstractArtemisEmbeddedService.java
+++ b/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/AbstractArtemisEmbeddedService.java
@@ -54,8 +54,9 @@ public abstract class AbstractArtemisEmbeddedService implements ArtemisService,
     }
 
     /**
-     * This is needed for some tests that check reliability of the components by defining the port in advance, trying to connect
-     * first starting the service later
+     * This is needed for some tests that check reliability of the components by defining the port in advance, trying to
+     * connect first starting the service later
+     *
      * @param port the port to use
      */
     protected AbstractArtemisEmbeddedService(int port) {
@@ -66,8 +67,7 @@ public abstract class AbstractArtemisEmbeddedService implements ArtemisService,
     }
 
     private synchronized Configuration configure(int port) {
-        final int brokerId = BROKER_COUNT.intValue();
-        BROKER_COUNT.increment();
+        final int brokerId = computeBrokerId();
 
         // Base configuration
         artemisConfiguration.setSecurityEnabled(false);
@@ -86,6 +86,17 @@ public abstract class AbstractArtemisEmbeddedService implements ArtemisService,
         return config;
     }
 
+    /**
+     * Computes the current broker ID to use.
+     *
+     * @return the broker ID to use
+     */
+    protected int computeBrokerId() {
+        final int brokerId = BROKER_COUNT.intValue();
+        BROKER_COUNT.increment();
+        return brokerId;
+    }
+
     private static File createInstance(int brokerId) {
         File instanceDir = null;
         final File target = new File("target");
diff --git a/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/ArtemisVMService.java b/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/ArtemisVMService.java
index c9d070b0fe2..6a916cef8a3 100644
--- a/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/ArtemisVMService.java
+++ b/test-infra/camel-test-infra-artemis/src/test/java/org/apache/camel/test/infra/artemis/services/ArtemisVMService.java
@@ -30,6 +30,17 @@ public class ArtemisVMService extends AbstractArtemisEmbeddedService {
 
     private String brokerURL;
 
+    /**
+     * This class should rarely be used. It is intended for some tests that check for reliability operations and require
+     * using the same broker ID between start/stop cycles.
+     */
+    public static class ReusableArtemisVMService extends ArtemisVMService {
+        @Override
+        protected int computeBrokerId() {
+            return 0;
+        }
+    }
+
     @Override
     protected Configuration configure(Configuration configuration, int port, int brokerId) {
         brokerURL = "vm://" + brokerId;