Add unit test for BATCH-1980
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package org.springframework.batch.core.test.timeout;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
public class LoggingItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
protected Log logger = LogFactory.getLog(LoggingItemWriter.class);
|
||||
|
||||
@Override
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
logger.info(items);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.springframework.batch.core.test.timeout;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
|
||||
public class SleepingItemProcessor<I> implements ItemProcessor<I, I> {
|
||||
|
||||
private long millisToSleep;
|
||||
|
||||
@Override
|
||||
public I process(I item) throws Exception {
|
||||
Thread.sleep(millisToSleep);
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setMillisToSleep(long millisToSleep) {
|
||||
this.millisToSleep = millisToSleep;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.springframework.batch.core.test.timeout;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
|
||||
public class SleepingTasklet implements Tasklet {
|
||||
|
||||
private long millisToSleep;
|
||||
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution,
|
||||
ChunkContext chunkContext) throws Exception {
|
||||
Thread.sleep(millisToSleep);
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
public void setMillisToSleep(long millisToSleep) {
|
||||
this.millisToSleep = millisToSleep;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
|
||||
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<job id="chunkTimeoutJob" xmlns="http://www.springframework.org/schema/batch">
|
||||
<step id="chunk-step">
|
||||
<tasklet>
|
||||
<chunk reader="dummyReader" processor="sleepingProcessor" writer="loggingWriter"
|
||||
commit-interval="5" />
|
||||
<transaction-attributes timeout="1"/>
|
||||
</tasklet>
|
||||
</step>
|
||||
</job>
|
||||
|
||||
<job id="taskletTimeoutJob" xmlns="http://www.springframework.org/schema/batch">
|
||||
<step id="tasklet-step">
|
||||
<tasklet ref="sleepingTasklet">
|
||||
<transaction-attributes timeout="1"/>
|
||||
</tasklet>
|
||||
</step>
|
||||
</job>
|
||||
|
||||
<bean id="dummyReader" class="org.springframework.batch.item.support.ListItemReader">
|
||||
<constructor-arg>
|
||||
<list>
|
||||
<value>1</value>
|
||||
<value>2</value>
|
||||
<value>3</value>
|
||||
<value>4</value>
|
||||
<value>5</value>
|
||||
<value>6</value>
|
||||
<value>7</value>
|
||||
<value>8</value>
|
||||
<value>9</value>
|
||||
<value>10</value>
|
||||
</list>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="sleepingProcessor" class="org.springframework.batch.core.test.timeout.SleepingItemProcessor">
|
||||
<property name="millisToSleep" value="500" />
|
||||
</bean>
|
||||
|
||||
<bean id="loggingWriter" class="org.springframework.batch.core.test.timeout.LoggingItemWriter" />
|
||||
|
||||
<bean id="sleepingTasklet" class="org.springframework.batch.core.test.timeout.SleepingTasklet">
|
||||
<property name="millisToSleep" value="2000" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.springframework.batch.core.test.timeout;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/timeoutJob.xml" })
|
||||
public class TimeoutJobIntegrationTests {
|
||||
|
||||
/** Logger */
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@Autowired
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("chunkTimeoutJob")
|
||||
private Job chunkTimeoutJob;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("taskletTimeoutJob")
|
||||
private Job taskletTimeoutJob;
|
||||
|
||||
@Test
|
||||
public void testChunkTimeoutShouldFail() throws Exception {
|
||||
JobExecution execution = jobLauncher.run(chunkTimeoutJob, new JobParametersBuilder().addLong("id", System.currentTimeMillis())
|
||||
.toJobParameters());
|
||||
assertEquals(BatchStatus.FAILED, execution.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTaskletTimeoutShouldFail() throws Exception {
|
||||
JobExecution execution = jobLauncher.run(taskletTimeoutJob, new JobParametersBuilder().addLong("id", System.currentTimeMillis())
|
||||
.toJobParameters());
|
||||
assertEquals(BatchStatus.FAILED, execution.getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user