use of com.ctrip.xpipe.retry.RetryDelay in project x-pipe by ctripcorp.
the class AdvancedDcMetaServiceTest method testRetry3TimesUntilSuccess.
@Test
public void testRetry3TimesUntilSuccess() throws Exception {
ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
service.setScheduled(scheduled).setFactory(new DefaultRetryCommandFactory(3, new RetryDelay(10), scheduled));
Command<String> command = service.retry3TimesUntilSuccess(new AbstractCommand<String>() {
private AtomicInteger counter = new AtomicInteger(0);
@Override
protected void doExecute() throws Exception {
int currentCount = counter.getAndIncrement();
logger.info(String.format("Run %d time", currentCount));
if (currentCount > 1) {
future().setSuccess("success");
} else {
throw new XpipeRuntimeException("test exception");
}
}
@Override
protected void doReset() {
}
@Override
public String getName() {
return "test-retry";
}
});
AtomicBoolean complete = new AtomicBoolean(false);
command.future().addListener(commandFuture -> {
Assert.assertEquals("success", commandFuture.getNow());
complete.getAndSet(true);
});
command.execute();
waitConditionUntilTimeOut(() -> complete.get());
}
use of com.ctrip.xpipe.retry.RetryDelay in project x-pipe by ctripcorp.
the class CommandRetryWrapperTest method testRetryUntilTimeout.
@Test
public void testRetryUntilTimeout() {
int timeout = sleepBase * 10;
TestCommand command = new TestCommand(new Exception("just throw"));
CommandRetryWrapper<String> wrapper = (CommandRetryWrapper<String>) CommandRetryWrapper.buildTimeoutRetry(timeout, new RetryDelay(sleepBase), command, scheduled);
long before = System.currentTimeMillis();
try {
wrapper.execute().get();
Assert.fail();
} catch (InterruptedException | ExecutionException e) {
}
long after = System.currentTimeMillis();
Assert.assertTrue((after - before) >= timeout);
}
Aggregations