diff --git a/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java b/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java index 4313f91a1e5a6..d2ef34439a663 100644 --- a/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java +++ b/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java @@ -43,6 +43,12 @@ */ public abstract class ContextTestSupport extends TestSupport { + private static final ThreadLocal THREAD_CAMEL_CONTEXT = new ThreadLocal<>(); + private static final ThreadLocal THREAD_TEMPLATE = new ThreadLocal<>(); + private static final ThreadLocal THREAD_CONSUMER = new ThreadLocal<>(); + + private static final ThreadLocal THREAD_SERVICE = new ThreadLocal<>(); + protected volatile ModelCamelContext context; protected volatile ProducerTemplate template; protected volatile ConsumerTemplate consumer; @@ -74,6 +80,7 @@ public Service getCamelContextService() { */ public void setCamelContextService(Service camelContextService) { this.camelContextService = camelContextService; + THREAD_SERVICE.set(camelContextService); } /** @@ -97,6 +104,7 @@ public void setUp() throws Exception { assertValidContext(context); context.build(); + THREAD_CAMEL_CONTEXT.set(context); // make SEDA run faster context.getComponent("seda", SedaComponent.class).setDefaultPollTimeout(10); @@ -126,6 +134,9 @@ public void setUp() throws Exception { template.start(); consumer.start(); + THREAD_TEMPLATE.set(template); + THREAD_CONSUMER.set(consumer); + // create a default notifier when 1 exchange is done which is the most // common case oneExchangeDone = event().whenDone(1).create(); @@ -135,7 +146,7 @@ public void setUp() throws Exception { } // reduce default shutdown timeout to avoid waiting for 300 seconds - context.getShutdownStrategy().setTimeout(10); + context.getShutdownStrategy().setTimeout(getShutdownTimeout()); } @Override @@ -143,9 +154,15 @@ public void setUp() throws Exception { public void tearDown() throws Exception { log.debug("tearDown test: {}", getName()); if (consumer != null) { + if (consumer == THREAD_CONSUMER.get()) { + THREAD_CONSUMER.remove(); + } consumer.stop(); } if (template != null) { + if (template == THREAD_TEMPLATE.get()) { + THREAD_TEMPLATE.remove(); + } template.stop(); } stopCamelContext(); @@ -153,6 +170,17 @@ public void tearDown() throws Exception { super.tearDown(); } + /** + * Returns the timeout to use when shutting down (unit in seconds). + *

+ * Will default use 10 seconds. + * + * @return the timeout to use + */ + protected int getShutdownTimeout() { + return 10; + } + /** * Whether or not JMX should be used during testing. * @@ -172,9 +200,15 @@ protected boolean isLoadTypeConverters() { protected void stopCamelContext() throws Exception { if (camelContextService != null) { + if (camelContextService == THREAD_SERVICE.get()) { + THREAD_SERVICE.remove(); + } camelContextService.stop(); } else { if (context != null) { + if (context == THREAD_CAMEL_CONTEXT.get()) { + THREAD_CAMEL_CONTEXT.remove(); + } context.stop(); } } @@ -184,7 +218,14 @@ protected void startCamelContext() throws Exception { if (camelContextService != null) { camelContextService.start(); } else { - context.start(); + if (context instanceof DefaultCamelContext) { + DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; + if (!defaultCamelContext.isStarted()) { + defaultCamelContext.start(); + } + } else { + context.start(); + } } } diff --git a/parent/pom.xml b/parent/pom.xml index 9e92bad1d5923..f6d79c8ad7744 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -38,7 +38,7 @@ -XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError ${camel.surefire.fork.additional-vmargs} 1 600 - true + false false 1 dynamic