use of com.ctrip.xpipe.command.CommandTimeoutException in project x-pipe by ctripcorp.
the class AbstractNettyRequestResponseCommand method doSendRequest.
@Override
protected void doSendRequest(final NettyClient nettyClient, ByteBuf byteBuf) {
if (logRequest()) {
logger.info("[doSendRequest]{}, {}", nettyClient, ByteBufUtils.readToString(byteBuf.slice()));
}
if (hasResponse()) {
nettyClient.sendRequest(byteBuf, this);
} else {
nettyClient.sendRequest(byteBuf);
// TODO sendfuture, make sure send success
future().setSuccess(null);
return;
}
if (getCommandTimeoutMilli() > 0 && scheduled != null) {
logger.debug("[doSendRequest][schedule timeout]{}, {}", this, getCommandTimeoutMilli());
final ScheduledFuture<?> timeoutFuture = scheduled.schedule(new AbstractExceptionLogTask() {
@Override
public void doRun() {
logger.info("[run][timeout]{}", nettyClient);
future().setFailure(new CommandTimeoutException("timeout " + +getCommandTimeoutMilli()));
}
}, getCommandTimeoutMilli(), TimeUnit.MILLISECONDS);
future().addListener(new CommandFutureListener<V>() {
@Override
public void operationComplete(CommandFuture<V> commandFuture) {
boolean cancel = true;
try {
commandFuture.get();
} catch (InterruptedException e) {
} catch (ExecutionException e) {
if (e.getCause() instanceof CommandTimeoutException) {
cancel = false;
}
}
if (cancel) {
logger.debug("[operationComplete][cancel timeout future]");
timeoutFuture.cancel(false);
}
}
});
}
}
use of com.ctrip.xpipe.command.CommandTimeoutException in project x-pipe by ctripcorp.
the class RedisCommandTest method testTimeoutNext.
@Test
public void testTimeoutNext() throws Exception {
Server server = startEchoPrefixServer(String.valueOf((char) RedisClientProtocol.PLUS_BYTE));
SimpleObjectPool<NettyClient> keyPool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress("127.0.0.1", server.getPort()));
int sleepTime = timeoutMilli + 50;
String str1 = String.format("sleep %d %s\r\n", sleepTime, randomString(10));
String str2 = randomString(10) + "\r\n";
try {
new TestCommand(str1, timeoutMilli, keyPool, scheduled).execute().get();
Assert.fail();
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof CommandTimeoutException);
}
sleep(sleepTime * 2);
new TestCommand(str2, sleepTime, keyPool, scheduled).execute().get();
}
use of com.ctrip.xpipe.command.CommandTimeoutException 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