@Test public void testSimulatedRestart() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,2,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); // Set up context with two messages (chunks) in the backlog stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.EXPECTED, 6); stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.ACTUAL, 4); // And make the back log real requests.send(getSimpleMessage("foo", stepExecution.getJobExecution().getJobId())); requests.send(getSimpleMessage("bar", stepExecution.getJobExecution().getJobId())); step.execute(stepExecution); waitForResults(8, 10); assertEquals(8, TestItemWriter.count); assertEquals(6, stepExecution.getReadCount()); }
@Test public void testVanillaIteration() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,2,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); step.execute(stepExecution); waitForResults(6, 10); assertEquals(6, TestItemWriter.count); assertEquals(6, stepExecution.getReadCount()); }
@Test public void testSimulatedRestartWithNoBacklog() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,2,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); // Set up expectation of three messages (chunks) in the backlog stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.EXPECTED, 6); stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.ACTUAL, 3); writer.setMaxWaitTimeouts(2); /* * With no backlog we process all the items, but the listener can't * reconcile the expected number of items with the actual. An infinite * loop would be bad, so the best we can do is fail as fast as possible. */ step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); String message = stepExecution.getExitStatus().getExitDescription(); assertTrue("Message did not contain 'timed out': " + message, message.toLowerCase().contains("timed out")); assertEquals(0, TestItemWriter.count); assertEquals(0, stepExecution.getReadCount()); }
/** * This one is flakey - we try to force it to wait until after the step to * finish processing just by waiting for long enough. */ @Test public void testFailureInStepListener() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("wait,fail,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); step.execute(stepExecution); waitForResults(2, 10); // The number of items processed is actually between 1 and 6, because // the one that failed might have been processed out of order. assertTrue(1 <= TestItemWriter.count); assertTrue(6 >= TestItemWriter.count); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); String exitDescription = stepExecution.getExitStatus().getExitDescription(); assertTrue("Exit description does not contain exception type name: " + exitDescription, exitDescription .contains(AsynchronousFailureException.class.getName())); }
@Test public void testSimulatedRestartWithBadMessagesFromAnotherJob() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,2,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); // Set up context with two messages (chunks) in the backlog stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.EXPECTED, 3); stepExecution.getExecutionContext().putInt(ChunkMessageChannelItemWriter.ACTUAL, 2); // Speed up the eventual failure writer.setMaxWaitTimeouts(2); // And make the back log real requests.send(getSimpleMessage("foo", 4321L)); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); String message = stepExecution.getExitStatus().getExitDescription(); assertTrue("Message does not contain 'wrong job': " + message, message.contains("wrong job")); waitForResults(1, 10); assertEquals(1, TestItemWriter.count); assertEquals(0, stepExecution.getReadCount()); }
@Test public void testEarlyCompletionSignalledInHandler() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,fail,3,4,5,6")))); factory.setCommitInterval(2); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); String message = stepExecution.getExitStatus().getExitDescription(); assertTrue("Message does not contain 'fail': " + message, message.contains("fail")); waitForResults(2, 10); // The number of items processed is actually between 1 and 6, because // the one that failed might have been processed out of order. assertTrue(1 <= TestItemWriter.count); assertTrue(6 >= TestItemWriter.count); // But it should fail the step in any case assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); }