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

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


The following commit(s) were added to refs/heads/main by this push:
     new df0f7e3d2edf CAMEL-26762: camel-test-infra-common - SingletonService 
registers its shutdown hook once (#31116)
df0f7e3d2edf is described below

commit df0f7e3d2edf3f8c96c7de92f6bfe55b59896b1e
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Nov 30 14:34:53 2026 +0100

    CAMEL-26762: camel-test-infra-common - SingletonService registers its 
shutdown hook once (#31116)
    
    * CAMEL-26762: camel-test-infra-common - SingletonService registers its 
shutdown hook once
    * CAMEL-26762: register the shutdown hook once per singleton instance, also 
across launcher executions
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../AzureEventHubsLocalContainerInfraService.java  |  16 ---
 .../infra/common/services/SingletonService.java    |  20 +++-
 .../common/services/SingletonServiceTest.java      | 116 +++++++++++++++++++++
 3 files changed, 134 insertions(+), 18 deletions(-)

diff --git 
a/test-infra/camel-test-infra-azure-eventhubs/src/main/java/org/apache/camel/test/infra/azure/eventhubs/services/AzureEventHubsLocalContainerInfraService.java
 
b/test-infra/camel-test-infra-azure-eventhubs/src/main/java/org/apache/camel/test/infra/azure/eventhubs/services/AzureEventHubsLocalContainerInfraService.java
index b5a71a6e1d68..27ed14953a72 100644
--- 
a/test-infra/camel-test-infra-azure-eventhubs/src/main/java/org/apache/camel/test/infra/azure/eventhubs/services/AzureEventHubsLocalContainerInfraService.java
+++ 
b/test-infra/camel-test-infra-azure-eventhubs/src/main/java/org/apache/camel/test/infra/azure/eventhubs/services/AzureEventHubsLocalContainerInfraService.java
@@ -76,7 +76,6 @@ public class AzureEventHubsLocalContainerInfraService extends 
FlociAzInfraServic
     private static final String SHARED_ACCESS_KEY = "devkey";
 
     private int amqpPort;
-    private volatile boolean started;
 
     @Override
     protected FlociAzContainer initContainer(String imageName) {
@@ -95,7 +94,6 @@ public class AzureEventHubsLocalContainerInfraService extends 
FlociAzInfraServic
 
     @Override
     public void initialize() {
-        started = true;
         try {
             getContainer().withBrokerImage(ARTEMIS_IMAGE_VARIABLE);
             getContainer().start();
@@ -149,20 +147,6 @@ public class AzureEventHubsLocalContainerInfraService 
extends FlociAzInfraServic
         return Integer.parseInt(matcher.group(1));
     }
 
-    /**
-     * Stops Floci AZ, which removes the broker too. A singleton service is 
shut down by a JVM shutdown hook for each
-     * test class that used it, and these hooks run at the same time.
-     */
-    @Override
-    public synchronized void shutdown() {
-        if (!started) {
-            return;
-        }
-        started = false;
-
-        super.shutdown();
-    }
-
     @Override
     public void registerProperties() {
         super.registerProperties();
diff --git 
a/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/services/SingletonService.java
 
b/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/services/SingletonService.java
index 3ba09382115d..b9fea00db6f1 100644
--- 
a/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/services/SingletonService.java
+++ 
b/test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/services/SingletonService.java
@@ -17,6 +17,8 @@
 
 package org.apache.camel.test.infra.common.services;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,6 +34,7 @@ public class SingletonService<T extends InfrastructureService>
 
     private final T service;
     private final String name;
+    private final AtomicBoolean shutdownHookRegistered = new AtomicBoolean();
 
     public SingletonService(T service, String name) {
         this.service = service;
@@ -46,16 +49,29 @@ public class SingletonService<T extends 
InfrastructureService>
         LOG.debug("Using store: {}", store);
 
         store.computeIfAbsent(name, this::doInitializeService);
-
-        Runtime.getRuntime().addShutdownHook(new Thread(service::shutdown));
     }
 
     protected SingletonService<T> doInitializeService(String name) {
         LOG.debug("Registering singleton service {}", name);
         service.initialize();
+        // the root store, which keeps the singleton, lasts one launcher 
execution: surefire and failsafe run one
+        // execution per test class when they reuse forks, and a rerun of 
failing tests is another one, so the service can
+        // be initialized again in the same JVM; the instance, which the 
factories keep in a static field, is the same
+        if (shutdownHookRegistered.compareAndSet(false, true)) {
+            registerShutdownHook();
+        }
         return this;
     }
 
+    /**
+     * Has the service shut down when the JVM exits. It is called once for 
this instance, when the service is first
+     * initialized: each test class that uses the singleton runs {@link 
#beforeAll(ExtensionContext)}, and a hook for
+     * each of them would shut the service down as many times, at the same 
time.
+     */
+    protected void registerShutdownHook() {
+        Runtime.getRuntime().addShutdownHook(new Thread(service::shutdown));
+    }
+
     @Override
     public void beforeAll(ExtensionContext extensionContext) {
         addToStore(extensionContext);
diff --git 
a/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/SingletonServiceTest.java
 
b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/SingletonServiceTest.java
new file mode 100644
index 000000000000..d70bf5350868
--- /dev/null
+++ 
b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/SingletonServiceTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.test.infra.common.services;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The singleton service is registered on this class and inherited by its two 
nested classes, so three test classes use
+ * it, as the test classes of a module use the singleton of a test-infra 
service.
+ */
+class SingletonServiceTest {
+
+    private static final AtomicInteger INITIALIZATIONS = new AtomicInteger();
+    private static final AtomicInteger SHUTDOWN_HOOKS = new AtomicInteger();
+
+    @RegisterExtension
+    static SingletonService<InfrastructureService> service
+            = new SingletonService<>(new CountingService(INITIALIZATIONS), 
"camel-test-infra-common-singleton-service-test") {
+                @Override
+                protected void registerShutdownHook() {
+                    SHUTDOWN_HOOKS.incrementAndGet();
+                }
+            };
+
+    @AfterAll
+    static void initializedOnceWithOneShutdownHook() {
+        assertEquals(1, INITIALIZATIONS.get(), "the service is initialized 
once");
+        assertEquals(1, SHUTDOWN_HOOKS.get(), "one shutdown hook is 
registered, however many test classes use the service");
+    }
+
+    @Test
+    void usesTheService() {
+        assertEquals(1, INITIALIZATIONS.get());
+    }
+
+    // surefire and failsafe run one launcher execution, with a root store of 
its own, per test class when they reuse
+    // forks: each execution initializes the singleton again, on the same 
instance
+    @Test
+    void registersOneShutdownHookAcrossLauncherExecutions() {
+        AtomicInteger initializations = new AtomicInteger();
+        AtomicInteger shutdownHooks = new AtomicInteger();
+        SingletonService<InfrastructureService> singleton
+                = new SingletonService<>(new CountingService(initializations), 
"another-launcher-execution") {
+                    @Override
+                    protected void registerShutdownHook() {
+                        shutdownHooks.incrementAndGet();
+                    }
+                };
+
+        singleton.doInitializeService("another-launcher-execution");
+        singleton.doInitializeService("another-launcher-execution");
+
+        assertEquals(2, initializations.get());
+        assertEquals(1, shutdownHooks.get());
+    }
+
+    @Nested
+    class SecondTestClass {
+        @Test
+        void usesTheService() {
+            assertEquals(1, INITIALIZATIONS.get());
+        }
+    }
+
+    @Nested
+    class ThirdTestClass {
+        @Test
+        void usesTheService() {
+            assertEquals(1, INITIALIZATIONS.get());
+        }
+    }
+
+    private static final class CountingService implements 
InfrastructureService {
+        private final AtomicInteger initializations;
+
+        CountingService(AtomicInteger initializations) {
+            this.initializations = initializations;
+        }
+
+        @Override
+        public void registerProperties() {
+            // NO-OP
+        }
+
+        @Override
+        public void initialize() {
+            initializations.incrementAndGet();
+        }
+
+        @Override
+        public void shutdown() {
+            // NO-OP
+        }
+    }
+}

Reply via email to