LimitTrialCountStrategy implements ITaskProcessStrategy { @Override public void process(final ITaskExecutionState state) throws TaskExecutionException, InvalidArgumentException, TaskProcessException { IShutdownAwareTask shutdownAwareTask = state.getTaskAs(IShutdownAwareTask.class); TrialCounter counter = (TrialCounter) shutdownAwareTask.getShutdownStatus(); if (null == counter) { counter = new TrialCounter(); shutdownAwareTask.setShutdownStatus(counter); } int counterState = counter.increment(); try { if (counterState > maxTrials) { shutdownAwareTask.notifyIgnored(); } else { if (counterState > silentTrials) { shutdownAwareTask.notifyShuttingDown(); } state.execute(); } } catch (ShutdownAwareTaskNotificationException e) { throw new TaskProcessException(e); } } LimitTrialCountStrategy(final int maxTrials, final int silentTrials); @Override void process(final ITaskExecutionState state);  }
@Test public void Should_NotifyAndExecuteTask() throws Exception { ITaskProcessStrategy strategy = new LimitTrialCountStrategy(4,2); strategy.process(executionState); strategy.process(executionState); verify(executionState, times(2)).execute(); verify(shutdownAwareTask, times(0)).notifyShuttingDown(); strategy.process(executionState); strategy.process(executionState); verify(executionState, times(4)).execute(); verify(shutdownAwareTask, times(2)).notifyShuttingDown(); verify(shutdownAwareTask, times(0)).notifyIgnored(); strategy.process(executionState); verify(executionState, times(4)).execute(); verify(shutdownAwareTask, times(2)).notifyShuttingDown(); verify(shutdownAwareTask, times(1)).notifyIgnored(); }