Search in sources :

Example 6 with RetryDelay

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultRetryCommandFactory(com.ctrip.xpipe.command.DefaultRetryCommandFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryDelay(com.ctrip.xpipe.retry.RetryDelay) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) IOException(java.io.IOException) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test)

Example 7 with RetryDelay

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);
}
Also used : RetryDelay(com.ctrip.xpipe.retry.RetryDelay) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

RetryDelay (com.ctrip.xpipe.retry.RetryDelay)7 Test (org.junit.Test)4 AbstractTest (com.ctrip.xpipe.AbstractTest)3 ExecutionException (java.util.concurrent.ExecutionException)3 DefaultRetryCommandFactory (com.ctrip.xpipe.command.DefaultRetryCommandFactory)2 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)2 XpipeObjectPoolFromKeyed (com.ctrip.xpipe.pool.XpipeObjectPoolFromKeyed)2 InetSocketAddress (java.net.InetSocketAddress)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 AbstractConsoleIntegrationTest (com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest)1 KeeperSetStateCommand (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractKeeperCommand.KeeperSetStateCommand)1 RedisError (com.ctrip.xpipe.redis.core.protocal.error.RedisError)1 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PostConstruct (javax.annotation.PostConstruct)1