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 5c03209c5e3314a059f19366869988212286c004 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Tue Apr 23 13:13:22 2024 +0200 (chores) camel-core: fix non-atomic operations on volatile fields --- .../component/file/FileBeginFailureOneTimeTest.java | 9 +++++---- .../file/FileConsumerCustomSchedulerTest.java | 7 ++++--- .../apache/camel/impl/EndpointShutdownOnceTest.java | 7 ++++--- .../camel/impl/Mock321ScheduledPollConsumer.java | 10 ++++++---- .../processor/DeadLetterChannelNoRedeliveryTest.java | 8 +++++--- .../RedeliveryErrorHandlerBlockedDelayTest.java | 7 +++++-- .../RedeliveryErrorHandlerNonBlockedDelayTest.java | 7 +++++-- ...veryErrorHandlerNonBlockedRedeliveryHeaderTest.java | 7 +++++-- .../RedeliveryOnExceptionBlockedDelayTest.java | 7 +++++-- .../AggregateSimpleExpressionIssueManualTest.java | 6 ++++-- .../async/AsyncEndpointCustomRoutePolicyTest.java | 7 ++++--- ...ointRedeliveryErrorHandlerNonBlockedDelay2Test.java | 7 +++++-- ...pointRedeliveryErrorHandlerNonBlockedDelayTest.java | 7 +++++-- .../org/apache/camel/management/LoadTimerTest.java | 8 +++++--- .../support/task/BackgroundIterationTimeTaskTest.java | 14 +++++++------- .../apache/camel/support/task/BackgroundTaskTest.java | 10 +++++----- .../apache/camel/support/task/ForegroundTaskTest.java | 12 ++++++------ .../camel/support/task/ForegroundTimeTaskTest.java | 14 +++++++------- .../org/apache/camel/support/task/TaskTestSupport.java | 18 ++++++++++-------- 19 files changed, 102 insertions(+), 70 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileBeginFailureOneTimeTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileBeginFailureOneTimeTest.java index 81dc264ca50..f4cc528c190 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileBeginFailureOneTimeTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileBeginFailureOneTimeTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.file; import java.io.File; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; @@ -63,7 +64,7 @@ public class FileBeginFailureOneTimeTest extends ContextTestSupport { private static class MyStrategy implements GenericFileProcessStrategy<File> { - private volatile int invoked; + private LongAdder invoked = new LongAdder(); @Override public void prepareOnStartup( @@ -75,8 +76,8 @@ public class FileBeginFailureOneTimeTest extends ContextTestSupport { GenericFileOperations<File> fileGenericFileOperations, GenericFileEndpoint<File> fileGenericFileEndpoint, Exchange exchange, GenericFile<File> fileGenericFile) { - invoked++; - if (invoked <= 1) { + invoked.increment(); + if (invoked.intValue() <= 1) { throw new IllegalArgumentException("Damn I cannot do this"); } return true; @@ -105,7 +106,7 @@ public class FileBeginFailureOneTimeTest extends ContextTestSupport { } public int getInvoked() { - return invoked; + return invoked.intValue(); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java index aad6cd3efc8..735e462caf0 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java @@ -18,6 +18,7 @@ package org.apache.camel.component.file; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.CamelContext; import org.apache.camel.Consumer; @@ -71,7 +72,7 @@ public class FileConsumerCustomSchedulerTest extends ContextTestSupport { private CamelContext camelContext; private TimerTask timerTask; - private volatile int counter; + private LongAdder counter = new LongAdder(); private String foo; @Override @@ -84,7 +85,7 @@ public class FileConsumerCustomSchedulerTest extends ContextTestSupport { this.timerTask = new TimerTask() { @Override public void run() { - counter++; + counter.increment(); task.run(); } }; @@ -96,7 +97,7 @@ public class FileConsumerCustomSchedulerTest extends ContextTestSupport { } public int getCounter() { - return counter; + return counter.intValue(); } public String getFoo() { diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/EndpointShutdownOnceTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/EndpointShutdownOnceTest.java index 24426861ce8..ddb63e0b1bc 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/EndpointShutdownOnceTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/EndpointShutdownOnceTest.java @@ -17,6 +17,7 @@ package org.apache.camel.impl; import java.util.Map; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.CamelContext; import org.apache.camel.Component; @@ -62,14 +63,14 @@ public class EndpointShutdownOnceTest { private static final class MyEndpoint extends DefaultEndpoint { - private volatile int invoked; + private LongAdder invoked = new LongAdder(); private MyEndpoint(String endpointUri, Component component) { super(endpointUri, component); } public int getInvoked() { - return invoked; + return invoked.intValue(); } @Override @@ -90,7 +91,7 @@ public class EndpointShutdownOnceTest { @Override protected void doShutdown() throws Exception { super.doShutdown(); - invoked++; + invoked.increment(); } } } diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/Mock321ScheduledPollConsumer.java b/core/camel-core/src/test/java/org/apache/camel/impl/Mock321ScheduledPollConsumer.java index 611d3568d71..e958e251eb1 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/Mock321ScheduledPollConsumer.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/Mock321ScheduledPollConsumer.java @@ -16,12 +16,14 @@ */ package org.apache.camel.impl; +import java.util.concurrent.atomic.AtomicInteger; + import org.apache.camel.Processor; import org.apache.camel.support.DefaultEndpoint; public class Mock321ScheduledPollConsumer extends MockScheduledPollConsumer { - private volatile int counter = 4; + private final AtomicInteger counter = new AtomicInteger(4); public Mock321ScheduledPollConsumer(DefaultEndpoint endpoint, Processor processor) { super(endpoint, processor); @@ -29,10 +31,10 @@ public class Mock321ScheduledPollConsumer extends MockScheduledPollConsumer { @Override protected int poll() { - if (counter > 0) { - counter = counter - 1; + if (counter.get() > 0) { + counter.decrementAndGet(); } - return counter; + return counter.get(); } @Override diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java index 695fd045163..6f8fe66345a 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNoRedeliveryTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -29,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; */ public class DeadLetterChannelNoRedeliveryTest extends ContextTestSupport { - private static volatile int counter; + private static LongAdder counter = new LongAdder(); @Test public void testDLCNoRedelivery() throws Exception { @@ -41,7 +43,7 @@ public class DeadLetterChannelNoRedeliveryTest extends ContextTestSupport { assertMockEndpointsSatisfied(); - assertEquals(1, counter, "Only the original attempt"); + assertEquals(1, counter.intValue(), "Only the original attempt"); } @Override @@ -60,7 +62,7 @@ public class DeadLetterChannelNoRedeliveryTest extends ContextTestSupport { @Override public void process(Exchange exchange) { - counter++; + counter.increment(); throw new IllegalArgumentException("Forced"); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerBlockedDelayTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerBlockedDelayTest.java index 030ffab3961..369424da82f 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerBlockedDelayTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerBlockedDelayTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -29,7 +31,7 @@ public class RedeliveryErrorHandlerBlockedDelayTest extends ContextTestSupport { private static final Logger LOG = LoggerFactory.getLogger(RedeliveryErrorHandlerBlockedDelayTest.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); @Test public void testRedelivery() throws Exception { @@ -61,7 +63,8 @@ public class RedeliveryErrorHandlerBlockedDelayTest extends ContextTestSupport { String body = exchange.getIn().getBody(String.class); if (body.contains("World")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedDelayTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedDelayTest.java index e82c882ca5d..e7bc686ee52 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedDelayTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedDelayTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -29,7 +31,7 @@ public class RedeliveryErrorHandlerNonBlockedDelayTest extends ContextTestSuppor private static final Logger LOG = LoggerFactory.getLogger(RedeliveryErrorHandlerNonBlockedDelayTest.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); @Test public void testRedelivery() throws Exception { @@ -61,7 +63,8 @@ public class RedeliveryErrorHandlerNonBlockedDelayTest extends ContextTestSuppor String body = exchange.getIn().getBody(String.class); if (body.contains("World")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest.java index ec1cf71005e..0974b4ad74a 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -29,7 +31,7 @@ public class RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest extends Contex private static final Logger LOG = LoggerFactory.getLogger(RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); @Test public void testRedelivery() throws Exception { @@ -63,7 +65,8 @@ public class RedeliveryErrorHandlerNonBlockedRedeliveryHeaderTest extends Contex String body = exchange.getIn().getBody(String.class); if (body.contains("World")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryOnExceptionBlockedDelayTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryOnExceptionBlockedDelayTest.java index c55298d9501..9f2f368c790 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryOnExceptionBlockedDelayTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/RedeliveryOnExceptionBlockedDelayTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -29,7 +31,7 @@ public class RedeliveryOnExceptionBlockedDelayTest extends ContextTestSupport { private static final Logger LOG = LoggerFactory.getLogger(RedeliveryOnExceptionBlockedDelayTest.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); @Test public void testRedelivery() throws Exception { @@ -61,7 +63,8 @@ public class RedeliveryOnExceptionBlockedDelayTest extends ContextTestSupport { String body = exchange.getIn().getBody(String.class); if (body.contains("World")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateSimpleExpressionIssueManualTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateSimpleExpressionIssueManualTest.java index bc0360590e1..2b828f82434 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateSimpleExpressionIssueManualTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateSimpleExpressionIssueManualTest.java @@ -21,6 +21,7 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.AggregationStrategy; import org.apache.camel.ContextTestSupport; @@ -87,10 +88,11 @@ public class AggregateSimpleExpressionIssueManualTest extends ContextTestSupport } public static final class MyBean { - private volatile int cnt; + private LongAdder cnt = new LongAdder(); public void invoke(final List<String> strList) { - LOG.info("Batch {}", ++cnt); + cnt.increment(); + LOG.info("Batch {}", cnt.intValue()); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointCustomRoutePolicyTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointCustomRoutePolicyTest.java index 1fd1901fcfa..f3255198973 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointCustomRoutePolicyTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointCustomRoutePolicyTest.java @@ -18,6 +18,7 @@ package org.apache.camel.processor.async; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; @@ -40,13 +41,13 @@ public class AsyncEndpointCustomRoutePolicyTest extends ContextTestSupport { private static class MyCustomRoutePolicy extends RoutePolicySupport { - private volatile int invoked; + private LongAdder invoked = new LongAdder(); private final AtomicBoolean stopped = new AtomicBoolean(); @Override public void onExchangeDone(Route route, Exchange exchange) { - invoked++; - if (invoked >= 2) { + invoked.increment(); + if (invoked.intValue() >= 2) { try { stopped.set(true); stopConsumer(route.getConsumer()); diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test.java b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test.java index c238895f586..5a309593e4c 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor.async; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -31,7 +33,7 @@ public class AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test extends Con private static final Logger LOG = LoggerFactory.getLogger(AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); private static String beforeThreadName; private static String afterThreadName; @@ -69,7 +71,8 @@ public class AsyncEndpointRedeliveryErrorHandlerNonBlockedDelay2Test extends Con String body = exchange.getIn().getBody(String.class); if (body.contains("Camel")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest.java index 39166c4cfec..e7fd429512d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.processor.async; +import java.util.concurrent.atomic.LongAdder; + import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -31,7 +33,7 @@ public class AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest extends Cont private static final Logger LOG = LoggerFactory.getLogger(AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest.class); - private static volatile int attempt; + private static LongAdder attempt = new LongAdder(); private static String beforeThreadName; private static String afterThreadName; @@ -66,7 +68,8 @@ public class AsyncEndpointRedeliveryErrorHandlerNonBlockedDelayTest extends Cont String body = exchange.getIn().getBody(String.class); if (body.contains("World")) { - if (++attempt <= 2) { + attempt.increment(); + if (attempt.intValue() <= 2) { LOG.info("Processing failed will thrown an exception"); throw new IllegalArgumentException("Damn"); } diff --git a/core/camel-management/src/test/java/org/apache/camel/management/LoadTimerTest.java b/core/camel-management/src/test/java/org/apache/camel/management/LoadTimerTest.java index 66c3f053504..200bc34f1e2 100644 --- a/core/camel-management/src/test/java/org/apache/camel/management/LoadTimerTest.java +++ b/core/camel-management/src/test/java/org/apache/camel/management/LoadTimerTest.java @@ -17,6 +17,7 @@ package org.apache.camel.management; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.LongAdder; import org.apache.camel.ContextTestSupport; import org.apache.camel.TimerListener; @@ -50,7 +51,7 @@ public class LoadTimerTest extends ContextTestSupport { myTimer.addTimerListener(test); try { await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { - assertTrue(test.counter >= SAMPLES); + assertTrue(test.counter.intValue() >= SAMPLES); assertFalse(Double.isNaN(test.load.getLoad1())); assertTrue(test.load.getLoad1() > 0.0d); assertTrue(test.load.getLoad1() < SAMPLES); @@ -64,12 +65,13 @@ public class LoadTimerTest extends ContextTestSupport { private static class TestLoadAware implements TimerListener { - volatile int counter; + LongAdder counter = new LongAdder(); final LoadTriplet load = new LoadTriplet(); @Override public void onTimer() { - load.update(++counter); + counter.increment(); + load.update(counter.intValue()); } } diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java index b7f112e1a2c..d2f4b22886d 100644 --- a/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java +++ b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java @@ -48,7 +48,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::booleanSupplier); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); assertFalse(completed, "The task did not complete, the return should be false"); } @@ -67,7 +67,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::booleanSupplier); - assertTrue(maxIterations > taskCount, "The task execution should not exceed the max iterations"); + assertTrue(maxIterations > taskCount.intValue(), "The task execution should not exceed the max iterations"); assertFalse(completed, "The task did not complete, the return should be false"); } @@ -85,7 +85,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicate, new Object()); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); assertFalse(completed, "The task did not complete, the return should be false"); } @@ -105,7 +105,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStop, Integer.valueOf(3)); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); assertTrue(completed, "The task did complete, the return should be true"); } @@ -124,7 +124,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3)); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); assertFalse(completed, "The task did not complete because of timeout, the return should be false"); } @@ -143,7 +143,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3)); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); assertFalse(completed, "The task did not complete because of timeout, the return should be false"); } @@ -161,7 +161,7 @@ public class BackgroundIterationTimeTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStop, 4); - assertTrue(maxIterations > taskCount, "The task execution should not exceed the max iterations"); + assertTrue(maxIterations > taskCount.intValue(), "The task execution should not exceed the max iterations"); assertTrue(completed, "The task did not complete, the return should be false"); } } diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundTaskTest.java b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundTaskTest.java index 2907d059d07..b7734efaa9c 100644 --- a/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundTaskTest.java +++ b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundTaskTest.java @@ -58,7 +58,7 @@ public class BackgroundTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::booleanSupplier); - assertTrue(taskCount <= maxIterations); + assertTrue(taskCount.intValue() <= maxIterations); assertFalse(completed, "The task did not complete, the return should be false"); Duration duration = task.elapsed(); @@ -88,7 +88,7 @@ public class BackgroundTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::booleanSupplier); - assertTrue((maxIterations - 1) <= taskCount); + assertTrue((maxIterations - 1) <= taskCount.intValue()); assertFalse(completed, "The task did not complete, the return should be false"); Duration duration = task.elapsed(); @@ -118,7 +118,7 @@ public class BackgroundTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicate, new Object()); - assertTrue(taskCount <= maxIterations); + assertTrue(taskCount.intValue() <= maxIterations); assertFalse(completed, "The task did not complete, the return should be false"); Duration duration = task.elapsed(); @@ -147,7 +147,7 @@ public class BackgroundTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStop, Integer.valueOf(3)); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); assertTrue(completed, "The task did complete, the return should be true"); } @@ -169,7 +169,7 @@ public class BackgroundTaskTest extends TaskTestSupport { .build(); boolean completed = task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3)); - assertTrue(taskCount <= 2, "Slow task: it should not run more than 2 times in 4 seconds"); + assertTrue(taskCount.intValue() <= 2, "Slow task: it should not run more than 2 times in 4 seconds"); Duration duration = task.elapsed(); assertNotNull(duration); diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTaskTest.java b/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTaskTest.java index 83b9f543aa0..7c31afb160b 100644 --- a/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTaskTest.java +++ b/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTaskTest.java @@ -45,7 +45,7 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::booleanSupplier); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); Duration duration = task.elapsed(); assertNotNull(duration); @@ -67,7 +67,7 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::booleanSupplier); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); } @DisplayName("Test that the task does not run for more than the max iterations when using a predicate and an initial delay") @@ -84,7 +84,7 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicate, new Object()); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); } @DisplayName("Test that the task stops running once the predicate is true") @@ -101,7 +101,7 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStop, Integer.valueOf(3)); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); } @DisplayName("Test that the task stops running once the predicate is true when the test is slow") @@ -118,7 +118,7 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3)); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); } @DisplayName("Test that the task stops running once the predicate is true when the test is slow") @@ -135,6 +135,6 @@ public class ForegroundTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3)); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); } } diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTimeTaskTest.java b/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTimeTaskTest.java index 795659b242f..e6b9a2bd9fa 100644 --- a/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTimeTaskTest.java +++ b/core/camel-support/src/test/java/org/apache/camel/support/task/ForegroundTimeTaskTest.java @@ -46,7 +46,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::booleanSupplier); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); Duration duration = task.elapsed(); assertNotNull(duration); @@ -70,7 +70,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::booleanSupplier); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); } @DisplayName("Test that the task does not run for more than the max iterations when using a predicate and an initial delay") @@ -88,7 +88,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicate, new Object()); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); } @DisplayName("Test that the task does not run for more than the max duration when using a predicate and an initial delay") @@ -106,7 +106,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicate, new Object()); - assertEquals(maxIterations, taskCount); + assertEquals(maxIterations, taskCount.intValue()); } @DisplayName("Test that the task stops running once the predicate is true") @@ -124,7 +124,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStop, 3); - assertEquals(3, taskCount); + assertEquals(3, taskCount.intValue()); } @DisplayName("Test that the task stops running once the predicate is true when the test is slow") @@ -142,7 +142,7 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStopSlow, 3); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); } @DisplayName("Test that the task stops running once the predicate is true when the test is slow") @@ -160,6 +160,6 @@ class ForegroundTimeTaskTest extends TaskTestSupport { .build(); task.run(this::taskPredicateWithDeterministicStopSlow, 3); - assertTrue(taskCount < maxIterations); + assertTrue(taskCount.intValue() < maxIterations); } } diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/TaskTestSupport.java b/core/camel-support/src/test/java/org/apache/camel/support/task/TaskTestSupport.java index b51e98a5bd8..a0cd53bbb2a 100644 --- a/core/camel-support/src/test/java/org/apache/camel/support/task/TaskTestSupport.java +++ b/core/camel-support/src/test/java/org/apache/camel/support/task/TaskTestSupport.java @@ -17,23 +17,25 @@ package org.apache.camel.support.task; +import java.util.concurrent.atomic.LongAdder; + import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertNotNull; public class TaskTestSupport { protected final int maxIterations = 5; - protected volatile int taskCount; + protected LongAdder taskCount = new LongAdder(); protected boolean booleanSupplier() { - taskCount++; + taskCount.increment(); return false; } protected boolean taskPredicate(Object o) { assertNotNull(o); - taskCount++; + taskCount.increment(); return false; } @@ -41,8 +43,8 @@ public class TaskTestSupport { protected boolean taskPredicateWithDeterministicStop(Integer stopAtValue) { assertNotNull(stopAtValue); - taskCount++; - if (taskCount == stopAtValue.intValue()) { + taskCount.increment(); + if (taskCount.intValue() == stopAtValue) { return true; } @@ -58,8 +60,8 @@ public class TaskTestSupport { return false; } - taskCount++; - if (taskCount == stopAtValue.intValue()) { + taskCount.increment(); + if (taskCount.intValue() == stopAtValue) { return true; } @@ -68,6 +70,6 @@ public class TaskTestSupport { @BeforeEach void setUp() { - taskCount = 0; + taskCount.reset(); } }
