Skip to content

Commit 00aab3e

Browse files
davidthextonDave Syer
authored and
Dave Syer
committed
Add tests and implement fix for very quick running (<1ms) jobs (incorporating suggestions made)
1 parent bea966c commit 00aab3e

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
6868
+ " from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ? order by JOB_EXECUTION_ID desc";
6969

7070
private static final String GET_LAST_EXECUTION = "SELECT JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, CREATE_TIME, LAST_UPDATED, VERSION "
71-
+ "from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ? and CREATE_TIME = (SELECT max(CREATE_TIME) from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ?)";
71+
+ "from %PREFIX%JOB_EXECUTION E where JOB_INSTANCE_ID = ? and JOB_EXECUTION_ID = (SELECT max(JOB_EXECUTION_ID) from %PREFIX%JOB_EXECUTION E2 where E.JOB_INSTANCE_ID = E2.JOB_INSTANCE_ID)";
7272

7373
private static final String GET_EXECUTION_BY_ID = "SELECT JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, CREATE_TIME, LAST_UPDATED, VERSION"
7474
+ " from %PREFIX%JOB_EXECUTION where JOB_EXECUTION_ID = ?";
@@ -221,7 +221,7 @@ public JobExecution getLastJobExecution(JobInstance jobInstance) {
221221
Long id = jobInstance.getId();
222222

223223
List<JobExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_EXECUTION),
224-
new JobExecutionRowMapper(jobInstance), id, id);
224+
new JobExecutionRowMapper(jobInstance), id);
225225

226226
Assert.state(executions.size() <= 1, "There must be at most one latest job execution");
227227

spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.batch.test;
1818

19-
import java.util.Date;
2019
import java.util.HashMap;
2120
import java.util.Map;
2221

@@ -67,6 +66,8 @@
6766
*/
6867
public class JobLauncherTestUtils {
6968

69+
private static final long JOB_PARAMETER_MAXIMUM = 1000000;
70+
7071
/** Logger */
7172
protected final Log logger = LogFactory.getLog(getClass());
7273

@@ -157,7 +158,7 @@ public JobExecution launchJob(JobParameters jobParameters) throws Exception {
157158
*/
158159
public JobParameters getUniqueJobParameters() {
159160
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
160-
parameters.put("timestamp", new JobParameter(new Date().getTime()));
161+
parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM)));
161162
return new JobParameters(parameters);
162163
}
163164

spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.beans.factory.annotation.Qualifier;
1414
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
15+
import org.springframework.test.annotation.Repeat;
1516
import org.springframework.test.context.ContextConfiguration;
1617

1718
/**
@@ -74,6 +75,13 @@ public void testStep2Execution() {
7475
}
7576

7677
@Test
78+
@Repeat(10)
79+
public void testStep3Execution() throws Exception {
80+
// logging only, may complete in < 1ms (repeat so that it's likely to for at least one of those times)
81+
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step3").getStatus());
82+
}
83+
84+
@Test
7785
public void testStepLaunchJobContextEntry() {
7886
ExecutionContext jobContext = new ExecutionContext();
7987
jobContext.put("key1", "value1");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.springframework.batch.test.sample;
2+
3+
import org.apache.commons.logging.Log;
4+
import org.apache.commons.logging.LogFactory;
5+
import org.springframework.batch.core.StepContribution;
6+
import org.springframework.batch.core.scope.context.ChunkContext;
7+
import org.springframework.batch.core.step.tasklet.Tasklet;
8+
import org.springframework.batch.repeat.RepeatStatus;
9+
10+
public class LoggingTasklet implements Tasklet {
11+
12+
protected static final Log logger = LogFactory.getLog(LoggingTasklet.class);
13+
14+
private int id = 0;
15+
16+
public LoggingTasklet(int id) {
17+
this.id = id;
18+
}
19+
20+
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
21+
logger.info("tasklet executing: id=" + id);
22+
return RepeatStatus.FINISHED;
23+
}
24+
}

spring-batch-test/src/test/resources/jobs/sample-steps.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@
3333
<constructor-arg value="2" />
3434
</bean>
3535

36+
<batch:step id="s3" parent="taskletStep">
37+
<batch:tasklet ref="tasklet3">
38+
<batch:listeners>
39+
<batch:listener ref="tasklet3"/>
40+
</batch:listeners>
41+
</batch:tasklet>
42+
</batch:step>
43+
44+
<bean id="tasklet3" class="org.springframework.batch.test.sample.LoggingTasklet">
45+
<constructor-arg value="3" />
46+
</bean>
47+
3648
</beans>

spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
<job id="sampleFlowJob">
1818
<step id="step1" parent="s1" next="step2"/>
19-
<step id="step2" parent="s2"/>
19+
<step id="step2" parent="s2" next="step3"/>
20+
<step id="step3" parent="s3"/>
2021
</job>
2122

2223
</beans:beans>

spring-batch-test/src/test/resources/jobs/sampleSimpleJob.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<list>
1515
<bean id="step1" parent="s1"/>
1616
<bean id="step2" parent="s2"/>
17+
<bean id="step3" parent="s3"/>
1718
</list>
1819
</property>
1920
</bean>

0 commit comments

Comments
 (0)