@Test public void testTimeoutNotSet() throws Exception { tasklet.setCommand("not-empty placeholder"); tasklet.setTimeout(0); try { tasklet.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } }
@Test public void testExecuteTimeout() throws Exception { String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 3"; tasklet.setCommand(command); tasklet.setTimeout(10); tasklet.afterPropertiesSet(); log.info("Executing command: " + command); try { tasklet.execute(null, null); fail(); } catch (SystemCommandException e) { assertTrue(e.getMessage().contains("did not finish within the timeout")); } }
@Test public void testExecuteFailure() throws Exception { String command = getJavaCommand() + " org.springframework.batch.sample.tasklet.UnknownClass"; tasklet.setCommand(command); tasklet.setTimeout(200L); tasklet.afterPropertiesSet(); log.info("Executing command: " + command); try { StepContribution contribution = stepExecution.createStepContribution(); RepeatStatus exitStatus = tasklet.execute(contribution, null); assertEquals(RepeatStatus.FINISHED, exitStatus); assertEquals(ExitStatus.FAILED, contribution.getExitStatus()); } catch (RuntimeException e) { // on some platforms the system call does not return assertEquals("Execution of system command did not finish within the timeout", e.getMessage()); } }
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()); }