)) .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()) .withRetryListener(new RetryListener() { @Override
)) .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()) .withRetryListener(new RetryListener() { @Override
@Test public void testWithBlockStrategy() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); final AtomicInteger counter = new AtomicInteger(); BlockStrategy blockStrategy = sleepTime -> counter.incrementAndGet(); Retryer retryer = RetryerBuilder.newBuilder() .withBlockStrategy(blockStrategy) .retryIfResult(Objects::isNull) .build(); final int retryCount = 5; boolean result = retryer.call(callable); assertTrue(result); assertEquals(counter.get(), retryCount); }
.withStopStrategy(StopStrategies.neverStop()) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()).build(); final List<HostPort> brokerList = parseBrokerString(brokers); try {
@Test public void testCallWhenBlockerIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .withBlockStrategy(new InterruptingBlockStrategy(3)) .build(); Thrower thrower = new Thrower(Exception.class, 5); boolean interrupted = false; try { retryer.call(thrower); fail("Should have thrown"); } catch (InterruptedException e) { interrupted = true; } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(3, thrower.invocations); }
@Test public void testRunWhenBlockerIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .withBlockStrategy(new InterruptingBlockStrategy(3)) .build(); Thrower thrower = new Thrower(Exception.class, 5); boolean interrupted = false; try { retryer.run(thrower); fail("Should have thrown"); } catch (InterruptedException e) { interrupted = true; } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(3, thrower.invocations); }