Skip to content

Commit

Permalink
Added ThreadLocal to handle threading issues in bulk unit tests; upda…
Browse files Browse the repository at this point in the history
…ted reuseForks property to default to 'false'
  • Loading branch information
Craig Ziesman committed Jan 3, 2024
1 parent 90faf3f commit eeb8731
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
*/
public abstract class ContextTestSupport extends TestSupport {

private static final ThreadLocal<ModelCamelContext> THREAD_CAMEL_CONTEXT = new ThreadLocal<>();
private static final ThreadLocal<ProducerTemplate> THREAD_TEMPLATE = new ThreadLocal<>();
private static final ThreadLocal<ConsumerTemplate> THREAD_CONSUMER = new ThreadLocal<>();

private static final ThreadLocal<Service> THREAD_SERVICE = new ThreadLocal<>();

protected volatile ModelCamelContext context;
protected volatile ProducerTemplate template;
protected volatile ConsumerTemplate consumer;
Expand Down Expand Up @@ -74,6 +80,7 @@ public Service getCamelContextService() {
*/
public void setCamelContextService(Service camelContextService) {
this.camelContextService = camelContextService;
THREAD_SERVICE.set(camelContextService);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -135,24 +146,41 @@ public void setUp() throws Exception {
}

// reduce default shutdown timeout to avoid waiting for 300 seconds
context.getShutdownStrategy().setTimeout(10);
context.getShutdownStrategy().setTimeout(getShutdownTimeout());
}

@Override
@AfterEach
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();

super.tearDown();
}

/**
* Returns the timeout to use when shutting down (unit in seconds).
* <p/>
* Will default use 10 seconds.
*
* @return the timeout to use
*/
protected int getShutdownTimeout() {
return 10;
}

/**
* Whether or not JMX should be used during testing.
*
Expand All @@ -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();
}
}
Expand All @@ -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();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<camel.surefire.fork.vmargs>-XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError ${camel.surefire.fork.additional-vmargs}</camel.surefire.fork.vmargs>
<camel.surefire.forkCount>1</camel.surefire.forkCount>
<camel.surefire.forkTimeout>600</camel.surefire.forkTimeout>
<camel.surefire.reuseForks>true</camel.surefire.reuseForks>
<camel.surefire.reuseForks>false</camel.surefire.reuseForks>
<camel.surefire.parallel>false</camel.surefire.parallel>
<camel.surefire.parallel.factor>1</camel.surefire.parallel.factor>
<camel.surefire.parallel.strategy>dynamic</camel.surefire.parallel.strategy>
Expand Down

0 comments on commit eeb8731

Please sign in to comment.