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

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

commit 7e059e88377ea9032c18ef80cd686e953ba1b524
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Fri Mar 12 20:20:42 2021 +0100

    Fix concurrency issue with the noStart option
---
 .../apache/camel/spring/processor/SpringTestHelper.java | 10 +++++++---
 .../spring/junit5/CamelSpringTestContextLoader.java     | 11 +++++++----
 .../spring/CamelSpringDelegatingTestContextLoader.java  |  4 +---
 .../camel/test/spring/CamelSpringTestContextLoader.java | 11 +++++++----
 .../camel/test/spring/CamelSpringTestSupport.java       | 17 ++++++++++-------
 .../java/org/apache/camel/impl/DefaultCamelContext.java |  2 +-
 6 files changed, 33 insertions(+), 22 deletions(-)

diff --git 
a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTestHelper.java
 
b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTestHelper.java
index 7237562..7423aa8 100644
--- 
a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTestHelper.java
+++ 
b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/processor/SpringTestHelper.java
@@ -41,8 +41,13 @@ public final class SpringTestHelper {
         test.setUseRouteBuilder(false);
 
         boolean isNoStart = DefaultCamelContext.isNoStart();
-        DefaultCamelContext.setNoStart(true);
-        final AbstractXmlApplicationContext applicationContext = new 
ClassPathXmlApplicationContext(classpathUri);
+        final AbstractXmlApplicationContext applicationContext;
+        try {
+            DefaultCamelContext.setNoStart(true);
+            applicationContext = new 
ClassPathXmlApplicationContext(classpathUri);
+        } finally {
+            DefaultCamelContext.setNoStart(isNoStart);
+        }
         test.setCamelContextService(new Service() {
             public void start() {
                 applicationContext.start();
@@ -57,7 +62,6 @@ public final class SpringTestHelper {
         for (Map.Entry<String, Object> entry : beans.entrySet()) {
             context.getRegistry().bind(entry.getKey(), entry.getValue());
         }
-        DefaultCamelContext.setNoStart(isNoStart);
         if (!isNoStart) {
             context.start();
         }
diff --git 
a/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestContextLoader.java
 
b/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestContextLoader.java
index 3b5275a..f43cf55 100644
--- 
a/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestContextLoader.java
+++ 
b/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestContextLoader.java
@@ -131,10 +131,13 @@ public class CamelSpringTestContextLoader extends 
AbstractContextLoader {
 
         // Temporarily disable CamelContext start while the contexts are 
instantiated.
         SpringCamelContext.setNoStart(true);
-        context.refresh();
-        context.registerShutdownHook();
-        // Turn CamelContext startup back on since the context's have now been 
instantiated.
-        SpringCamelContext.setNoStart(false);
+        try {
+            context.refresh();
+            context.registerShutdownHook();
+        } finally {
+            // Turn CamelContext startup back on since the context's have now 
been instantiated.
+            SpringCamelContext.setNoStart(false);
+        }
 
         // Post CamelContext(s) instantiation but pre CamelContext(s) start 
setup
         CamelAnnotationsHandler.handleRouteCoverage(context, testClass, s -> 
getTestMethod().getName());
diff --git 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
index 79d36a6..d949d80 100644
--- 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
+++ 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
@@ -55,13 +55,11 @@ public class CamelSpringDelegatingTestContextLoader extends 
DelegatingSmartConte
 
         try {
             SpringCamelContext.setNoStart(true);
-            System.setProperty("skipStartingCamelContext", "true");
             ConfigurableApplicationContext context = 
(ConfigurableApplicationContext) super.loadContext(mergedConfig);
-            SpringCamelContext.setNoStart(false);
-            System.clearProperty("skipStartingCamelContext");
             return loadContext(context, testClass);
         } finally {
             CamelAnnotationsHandler.cleanup(testClass);
+            SpringCamelContext.setNoStart(false);
         }
     }
 
diff --git 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
index 7224d50..b808b6d 100644
--- 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
+++ 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
@@ -131,10 +131,13 @@ public class CamelSpringTestContextLoader extends 
AbstractContextLoader {
 
         // Temporarily disable CamelContext start while the contexts are 
instantiated.
         SpringCamelContext.setNoStart(true);
-        context.refresh();
-        context.registerShutdownHook();
-        // Turn CamelContext startup back on since the context's have now been 
instantiated.
-        SpringCamelContext.setNoStart(false);
+        try {
+            context.refresh();
+            context.registerShutdownHook();
+        } finally {
+            // Turn CamelContext startup back on since the context's have now 
been instantiated.
+            SpringCamelContext.setNoStart(false);
+        }
 
         // Post CamelContext(s) instantiation but pre CamelContext(s) start 
setup
         CamelAnnotationsHandler.handleRouteCoverage(context, testClass, s -> 
getTestMethod().getName());
diff --git 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
index 958ee7e..3ffc947 100644
--- 
a/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
+++ 
b/components/camel-test/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
@@ -59,16 +59,19 @@ public abstract class CamelSpringTestSupport extends 
CamelTestSupport {
             // after we are finished setting up the unit test
             synchronized (lock) {
                 SpringCamelContext.setNoStart(true);
-                if (isCreateCamelContextPerClass()) {
-                    applicationContext = threadAppContext.get();
-                    if (applicationContext == null) {
+                try {
+                    if (isCreateCamelContextPerClass()) {
+                        applicationContext = threadAppContext.get();
+                        if (applicationContext == null) {
+                            applicationContext = doCreateApplicationContext();
+                            threadAppContext.set(applicationContext);
+                        }
+                    } else {
                         applicationContext = doCreateApplicationContext();
-                        threadAppContext.set(applicationContext);
                     }
-                } else {
-                    applicationContext = doCreateApplicationContext();
+                } finally {
+                    SpringCamelContext.setNoStart(false);
                 }
-                SpringCamelContext.setNoStart(false);
             }
         } else {
             log.info("Skipping starting CamelContext as system property 
skipStartingCamelContext is set to be true.");
diff --git 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index c482064..0580d7d 100644
--- 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -165,7 +165,7 @@ public class DefaultCamelContext extends SimpleCamelContext 
implements ModelCame
     public void start() {
         // for example from unit testing we want to start Camel later 
(manually)
         if (isNoStart()) {
-            LOG.trace("Ignoring start() as NO_START is false");
+            LOG.trace("Ignoring start() as NO_START is true");
             return;
         }
 

Reply via email to