use of com.ctrip.xpipe.api.command.CommandFuture in project x-pipe by ctripcorp.
the class AsyncSendEmailCommandTest method testAsyncSendEmailCommand.
@Test
public void testAsyncSendEmailCommand() throws Exception {
when(client.sendEmail(any())).thenThrow(new XpipeException("test exception"));
CtripPlatformEmailService.AsyncSendEmailCommand command = new CtripPlatformEmailService.AsyncSendEmailCommand(generateEmail());
command.setClient(client);
CommandFuture future = command.execute();
Assert.assertFalse(future.isSuccess());
Assert.assertEquals("test exception", future.cause().getMessage());
}
use of com.ctrip.xpipe.api.command.CommandFuture in project x-pipe by ctripcorp.
the class AsyncSendEmailCommandTest method testAsyncSendEmailCommand2.
@Test
public void testAsyncSendEmailCommand2() throws Exception {
SendEmailResponse response = new SendEmailResponse();
response.setResultCode(1);
when(client.sendEmail(any())).thenReturn(response);
GetEmailStatusResponse getResponse = new GetEmailStatusResponse();
getResponse.setResultCode(1);
when(client.getEmailStatus(any())).thenReturn(getResponse);
CtripPlatformEmailService.AsyncSendEmailCommand command = new CtripPlatformEmailService.AsyncSendEmailCommand(generateEmail());
command.setClient(client);
CommandFuture future = command.execute();
Assert.assertTrue(future.isSuccess());
}
use of com.ctrip.xpipe.api.command.CommandFuture in project x-pipe by ctripcorp.
the class ScheduleCommandWrapper method doExecute.
@Override
protected void doExecute() throws Exception {
final ScheduledFuture<?> scheduleFuture = scheduled.schedule(new AbstractExceptionLogTask() {
@Override
protected void doRun() throws Exception {
try {
command.execute().addListener(new CommandFutureListener<V>() {
@Override
public void operationComplete(CommandFuture<V> commandFuture) throws Exception {
if (commandFuture.isSuccess()) {
future().setSuccess(commandFuture.get());
} else {
future().setFailure(ExceptionUtils.getRootCause(commandFuture.cause()));
}
}
});
} catch (Exception e) {
future().setFailure(ExceptionUtils.getRootCause(e));
}
}
}, time, timeUnit);
future().addListener(new CommandFutureListener<V>() {
@Override
public void operationComplete(CommandFuture<V> commandFuture) throws Exception {
if (commandFuture.isCancelled()) {
logger.info("[command canceled][cancel execution]{}", time);
command.future().cancel(true);
scheduleFuture.cancel(false);
}
}
});
}
use of com.ctrip.xpipe.api.command.CommandFuture in project x-pipe by ctripcorp.
the class TransactionalCommand method doWork.
@SuppressWarnings("unchecked")
protected void doWork(final SimpleObjectPool<NettyClient> clientPool) {
SequenceCommandChain chain = new SequenceCommandChain(false);
for (RedisCommand currentCommand : commands) {
OneTranscationCommand oneTranscationCommand = new OneTranscationCommand(clientPool, currentCommand, scheduled);
chain.add(oneTranscationCommand);
}
chain.execute().addListener(new CommandFutureListener() {
@Override
public void operationComplete(CommandFuture commandFuture) throws Exception {
if (!commandFuture.isSuccess()) {
logger.error("[doWork][fail]", commandFuture.cause());
}
endTranscation(clientPool);
}
});
}
use of com.ctrip.xpipe.api.command.CommandFuture in project x-pipe by ctripcorp.
the class RequestResponseCommandTest method testTimeout.
@Test
public void testTimeout() throws CommandExecutionException, InterruptedException {
TestCommand testCommand = new TestCommand("sleep 5000\r\n", 1000, clientPool, scheduled, null);
CommandFuture<String> future = testCommand.execute();
final AtomicReference<CommandFuture<String>> listenerFuture = new AtomicReference<CommandFuture<String>>(null);
final CountDownLatch latch = new CountDownLatch(1);
future.addListener(new CommandFutureListener<String>() {
@Override
public void operationComplete(CommandFuture<String> commandFuture) throws Exception {
try {
listenerFuture.set(commandFuture);
} finally {
latch.countDown();
}
}
});
try {
future.get();
Assert.fail();
} catch (InterruptedException e) {
Assert.fail();
} catch (ExecutionException e) {
if (!(e.getCause() instanceof CommandTimeoutException)) {
Assert.fail();
}
}
latch.await();
Assert.assertTrue(listenerFuture.get() != null);
}
Aggregations