Skip to content

Commit b96ac3c

Browse files
committed
Fix PartitionParserTests
Some tests in PartitionParserTests were failing intermittently due to the usage of non-synchronized shared state between concurrent threads. This commit updates the test code to use `AtomicInteger` instead of `int` for the state shared between concurrent threads. (cherry picked from commit 98fba4a)
1 parent af3eff3 commit b96ac3c

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/PartitionParserTests.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.Properties;
2424
import java.util.Set;
2525
import java.util.Vector;
26+
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.regex.Matcher;
2728
import java.util.regex.Pattern;
2829
import javax.batch.api.BatchProperty;
@@ -53,7 +54,7 @@ public class PartitionParserTests extends AbstractJsrTestCase {
5354

5455
@Before
5556
public void before() {
56-
MyBatchlet.processed = 0;
57+
MyBatchlet.processed = new AtomicInteger(0);
5758
MyBatchlet.threadNames = Collections.synchronizedSet(new HashSet<>());
5859
MyBatchlet.artifactNames = Collections.synchronizedSet(new HashSet<>());
5960
PartitionCollector.artifactNames = Collections.synchronizedSet(new HashSet<>());
@@ -64,7 +65,7 @@ public void testBatchletNoProperties() throws Exception {
6465
BatchStatus curBatchStatus = runJob("partitionParserTestsBatchlet", new Properties(), TIMEOUT).getBatchStatus();
6566

6667
assertEquals(BatchStatus.COMPLETED, curBatchStatus);
67-
assertEquals(10, MyBatchlet.processed);
68+
assertEquals(10, MyBatchlet.processed.get());
6869
assertEquals(10, MyBatchlet.threadNames.size());
6970
}
7071

@@ -88,7 +89,7 @@ public void testFullPartitionConfiguration() throws Exception {
8889
assertTrue(execution.getExitStatus().endsWith("BPSC_APSC"));
8990
assertEquals(3, countMatches(execution.getExitStatus(), caPattern));
9091
assertEquals(3, countMatches(execution.getExitStatus(), asPattern));
91-
assertEquals(3, MyBatchlet.processed);
92+
assertEquals(3, MyBatchlet.processed.get());
9293
assertEquals(3, MyBatchlet.threadNames.size());
9394
}
9495

@@ -101,7 +102,7 @@ public void testFullPartitionConfigurationWithProperties() throws Exception {
101102
assertTrue(execution.getExitStatus().endsWith("BPSC_APSC"));
102103
assertEquals(3, countMatches(execution.getExitStatus(), caPattern));
103104
assertEquals(3, countMatches(execution.getExitStatus(), asPattern));
104-
assertEquals(3, MyBatchlet.processed);
105+
assertEquals(3, MyBatchlet.processed.get());
105106
assertEquals(3, MyBatchlet.threadNames.size());
106107
assertEquals(MyBatchlet.artifactNames.iterator().next(), "batchlet");
107108
assertEquals(PartitionMapper.name, "mapper");
@@ -120,7 +121,7 @@ public void testFullPartitionConfigurationWithMapperSuppliedProperties() throws
120121
assertTrue(execution.getExitStatus().endsWith("BPSC_APSC"));
121122
assertEquals(3, countMatches(execution.getExitStatus(), caPattern));
122123
assertEquals(3, countMatches(execution.getExitStatus(), asPattern));
123-
assertEquals(3, MyBatchlet.processed);
124+
assertEquals(3, MyBatchlet.processed.get());
124125
assertEquals(3, MyBatchlet.threadNames.size());
125126

126127
assertEquals(MyBatchlet.artifactNames.size(), 3);
@@ -145,7 +146,7 @@ public void testFullPartitionConfigurationWithHardcodedProperties() throws Excep
145146
assertTrue(execution.getExitStatus().endsWith("BPSC_APSC"));
146147
assertEquals(3, countMatches(execution.getExitStatus(), caPattern));
147148
assertEquals(3, countMatches(execution.getExitStatus(), asPattern));
148-
assertEquals(3, MyBatchlet.processed);
149+
assertEquals(3, MyBatchlet.processed.get());
149150
assertEquals(3, MyBatchlet.threadNames.size());
150151

151152
assertEquals(MyBatchlet.artifactNames.size(), 3);
@@ -292,7 +293,7 @@ public PartitionPlan mapPartitions() throws Exception {
292293

293294
public static class MyBatchlet implements Batchlet {
294295

295-
protected static int processed = 0;
296+
protected static AtomicInteger processed = new AtomicInteger(0);;
296297
protected static Set<String> threadNames = Collections.synchronizedSet(new HashSet<>());
297298
protected static Set<String> artifactNames = Collections.synchronizedSet(new HashSet<>());
298299

@@ -310,7 +311,7 @@ public static class MyBatchlet implements Batchlet {
310311
public String process() throws Exception {
311312
artifactNames.add(artifactName);
312313
threadNames.add(Thread.currentThread().getName());
313-
processed++;
314+
processed.incrementAndGet();
314315

315316
stepContext.setExitStatus("bad step exit status");
316317
jobContext.setExitStatus("bad job exit status");

0 commit comments

Comments
 (0)