NowOrLaterRetryPolicy implements SchedulerRetryPolicy { public SchedulerRetryAction getRetryAction(ScheduledTask task) { if (task == null) { throw new IllegalArgumentException("task cannot be null"); } Throwable result = task.getLastFailure(); if (result == null) { throw new IllegalStateException("task's last failure " + "cannot be null"); } if ((result instanceof ExceptionRetryStatus) && (((ExceptionRetryStatus) result).shouldRetry())) { if (task.getTryCount() > retryBackoffThreshold) { if (result instanceof TransactionTimeoutException && task.getTimeout() * 2L < (long) Integer.MAX_VALUE) { logger.logThrow(Level.WARNING, task.getLastFailure(), "Task has been retried {0} times: {1}\n" + "Increasing its timeout to {2} ms and " + "scheduling its retry for later", task.getTryCount(), task, task.getTimeout() * 2); task.setTimeout(task.getTimeout() * 2); } else { logger.logThrow(Level.WARNING, task.getLastFailure(), "Task has been retried {0} times: {1}\n" + "scheduling its retry for later", task.getTryCount(), task); } return SchedulerRetryAction.RETRY_LATER; } else { return SchedulerRetryAction.RETRY_NOW; } } else { if (logger.isLoggable(Level.FINE)) { if (task.isRecurring()) { logger.log(Level.FINE, "skipping a recurrence of a task because it " + "failed with a non-retryable exception: {0}", task); } else { logger.log(Level.FINE, "dropping a task because it failed with a " + "non-retryable exception: {0}", task); } } return SchedulerRetryAction.DROP; } } NowOrLaterRetryPolicy(Properties properties); SchedulerRetryAction getRetryAction(ScheduledTask task);  }
@Test(expected=IllegalArgumentException.class) public void testNullTask() { policy.getRetryAction(null); }
@Test(expected=IllegalStateException.class) public void testNullResult() { setupTask(null); replayMocks(); policy.getRetryAction(task); }
@Test public void testRetryableTrueResult() { setupTask(new RetryableException(true)); EasyMock.expect(task.getTryCount()).andStubReturn(1); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.RETRY_NOW, action); verifyMocks(); }
@Test public void testRetryableFalseResult() { setupTask(new RetryableException(false)); EasyMock.expect(task.isRecurring()).andStubReturn(true); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.DROP, action); verifyMocks(); }
@Test public void testRetryableTrueResultAndRetryCountAboveBackoffThreshold() { setupTask(new RetryableException(true)); EasyMock.expect(task.getTryCount()).andStubReturn( NowOrLaterRetryPolicy.DEFAULT_RETRY_BACKOFF_THRESHOLD + 1); EasyMock.expect(task.getTimeout()).andStubReturn(100L); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.RETRY_LATER, action); verifyMocks(); }
@Test public void testTimeoutResultAndRetryCountAboveBackoffThreshold() { setupTask(new TransactionTimeoutException("timed out")); EasyMock.expect(task.getTryCount()).andStubReturn( NowOrLaterRetryPolicy.DEFAULT_RETRY_BACKOFF_THRESHOLD + 1); EasyMock.expect(task.getTimeout()).andStubReturn(100L); task.setTimeout(100L * 2); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.RETRY_LATER, action); verifyMocks(); }
@Test public void testTimeoutResultAndTimeoutSetTooHigh() { setupTask(new TransactionTimeoutException("timed out")); EasyMock.expect(task.getTryCount()).andStubReturn( NowOrLaterRetryPolicy.DEFAULT_RETRY_BACKOFF_THRESHOLD + 1); EasyMock.expect(task.getTimeout()).andStubReturn( (long) (Integer.MAX_VALUE / 2 + Integer.MAX_VALUE / 4)); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.RETRY_LATER, action); verifyMocks(); }
@Test public void testNotRetryableExceptionAndRecurringTask() { setupTask(new Exception()); EasyMock.expect(task.isRecurring()).andStubReturn(true); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.DROP, action); verifyMocks(); }
@Test public void testNotRetryableExceptionAndNotRecurringTask() { setupTask(new Exception()); EasyMock.expect(task.isRecurring()).andStubReturn(false); replayMocks(); SchedulerRetryAction action = policy.getRetryAction(task); Assert.assertEquals(SchedulerRetryAction.DROP, action); verifyMocks(); }