public R apply(final T t) throws Exception { while (true) { try { return getUserFunction().apply(t); } catch (Exception e) { if (retryStrategy.shouldRetry()) { log.info(retryStrategy.retryMessage()); } else { log.info(retryStrategy.retryMessage()); throw e; } } } }
@Override protected IFunctionThrowsException<T, R> getUserFunction() { this.count++; return super.getUserFunction(); }
public RetryableFunction<BT, BR> build() { final IRetryStrategy retryStrategy = new RetryStrategyConfiguration(this.conf).getRetryStrategy(); return new RetryableFunction<BT, BR>(this.func, retryStrategy); } }
public MockRetryableFunction(IFunctionThrowsException<T,R> func, IRetryStrategy retryStrategy) { super(func,spy(retryStrategy)); when(super.retryStrategy.shouldRetry()).thenCallRealMethod(); when(super.retryStrategy.retryMessage()).thenCallRealMethod(); }
@Test(expected = IllegalArgumentException.class) public void testBadRetryableTask() throws Exception { counter = 0; final MockRetryableFunction<Integer, Integer> divideBy = new MockRetryableFunction<>( this::divide, new RetryStrategyConfiguration(conf).getRetryStrategy()); try { divideBy.apply(0); } finally { divideBy.verifyRetry(maxRetries, maxRetries); } } }
public void verifyRetry(int numFuncApply, int numShouldRetry) { Assert.assertEquals(count, numFuncApply); Mockito.verify(this.retryStrategy, times(numShouldRetry)).shouldRetry(); } }
public IRetryStrategy getRetryStrategy() { final String strategy = conf.getProperty(DEFAULT_STRATEGY, SIMPLE_RETRY_STRATEGY); switch (strategy) { case SIMPLE_RETRY_STRATEGY: return new SimpleRetryStrategy(new SimpleRetryStrategyConfiguration(conf)); default: throw new JobRuntimeException(String.format("Retry strategy %s is not supported.", strategy)); } } }
@Test public void testGoodRetryableFunction() throws Exception { counter = 0; final MockRetryableFunction<Integer, Integer> divideBy = new MockRetryableFunction<>( this::divide, new RetryStrategyConfiguration(conf).getRetryStrategy()); assertEquals(new Integer(50), divideBy.apply(2)); divideBy.verifyRetry(maxRetries, maxRetries-1); }