Skip to content

Commit e459cc9

Browse files
committed
Made batch tests more robust against timeout and races
1 parent c871e33 commit e459cc9

File tree

30 files changed

+476
-180
lines changed

30 files changed

+476
-180
lines changed

batch/batch-listeners/src/test/java/org/javaee7/batch/batch/listeners/BatchListenersTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.util.Properties;
1818
import java.util.concurrent.TimeUnit;
1919

20+
import static javax.batch.runtime.BatchRuntime.getJobOperator;
21+
import static javax.batch.runtime.BatchStatus.COMPLETED;
22+
import static org.javaee7.util.BatchTestHelper.keepTestAlive;
2023
import static org.junit.Assert.assertEquals;
2124
import static org.junit.Assert.assertTrue;
2225

@@ -140,11 +143,23 @@ public static WebArchive createDeployment() {
140143
*/
141144
@Test
142145
public void testBatchListeners() throws Exception {
143-
JobOperator jobOperator = BatchRuntime.getJobOperator();
144-
Long executionId = jobOperator.start("myJob", new Properties());
145-
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
146-
147-
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
146+
147+
JobOperator jobOperator = null;
148+
Long executionId = null;
149+
JobExecution jobExecution = null;
150+
for (int i = 0; i<3; i++) {
151+
jobOperator = getJobOperator();
152+
executionId = jobOperator.start("myJob", new Properties());
153+
jobExecution = jobOperator.getJobExecution(executionId);
154+
155+
jobExecution = keepTestAlive(jobExecution);
156+
157+
if (COMPLETED.equals(jobExecution.getBatchStatus())) {
158+
break;
159+
}
160+
161+
System.out.println("Execution did not complete, trying again");
162+
}
148163

149164
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
150165
for (StepExecution stepExecution : stepExecutions) {

batch/batchlet-simple/src/test/java/org/javaee7/batch/batchlet/simple/MyBatchletTest.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package org.javaee7.batch.batchlet.simple;
22

3+
import static javax.batch.runtime.BatchStatus.COMPLETED;
4+
import static org.jboss.shrinkwrap.api.ArchivePaths.create;
5+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
6+
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.util.Properties;
10+
11+
import javax.batch.operations.JobOperator;
12+
import javax.batch.runtime.BatchRuntime;
13+
import javax.batch.runtime.JobExecution;
14+
315
import org.javaee7.util.BatchTestHelper;
416
import org.jboss.arquillian.container.test.api.Deployment;
517
import org.jboss.arquillian.junit.Arquillian;
6-
import org.jboss.shrinkwrap.api.ArchivePaths;
7-
import org.jboss.shrinkwrap.api.ShrinkWrap;
8-
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
918
import org.jboss.shrinkwrap.api.spec.WebArchive;
1019
import org.junit.Test;
1120
import org.junit.runner.RunWith;
1221

13-
import javax.batch.operations.JobOperator;
14-
import javax.batch.runtime.BatchRuntime;
15-
import javax.batch.runtime.BatchStatus;
16-
import javax.batch.runtime.JobExecution;
17-
import java.util.Properties;
18-
19-
import static org.junit.Assert.assertEquals;
20-
2122
/**
2223
* Batchlet is the simplest processing style available in the Batch specification. It's a task oriented step where the
2324
* task is invoked once, executes, and returns an exit status.
@@ -51,12 +52,21 @@ public class MyBatchletTest {
5152
*/
5253
@Deployment
5354
public static WebArchive createDeployment() {
54-
WebArchive war = ShrinkWrap.create(WebArchive.class)
55+
56+
System.out.println("************************************************************");
57+
WebArchive war = null;
58+
try {
59+
war = create(WebArchive.class)
5560
.addClass(BatchTestHelper.class)
5661
.addClass(MyBatchlet.class)
57-
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
62+
.addAsWebInfResource(INSTANCE, create("beans.xml"))
5863
.addAsResource("META-INF/batch-jobs/myJob.xml");
64+
5965
System.out.println(war.toString(true));
66+
} catch (Throwable e) {
67+
e.printStackTrace();
68+
}
69+
6070
return war;
6171
}
6272

@@ -69,13 +79,24 @@ public static WebArchive createDeployment() {
6979
*/
7080
@Test
7181
public void testBatchletProcess() throws Exception {
72-
JobOperator jobOperator = BatchRuntime.getJobOperator();
73-
Long executionId = jobOperator.start("myJob", new Properties());
74-
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
75-
76-
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
82+
83+
JobExecution jobExecution = null;
84+
85+
for (int i = 0; i<3; i++) {
86+
JobOperator jobOperator = BatchRuntime.getJobOperator();
87+
Long executionId = jobOperator.start("myJob", new Properties());
88+
jobExecution = jobOperator.getJobExecution(executionId);
89+
90+
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
91+
92+
if (COMPLETED.equals(jobExecution.getBatchStatus())) {
93+
break;
94+
}
95+
96+
System.out.println("Execution did not complete, trying again");
97+
}
7798

7899
// <1> Job should be completed.
79-
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
100+
assertEquals(jobExecution.getBatchStatus(), COMPLETED);
80101
}
81102
}

batch/chunk-checkpoint/src/main/java/org/javaee7/batch/chunk/checkpoint/MyCheckpointAlgorithm.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@
4040

4141
package org.javaee7.batch.chunk.checkpoint;
4242

43+
import java.util.concurrent.CountDownLatch;
44+
4345
import javax.batch.api.chunk.AbstractCheckpointAlgorithm;
4446
import javax.inject.Named;
45-
import java.util.concurrent.CountDownLatch;
4647

4748
/**
4849
* @author Arun Gupta
4950
*/
5051
@Named
5152
public class MyCheckpointAlgorithm extends AbstractCheckpointAlgorithm {
53+
5254
public static CountDownLatch checkpointCountDownLatch = new CountDownLatch(10);
5355

5456
@Override
5557
public boolean isReadyToCheckpoint() throws Exception {
5658
checkpointCountDownLatch.countDown();
59+
5760
return MyItemReader.COUNT % 5 == 0;
5861
}
5962
}

batch/chunk-checkpoint/src/main/java/org/javaee7/batch/chunk/checkpoint/MyItemReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public MyInputRecord readItem() {
6464
COUNT++;
6565
return new MyInputRecord(Integer.valueOf(tokens.nextToken()));
6666
}
67+
6768
return null;
6869
}
6970
}

batch/chunk-checkpoint/src/test/java/org/javaee7/batch/chunk/checkpoint/BatchChunkCheckpointTest.java

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
package org.javaee7.batch.chunk.checkpoint;
22

3+
import static java.util.concurrent.TimeUnit.SECONDS;
4+
import static javax.batch.runtime.BatchRuntime.getJobOperator;
5+
import static javax.batch.runtime.BatchStatus.COMPLETED;
6+
import static javax.batch.runtime.Metric.MetricType.COMMIT_COUNT;
7+
import static javax.batch.runtime.Metric.MetricType.READ_COUNT;
8+
import static javax.batch.runtime.Metric.MetricType.WRITE_COUNT;
9+
import static org.javaee7.batch.chunk.checkpoint.MyCheckpointAlgorithm.checkpointCountDownLatch;
10+
import static org.javaee7.util.BatchTestHelper.getMetricsMap;
11+
import static org.javaee7.util.BatchTestHelper.keepTestAlive;
12+
import static org.jboss.shrinkwrap.api.ArchivePaths.create;
13+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
14+
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import java.util.Map;
19+
import java.util.Properties;
20+
21+
import javax.batch.operations.JobOperator;
22+
import javax.batch.runtime.JobExecution;
23+
import javax.batch.runtime.Metric;
24+
import javax.batch.runtime.StepExecution;
25+
326
import org.javaee7.util.BatchTestHelper;
427
import org.jboss.arquillian.container.test.api.Deployment;
528
import org.jboss.arquillian.junit.Arquillian;
6-
import org.jboss.shrinkwrap.api.ArchivePaths;
7-
import org.jboss.shrinkwrap.api.ShrinkWrap;
8-
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
929
import org.jboss.shrinkwrap.api.spec.WebArchive;
1030
import org.junit.Test;
1131
import org.junit.runner.RunWith;
1232

13-
import javax.batch.operations.JobOperator;
14-
import javax.batch.runtime.*;
15-
import java.util.List;
16-
import java.util.Map;
17-
import java.util.Properties;
18-
import java.util.concurrent.TimeUnit;
19-
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
22-
2333
/**
2434
* The Batch specification provides a Chunk Oriented processing style. This style is defined by enclosing into a
2535
* transaction a set of reads, process and write operations via +javax.batch.api.chunk.ItemReader+,
@@ -47,6 +57,7 @@
4757
*/
4858
@RunWith(Arquillian.class)
4959
public class BatchChunkCheckpointTest {
60+
5061
/**
5162
* We're just going to deploy the application as a +web archive+. Note the inclusion of the following files:
5263
*
@@ -59,12 +70,14 @@ public class BatchChunkCheckpointTest {
5970
*/
6071
@Deployment
6172
public static WebArchive createDeployment() {
62-
WebArchive war = ShrinkWrap.create(WebArchive.class)
73+
WebArchive war = create(WebArchive.class)
6374
.addClass(BatchTestHelper.class)
6475
.addPackage("org.javaee7.batch.chunk.checkpoint")
65-
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
76+
.addAsWebInfResource(INSTANCE, create("beans.xml"))
6677
.addAsResource("META-INF/batch-jobs/myJob.xml");
67-
System.out.println(war.toString(true));
78+
79+
System.out.println("\nBatchChunkCheckpointTest test war content: \n" + war.toString(true) + "\n");
80+
6881
return war;
6982
}
7083

@@ -79,29 +92,43 @@ public static WebArchive createDeployment() {
7992
*/
8093
@Test
8194
public void testBatchChunkCheckpoint() throws Exception {
82-
JobOperator jobOperator = BatchRuntime.getJobOperator();
83-
Long executionId = jobOperator.start("myJob", new Properties());
84-
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
85-
86-
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
95+
96+
JobOperator jobOperator = null;
97+
Long executionId = null;
98+
JobExecution jobExecution = null;
99+
for (int i = 0; i<3; i++) {
100+
jobOperator = getJobOperator();
101+
executionId = jobOperator.start("myJob", new Properties());
102+
jobExecution = jobOperator.getJobExecution(executionId);
103+
104+
jobExecution = keepTestAlive(jobExecution);
105+
106+
if (COMPLETED.equals(jobExecution.getBatchStatus())) {
107+
break;
108+
}
109+
110+
System.out.println("Execution did not complete, trying again");
111+
}
87112

88-
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
89-
for (StepExecution stepExecution : stepExecutions) {
113+
for (StepExecution stepExecution : jobOperator.getStepExecutions(executionId)) {
90114
if (stepExecution.getStepName().equals("myStep")) {
91-
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
115+
Map<Metric.MetricType, Long> metricsMap = getMetricsMap(stepExecution.getMetrics());
92116

93117
// <1> The read count should be 10 elements. Check +MyItemReader+.
94-
assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
118+
assertEquals(10L, metricsMap.get(READ_COUNT).longValue());
119+
95120
// <2> The write count should be 5. Only half of the elements read are processed to be written.
96-
assertEquals(10L / 2L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
121+
assertEquals(10L / 2L, metricsMap.get(WRITE_COUNT).longValue());
122+
97123
// <3> The commit count should be 3. Checkpoint is on every 5th read, plus one final read-commit.
98-
assertEquals(10L / 5L + 1, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
124+
assertEquals(10L / 5L + 1, metricsMap.get(COMMIT_COUNT).longValue());
99125
}
100126
}
101127

102128
// <4> The checkpoint algorithm should be checked 10 times. One for each element read.
103-
assertTrue(MyCheckpointAlgorithm.checkpointCountDownLatch.await(0, TimeUnit.SECONDS));
129+
assertTrue(checkpointCountDownLatch.await(0, SECONDS));
130+
104131
// <5> Job should be completed.
105-
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
132+
assertEquals(jobExecution.getBatchStatus(), COMPLETED);
106133
}
107134
}

batch/chunk-csv-database/src/test/java/org/javaee7/batch/chunk/csv/database/BatchCSVDatabaseTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Map;
2020
import java.util.Properties;
2121

22+
import static javax.batch.runtime.BatchRuntime.getJobOperator;
23+
import static javax.batch.runtime.BatchStatus.COMPLETED;
24+
import static org.javaee7.util.BatchTestHelper.keepTestAlive;
2225
import static org.junit.Assert.assertEquals;
2326

2427
/**
@@ -93,11 +96,23 @@ public static WebArchive createDeployment() {
9396
@SuppressWarnings("unchecked")
9497
@Test
9598
public void testBatchCSVDatabase() throws Exception {
96-
JobOperator jobOperator = BatchRuntime.getJobOperator();
97-
Long executionId = jobOperator.start("myJob", new Properties());
98-
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
99-
100-
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
99+
100+
JobOperator jobOperator = null;
101+
Long executionId = null;
102+
JobExecution jobExecution = null;
103+
for (int i = 0; i<3; i++) {
104+
jobOperator = getJobOperator();
105+
executionId = jobOperator.start("myJob", new Properties());
106+
jobExecution = jobOperator.getJobExecution(executionId);
107+
108+
jobExecution = keepTestAlive(jobExecution);
109+
110+
if (COMPLETED.equals(jobExecution.getBatchStatus())) {
111+
break;
112+
}
113+
114+
System.out.println("Execution did not complete, trying again");
115+
}
101116

102117
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
103118
for (StepExecution stepExecution : stepExecutions) {

batch/chunk-exception/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
<groupId>org.javaee7</groupId>
77
<artifactId>batch</artifactId>
88
<version>1.0-SNAPSHOT</version>
9-
<relativePath>../pom.xml</relativePath>
109
</parent>
10+
1111
<artifactId>batch-chunk-exception</artifactId>
1212
<packaging>war</packaging>
13+
1314
<name>Java EE 7 Sample: batch - chunk-exception</name>
1415
<description>Chunk Exception Handling - Retrying and Skipping</description>
1516

batch/chunk-exception/src/main/resources/META-INF/batch-jobs/myJob.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<listener ref="myRetryProcessorListener"/>
5252
<listener ref="myRetryWriteListener"/>
5353
</listeners>
54+
5455
<!-- skip-limit and retry-limit should have values greater than 0 to perform the expected behaviour -->
5556
<chunk checkpoint-policy="item" item-count="3" skip-limit="3" retry-limit="3">
5657
<reader ref="myItemReader"/>

0 commit comments

Comments
 (0)