Skip to content

Commit eba7265

Browse files
committed
Fix tests failing randomly
Tests modified in this change set fail randomly due to shared mutable state between multiple threads. This commit ensures that the shared state is correctly synchronised between threads or re-initialized before each test run.
1 parent c9f72cd commit eba7265

File tree

2 files changed

+367
-361
lines changed

2 files changed

+367
-361
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/partition/JsrPartitionHandlerTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2018 the original author or authors.
2+
* Copyright 2013-2019 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.
@@ -45,6 +45,7 @@
4545
import java.util.Properties;
4646
import java.util.Queue;
4747
import java.util.concurrent.ConcurrentLinkedQueue;
48+
import java.util.concurrent.atomic.AtomicInteger;
4849

4950
import static org.junit.Assert.assertEquals;
5051
import static org.junit.Assert.assertTrue;
@@ -55,7 +56,7 @@ public class JsrPartitionHandlerTests extends AbstractJsrTestCase {
5556
private JsrPartitionHandler handler;
5657
private JobRepository repository = new JobRepositorySupport();
5758
private StepExecution stepExecution;
58-
private int count;
59+
private AtomicInteger count;
5960
private BatchPropertyContext propertyContext;
6061
private JsrStepExecutionSplitter stepSplitter;
6162

@@ -67,12 +68,12 @@ public void setUp() throws Exception {
6768
stepSplitter = new JsrStepExecutionSplitter(repository, false, "step1", true);
6869
Analyzer.collectorData = "";
6970
Analyzer.status = "";
70-
count = 0;
71+
count = new AtomicInteger(0);
7172
handler = new JsrPartitionHandler();
7273
handler.setStep(new StepSupport() {
7374
@Override
7475
public void execute(StepExecution stepExecution) throws JobInterruptedException {
75-
count++;
76+
count.incrementAndGet();
7677
stepExecution.setStatus(org.springframework.batch.core.BatchStatus.COMPLETED);
7778
stepExecution.setExitStatus(new ExitStatus("done"));
7879
}
@@ -135,7 +136,7 @@ public void testHardcodedNumberOfPartitions() throws Exception {
135136
Collection<StepExecution> executions = handler.handle(stepSplitter, stepExecution);
136137

137138
assertEquals(3, executions.size());
138-
assertEquals(3, count);
139+
assertEquals(3, count.get());
139140
}
140141

141142
@Test
@@ -151,7 +152,7 @@ public void testPollingPartitionsCompletion() throws Exception {
151152
stopWatch.stop();
152153

153154
assertEquals(3, executions.size());
154-
assertEquals(3, count);
155+
assertEquals(3, count.get());
155156
assertTrue(stopWatch.getLastTaskTimeMillis() >= 1000);
156157
}
157158

@@ -173,7 +174,7 @@ public PartitionPlan mapPartitions() throws Exception {
173174
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);
174175

175176
assertEquals(3, executions.size());
176-
assertEquals(3, count);
177+
assertEquals(3, count.get());
177178
}
178179

179180
@Test
@@ -194,7 +195,7 @@ public PartitionPlan mapPartitions() throws Exception {
194195
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);
195196

196197
assertEquals(3, executions.size());
197-
assertEquals(3, count);
198+
assertEquals(3, count.get());
198199
}
199200

200201
@Test
@@ -221,7 +222,7 @@ public PartitionPlan mapPartitions() throws Exception {
221222
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);
222223

223224
assertEquals(3, executions.size());
224-
assertEquals(3, count);
225+
assertEquals(3, count.get());
225226
assertEquals("value1", propertyContext.getStepProperties("step1:partition0").get("key1"));
226227
assertEquals("value2", propertyContext.getStepProperties("step1:partition1").get("key1"));
227228
}
@@ -241,7 +242,7 @@ public void testAnalyzer() throws Exception {
241242
Collection<StepExecution> executions = handler.handle(new JsrStepExecutionSplitter(repository, false, "step1", true), stepExecution);
242243

243244
assertEquals(2, executions.size());
244-
assertEquals(2, count);
245+
assertEquals(2, count.get());
245246
assertEquals("foobar", Analyzer.collectorData);
246247
assertEquals("COMPLETEDdone", Analyzer.status);
247248
}

0 commit comments

Comments
 (0)