Search in sources :

Example 1 with DefaultCommandFuture

use of com.ctrip.xpipe.command.DefaultCommandFuture in project x-pipe by ctripcorp.

the class OneThreadTaskExecutorTest method testClose.

@Test
public void testClose() throws Exception {
    CommandFuture<Void> future = new DefaultCommandFuture<>();
    when(command.execute()).thenReturn(future);
    future.setFailure(new Exception());
    RetryCommandFactory<Void> retryCommandFactory = DefaultRetryCommandFactory.retryForever(scheduled, 0);
    OneThreadTaskExecutor oneThreadTaskExecutor = new OneThreadTaskExecutor(retryCommandFactory, executors);
    oneThreadTaskExecutor.executeCommand(command);
    sleep(100);
    logger.info("[testClose][destroy]");
    oneThreadTaskExecutor.destroy();
    retryCommandFactory.destroy();
    sleep(50);
    verify(command, new AtLeast(1)).execute();
    verify(command, new AtLeast(1)).reset();
    logger.info("[testClose][sleep verify no more interactions]");
    sleep(100);
    verifyNoMoreInteractions(command);
}
Also used : DefaultCommandFuture(com.ctrip.xpipe.command.DefaultCommandFuture) AtLeast(org.mockito.internal.verification.AtLeast) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 2 with DefaultCommandFuture

use of com.ctrip.xpipe.command.DefaultCommandFuture in project x-pipe by ctripcorp.

the class OneThreadTaskExecutorTest method testRetryTemplate.

@Test
public void testRetryTemplate() throws TimeoutException {
    int retryTimes = 10;
    CommandFuture<Void> future = new DefaultCommandFuture<>();
    when(command.execute()).thenReturn(future);
    future.setFailure(new Exception());
    RetryCommandFactory retryCommandFactory = DefaultRetryCommandFactory.retryNTimes(scheduled, retryTimes, 10);
    OneThreadTaskExecutor oneThreadTaskExecutor = new OneThreadTaskExecutor(retryCommandFactory, executors);
    oneThreadTaskExecutor.executeCommand(command);
    waitConditionUntilTimeOut(() -> {
        try {
            verify(command, times(retryTimes + 1)).execute();
            return true;
        } catch (Throwable e) {
        }
        return false;
    }, 2000);
}
Also used : DefaultCommandFuture(com.ctrip.xpipe.command.DefaultCommandFuture) TimeoutException(java.util.concurrent.TimeoutException) DefaultRetryCommandFactory(com.ctrip.xpipe.command.DefaultRetryCommandFactory) RetryCommandFactory(com.ctrip.xpipe.command.RetryCommandFactory) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

AbstractTest (com.ctrip.xpipe.AbstractTest)2 DefaultCommandFuture (com.ctrip.xpipe.command.DefaultCommandFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 DefaultRetryCommandFactory (com.ctrip.xpipe.command.DefaultRetryCommandFactory)1 RetryCommandFactory (com.ctrip.xpipe.command.RetryCommandFactory)1 AtLeast (org.mockito.internal.verification.AtLeast)1