Skip to content

Commit 22b0eeb

Browse files
Fixed sporadic test errors. Fixed JavaDoc error.
1 parent b44d92c commit 22b0eeb

File tree

2 files changed

+51
-61
lines changed

2 files changed

+51
-61
lines changed

util/src/main/java/com/github/chrisgleissner/springbatchrest/util/core/AdHocStarter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.github.chrisgleissner.springbatchrest.util.core;
22

33
import com.github.chrisgleissner.springbatchrest.util.JobParamUtil;
4-
import com.github.chrisgleissner.springbatchrest.util.core.JobConfig.JobConfigBuilder;
54
import com.github.chrisgleissner.springbatchrest.util.core.property.JobPropertyResolvers;
65
import lombok.extern.slf4j.Slf4j;
7-
import org.springframework.batch.core.*;
6+
import org.springframework.batch.core.Job;
7+
import org.springframework.batch.core.JobExecution;
8+
import org.springframework.batch.core.JobExecutionException;
9+
import org.springframework.batch.core.JobParameter;
10+
import org.springframework.batch.core.JobParameters;
811
import org.springframework.batch.core.configuration.JobLocator;
912
import org.springframework.batch.core.configuration.JobRegistry;
1013
import org.springframework.batch.core.launch.JobLauncher;
@@ -65,8 +68,7 @@ public JobExecution start(Job job, Boolean async, Map<String, Object> properties
6568
} catch (NoSuchJobException e) {
6669
log.info("Registering new job: " + job.getName());
6770
}
68-
JobConfigBuilder builder = new JobConfigBuilder();
69-
JobConfig jobConfig = builder
71+
JobConfig jobConfig = JobConfig.builder()
7072
.asynchronous(async)
7173
.properties(properties == null ? new HashMap<>() : properties)
7274
.name(job.getName()).build();

util/src/test/java/com/github/chrisgleissner/springbatchrest/util/core/AdHocStarterTest.java

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,79 @@
1212
import com.github.chrisgleissner.springbatchrest.util.core.config.AdHocBatchConfig;
1313

1414
import java.util.HashMap;
15-
import java.util.HashSet;
1615
import java.util.Set;
16+
import java.util.concurrent.ConcurrentSkipListSet;
1717
import java.util.concurrent.CountDownLatch;
1818

