@Test public void testInterruption() throws Exception { String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 5"; tasklet.setCommand(command); tasklet.setTerminationCheckInterval(10); tasklet.afterPropertiesSet(); stepExecution.setTerminateOnly(); try { tasklet.execute(null, null); fail(); } catch (JobInterruptedException e) { System.out.println(e.getMessage()); assertTrue(e.getMessage().contains("Job interrupted while executing system command")); assertTrue(e.getMessage().contains(command)); } }
private void initializeTasklet() { tasklet = new SystemCommandTasklet(); tasklet.setEnvironmentParams(null); // inherit from parent process tasklet.setWorkingDirectory(null); // inherit from parent process tasklet.setSystemProcessExitCodeMapper(new SimpleSystemProcessExitCodeMapper()); tasklet.setTimeout(5000); // long enough timeout tasklet.setTerminationCheckInterval(500); tasklet.setCommand("invalid command, change value for successful execution"); tasklet.setInterruptOnCancel(true); tasklet.setTaskExecutor(new SimpleAsyncTaskExecutor()); }
@Test public void testStopped() throws Exception { initializeTasklet(); tasklet.setJobExplorer(jobExplorer); tasklet.afterPropertiesSet(); tasklet.beforeStep(stepExecution); JobExecution stoppedJobExecution = new JobExecution(stepExecution.getJobExecution()); stoppedJobExecution.setStatus(BatchStatus.STOPPING); when(jobExplorer.getJobExecution(1L)).thenReturn(stepExecution.getJobExecution(), stepExecution.getJobExecution(), stoppedJobExecution); String command = isRunningOnWindows() ? "ping 127.0.0.1 -n 5" : "sleep 15"; tasklet.setCommand(command); tasklet.setTerminationCheckInterval(10); tasklet.afterPropertiesSet(); StepContribution contribution = stepExecution.createStepContribution(); StepContext stepContext = new StepContext(stepExecution); ChunkContext chunkContext = new ChunkContext(stepContext); tasklet.execute(contribution, chunkContext); assertEquals(ExitStatus.STOPPED.getExitCode(), contribution.getExitStatus().getExitCode()); }