use of io.netty.channel.ChannelPromise in project netty by netty.
the class AbstractNioChannel method doClose.
@Override
protected void doClose() throws Exception {
ChannelPromise promise = connectPromise;
if (promise != null) {
// Use tryFailure() instead of setFailure() to avoid the race against cancel().
promise.tryFailure(new ClosedChannelException());
connectPromise = null;
}
Future<?> future = connectTimeoutFuture;
if (future != null) {
future.cancel(false);
connectTimeoutFuture = null;
}
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class WebSocketClientProtocolHandshakeHandler method applyHandshakeTimeout.
private void applyHandshakeTimeout() {
final ChannelPromise localHandshakePromise = handshakePromise;
if (handshakeTimeoutMillis <= 0 || localHandshakePromise.isDone()) {
return;
}
final Future<?> timeoutFuture = ctx.executor().schedule(new Runnable() {
@Override
public void run() {
if (localHandshakePromise.isDone()) {
return;
}
if (localHandshakePromise.tryFailure(new WebSocketClientHandshakeException("handshake timed out"))) {
ctx.flush().fireUserEventTriggered(ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT).close();
}
}
}, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
// Cancel the handshake timeout when handshake is finished.
localHandshakePromise.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> f) throws Exception {
timeoutFuture.cancel(false);
}
});
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class MessageToMessageEncoderTest method testIntermediateWriteFailures.
@Test
public void testIntermediateWriteFailures() {
ChannelHandler encoder = new MessageToMessageEncoder<Object>() {
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
out.add(new Object());
out.add(msg);
}
};
final Exception firstWriteException = new Exception();
ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {
private boolean firstWritten;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (firstWritten) {
ctx.write(msg, promise);
} else {
firstWritten = true;
promise.setFailure(firstWriteException);
}
}
};
EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
Object msg = new Object();
ChannelFuture write = channel.writeAndFlush(msg);
assertSame(firstWriteException, write.cause());
assertSame(msg, channel.readOutbound());
assertFalse(channel.finish());
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class AbstractHttp2StreamChannel method write0.
protected ChannelFuture write0(ChannelHandlerContext ctx, Object msg) {
ChannelPromise promise = ctx.newPromise();
ctx.write(msg, promise);
return promise;
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class SslHandlerTest method testNonApplicationDataFailureFailsQueuedWrites.
@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testNonApplicationDataFailureFailsQueuedWrites() throws NoSuchAlgorithmException, InterruptedException {
final CountDownLatch writeLatch = new CountDownLatch(1);
final Queue<ChannelPromise> writesToFail = new ConcurrentLinkedQueue<ChannelPromise>();
SSLEngine engine = newClientModeSSLEngine();
SslHandler handler = new SslHandler(engine) {
@Override
public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
super.write(ctx, msg, promise);
writeLatch.countDown();
}
};
EmbeddedChannel ch = new EmbeddedChannel(new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof ByteBuf) {
if (((ByteBuf) msg).isReadable()) {
writesToFail.add(promise);
} else {
promise.setSuccess();
}
}
ReferenceCountUtil.release(msg);
}
}, handler);
try {
final CountDownLatch writeCauseLatch = new CountDownLatch(1);
final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
ch.write(Unpooled.wrappedBuffer(new byte[] { 1 })).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
failureRef.compareAndSet(null, future.cause());
writeCauseLatch.countDown();
}
});
writeLatch.await();
// Simulate failing the SslHandler non-application writes after there are applications writes queued.
ChannelPromise promiseToFail;
while ((promiseToFail = writesToFail.poll()) != null) {
promiseToFail.setFailure(new RuntimeException("fake exception"));
}
writeCauseLatch.await();
Throwable writeCause = failureRef.get();
assertNotNull(writeCause);
assertThat(writeCause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
Throwable cause = handler.handshakeFuture().cause();
assertNotNull(cause);
assertThat(cause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
} finally {
assertFalse(ch.finishAndReleaseAll());
}
}
Aggregations