1919
import static com.github.chrisgleissner.springbatchrest.util.core.property.JobPropertyResolvers.JobProperties;
2020
import static java.util.concurrent.TimeUnit.SECONDS;
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23-
@Slf4j
2423
@RunWith(SpringRunner.class)
2524
@ContextConfiguration(classes = AdHocBatchConfig.class)
2625
@TestPropertySource(properties = "foo=bar")
26+
@Slf4j
2727
public class AdHocStarterTest {
28+
private static final int NUMBER_OF_ITERATIONS = 3;
29+
private static final int NUMBER_OF_JOBS_PER_ITERATION = 2;
2830

29-
@Autowired
30-
private AdHocStarter starter;
31+
@Autowired private AdHocStarter starter;
32+
@Autowired private JobBuilder jobBuilder;
3133

32-
@Autowired
33-
private JobBuilder jobBuilder;
34-
35-
private static final int numberOfIterations = 2;
36-
private static final int numberOfJobsPerIteration = 2;
37-
3834
@Test
39-
public void startWorks() throws InterruptedException {
40-
// Check that asynchronous execution with property overrides works
41-
Set<String> propertyValues = new HashSet<>();
42-
int propertyValue = 0;
43-
for (int i = 0; i < numberOfIterations; i++) {
44-
CountDownLatch latch = new CountDownLatch(numberOfJobsPerIteration);
45-
for (int j = 0; j < numberOfJobsPerIteration; j++) {
46-
String jobName = "AdHocStarterTest" + j;
47-
Job jobToRun = jobBuilder.createJob(jobName, () -> {
48-
log.info("Running " + jobName);
49-
propertyValues.add(JobProperties.of(jobName).getProperty("foo"));
50-
latch.countDown();
51-
});
52-
HashMap<String, Object> propMap = new HashMap<String, Object>();
53-
propMap.put("foo", propertyValue++);
54-
starter.start(jobToRun, true, propMap);
55-
}
56-
assertThat(latch.await(3, SECONDS)).isTrue();
57-
assertThat(propertyValues).hasSize(propertyValue); // TODO: Intermittent failures here - presumed timing issue
58-
}
59-
Thread.sleep(100); // Job completion takes place after latch is counted down
60-
assertThat(JobProperties.of("AdHocStarterTest0").getProperty("foo")).isEqualTo("bar");
61-
62-
// Check that synchronous execution without overrides works
63-
starter.start(JobConfig.builder()
64-
.name("AdHocStarterTest0")
65-
.asynchronous(false)
66-
.build());
67-
assertThat(propertyValues).contains("bar");
35+
public void startJobWithPropertyMap() throws InterruptedException {
36+
assertThatConcurrentlyStartedJobsCanHaveDifferentProperties((jobName, propertyValue, propertyValues, latch) -> {
37+
final Job jobToRun = jobBuilder.createJob(jobName, () -> {
38+
log.info("Running " + jobName);
39+
propertyValues.add(JobProperties.of(jobName).getProperty("foo"));
40+
latch.countDown();
41+
});
42+
final HashMap<String, Object> propMap = new HashMap<String, Object>();
43+
propMap.put("foo", propertyValue);
44+
starter.start(jobToRun, true, propMap);
45+
});
6846
}
6947

7048
@Test
71-
public void startJobConfigWorks() throws InterruptedException {
49+
public void startJobWithJobConfig() throws InterruptedException {
50+
assertThatConcurrentlyStartedJobsCanHaveDifferentProperties((jobName, propertyValue, propertyValues, latch) -> {
51+
jobBuilder.createJob(jobName, () -> {
52+
log.info("Running " + jobName);
53+
propertyValues.add(JobProperties.of(jobName).getProperty("foo"));
54+
latch.countDown();
55+
});
56+
starter.start(JobConfig.builder()
57+
.name(jobName)
58+
.property("foo", "" + propertyValue)
59+
.asynchronous(true).build());
60+
});
61+
}
62+
63+
private void assertThatConcurrentlyStartedJobsCanHaveDifferentProperties(JobStarter jobStarter) throws InterruptedException {
7264
// Check that asynchronous execution with property overrides works
73-
Set<String> propertyValues = new HashSet<>();
65+
final Set<String> propertyValues = new ConcurrentSkipListSet<>();
7466
int propertyValue = 0;
75-
for (int i = 0; i < numberOfIterations; i++) {
76-
CountDownLatch latch = new CountDownLatch(numberOfJobsPerIteration);
77-
for (int j = 0; j < numberOfJobsPerIteration; j++) {
78-
String jobName = "AdHocStarterTest" + j;
79-
jobBuilder.createJob(jobName, () -> {
80-
log.info("Running " + jobName);
81-
propertyValues.add(JobProperties.of(jobName).getProperty("foo"));
82-
latch.countDown();
83-
});
84-
starter.start(JobConfig.builder()
85-
.name(jobName)
86-
.property("foo", "" + propertyValue++)
87-
.asynchronous(true).build());
67+
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
68+
final CountDownLatch latch = new CountDownLatch(NUMBER_OF_JOBS_PER_ITERATION);
69+
for (int j = 0; j < NUMBER_OF_JOBS_PER_ITERATION; j++) {
70+
final String jobName = String.format("AdHocStarterTest-%s-%s", i, j);
71+
jobStarter.startJob(jobName, propertyValue++, propertyValues, latch);
8872
}
8973
assertThat(latch.await(3, SECONDS)).isTrue();
90-
assertThat(propertyValues).hasSize(propertyValue); // TODO: Intermittent failures here - presumed timing issue
74+
assertThat(propertyValues).hasSize(propertyValue);
9175
}
9276
Thread.sleep(100); // Job completion takes place after latch is counted down
93-
assertThat(JobProperties.of("AdHocStarterTest0").getProperty("foo")).isEqualTo("bar");
77+
assertThat(JobProperties.of("AdHocStarterTest-0-0").getProperty("foo")).isEqualTo("bar");
9478

9579
// Check that synchronous execution without overrides works
9680
starter.start(JobConfig.builder()
97-
.name("AdHocStarterTest0")
81+
.name("AdHocStarterTest-0-0")
9882
.asynchronous(false)
9983
.build());
10084
assertThat(propertyValues).contains("bar");
10185
}
86+
87+
private interface JobStarter {
88+
void startJob(String jobName, int propertyValue, Set<String> propertyValues, CountDownLatch latch);
89+
}
10290
}

0 commit comments

Comments
 (0)