|
12 | 12 | import com.github.chrisgleissner.springbatchrest.util.core.config.AdHocBatchConfig; |
13 | 13 |
|
14 | 14 | import java.util.HashMap; |
15 | | -import java.util.HashSet; |
16 | 15 | import java.util.Set; |
| 16 | +import java.util.concurrent.ConcurrentSkipListSet; |
17 | 17 | import java.util.concurrent.CountDownLatch; |
18 | 18 |
|
19 | 19 | import static com.github.chrisgleissner.springbatchrest.util.core.property.JobPropertyResolvers.JobProperties; |
20 | 20 | import static java.util.concurrent.TimeUnit.SECONDS; |
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 22 |
|
23 | | -@Slf4j |
24 | 23 | @RunWith(SpringRunner.class) |
25 | 24 | @ContextConfiguration(classes = AdHocBatchConfig.class) |
26 | 25 | @TestPropertySource(properties = "foo=bar") |
| 26 | +@Slf4j |
27 | 27 | public class AdHocStarterTest { |
| 28 | + private static final int NUMBER_OF_ITERATIONS = 3; |
| 29 | + private static final int NUMBER_OF_JOBS_PER_ITERATION = 2; |
28 | 30 |
|
29 | | - @Autowired |
30 | | - private AdHocStarter starter; |
| 31 | + @Autowired private AdHocStarter starter; |
| 32 | + @Autowired private JobBuilder jobBuilder; |
31 | 33 |
|
32 | | - @Autowired |
33 | | - private JobBuilder jobBuilder; |
34 | | - |
35 | | - private static final int numberOfIterations = 2; |
36 | | - private static final int numberOfJobsPerIteration = 2; |
37 | | - |
38 | 34 | @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 | + }); |
68 | 46 | } |
69 | 47 |
|
70 | 48 | @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 { |
72 | 64 | // Check that asynchronous execution with property overrides works |
73 | | - Set<String> propertyValues = new HashSet<>(); |
| 65 | + final Set<String> propertyValues = new ConcurrentSkipListSet<>(); |
74 | 66 | 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); |
88 | 72 | } |
89 | 73 | assertThat(latch.await(3, SECONDS)).isTrue(); |
90 | | - assertThat(propertyValues).hasSize(propertyValue); // TODO: Intermittent failures here - presumed timing issue |
| 74 | + assertThat(propertyValues).hasSize(propertyValue); |
91 | 75 | } |
92 | 76 | 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"); |
94 | 78 |
|
95 | 79 | // Check that synchronous execution without overrides works |
96 | 80 | starter.start(JobConfig.builder() |
97 | | - .name("AdHocStarterTest0") |
| 81 | + .name("AdHocStarterTest-0-0") |
98 | 82 | .asynchronous(false) |
99 | 83 | .build()); |
100 | 84 | assertThat(propertyValues).contains("bar"); |
101 | 85 | } |
| 86 | + |
| 87 | + private interface JobStarter { |
| 88 | + void startJob(String jobName, int propertyValue, Set<String> propertyValues, CountDownLatch latch); |
| 89 | + } |
102 | 90 | } |
0 commit comments