From bd4f4426728dc25d25e8211fe57bbc019fb8a382 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Fri, 1 Feb 2019 11:54:12 +0100 Subject: [PATCH] Fix concurrency issue in RetryTransactionalPollingIntegrationTests (cherry picked from commit ffe158cadef2d86f1388ed1a098528fc97b6c1e5) --- .../retry/RetryTransactionalPollingIntegrationTests.java | 7 ++++--- .../batch/integration/retry/SimpleService.java | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/RetryTransactionalPollingIntegrationTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/RetryTransactionalPollingIntegrationTests.java index cc91f05e5..63fc774f0 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/RetryTransactionalPollingIntegrationTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/RetryTransactionalPollingIntegrationTests.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,12 +43,12 @@ public class RetryTransactionalPollingIntegrationTests implements ApplicationCon bus = (Lifecycle) applicationContext; } - private static volatile int count = 0; + private static AtomicInteger count = new AtomicInteger(0); @Before public void clearLists() { list.clear(); - count = 0; + count.set(0); } public String input() { @@ -61,7 +62,7 @@ public class RetryTransactionalPollingIntegrationTests implements ApplicationCon } public void output(String message) { - count++; + count.incrementAndGet(); logger.debug("Handled: " + message); } diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/SimpleService.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/SimpleService.java index 22d75adf3..028547355 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/SimpleService.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/retry/SimpleService.java @@ -3,6 +3,7 @@ package org.springframework.batch.integration.retry; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -18,7 +19,7 @@ public class SimpleService implements Service { private List expected = new ArrayList(); - private int count = 0; + private AtomicInteger count = new AtomicInteger(0); public void setExpected(List expected) { this.expected = expected; @@ -34,9 +35,9 @@ public class SimpleService implements Service { @ServiceActivator(inputChannel = "requests", outputChannel = "replies") public String process(String message) { - String result = message + ": " + (count++); + String result = message + ": " + count.incrementAndGet(); logger.debug("Handling: " + message); - if (count <= expected.size()) { + if (count.get() <= expected.size()) { processed.add(message); } if ("fail".equals(message)) {