use of io.netty.channel.ChannelPromise in project netty by netty.
the class ChunkedWriteHandlerTest method testFailureWhenLastChunkFailed.
// See https://github.com/netty/netty/issues/8700.
@Test
public void testFailureWhenLastChunkFailed() throws IOException {
ChannelOutboundHandlerAdapter failLast = new ChannelOutboundHandlerAdapter() {
private int passedWrites;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (++this.passedWrites < 4) {
ctx.write(msg, promise);
} else {
ReferenceCountUtil.release(msg);
promise.tryFailure(new RuntimeException());
}
}
};
EmbeddedChannel ch = new EmbeddedChannel(failLast, new ChunkedWriteHandler());
// 4 chunks
ChannelFuture r = ch.writeAndFlush(new ChunkedFile(TMP, 1024 * 16));
assertTrue(ch.finish());
assertFalse(r.isSuccess());
assertTrue(r.cause() instanceof RuntimeException);
// 3 out of 4 chunks were already written
int read = 0;
for (; ; ) {
ByteBuf buffer = ch.readOutbound();
if (buffer == null) {
break;
}
read += buffer.readableBytes();
buffer.release();
}
assertEquals(1024 * 16 * 3, read);
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class ChunkedWriteHandlerTest method checkSkipFailed.
private static void checkSkipFailed(Object input1, Object input2) {
ChannelOutboundHandlerAdapter failFirst = new ChannelOutboundHandlerAdapter() {
private boolean alreadyFailed;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (alreadyFailed) {
ctx.write(msg, promise);
} else {
this.alreadyFailed = true;
ReferenceCountUtil.release(msg);
promise.tryFailure(new RuntimeException());
}
}
};
EmbeddedChannel ch = new EmbeddedChannel(failFirst, new ChunkedWriteHandler());
ChannelFuture r1 = ch.write(input1);
ChannelFuture r2 = ch.writeAndFlush(input2).awaitUninterruptibly();
assertTrue(ch.finish());
assertTrue(r1.cause() instanceof RuntimeException);
assertTrue(r2.isSuccess());
// note, that after we've "skipped" the first write,
// we expect to see the second message, chunk by chunk
int i = 0;
int read = 0;
for (; ; ) {
ByteBuf buffer = ch.readOutbound();
if (buffer == null) {
break;
}
while (buffer.isReadable()) {
assertEquals(BYTES[i++], buffer.readByte());
read++;
if (i == BYTES.length) {
i = 0;
}
}
buffer.release();
}
assertEquals(BYTES.length, read);
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class ChunkedWriteHandlerTest method checkFirstFailed.
private static void checkFirstFailed(Object input) {
ChannelOutboundHandlerAdapter noOpWrites = new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
ReferenceCountUtil.release(msg);
promise.tryFailure(new RuntimeException());
}
};
EmbeddedChannel ch = new EmbeddedChannel(noOpWrites, new ChunkedWriteHandler());
ChannelFuture r = ch.writeAndFlush(input);
// Should be `false` as we do not expect any messages to be written
assertFalse(ch.finish());
assertTrue(r.cause() instanceof RuntimeException);
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class ChunkedWriteHandlerTest method testEndOfInputWhenChannelIsClosedwhenWrite.
@Test
public void testEndOfInputWhenChannelIsClosedwhenWrite() {
ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {
@Override
public boolean isEndOfInput() {
return true;
}
@Override
public void close() {
}
@Deprecated
@Override
public ByteBuf readChunk(ChannelHandlerContext ctx) {
return null;
}
@Override
public ByteBuf readChunk(ByteBufAllocator allocator) {
return null;
}
@Override
public long length() {
return -1;
}
@Override
public long progress() {
return 1;
}
};
EmbeddedChannel ch = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
ReferenceCountUtil.release(msg);
// Calling close so we will drop all queued messages in the ChunkedWriteHandler.
ctx.close();
promise.setSuccess();
}
}, new ChunkedWriteHandler());
ch.writeAndFlush(input).syncUninterruptibly();
assertFalse(ch.finishAndReleaseAll());
}
use of io.netty.channel.ChannelPromise in project netty by netty.
the class SpdySessionHandler method issueStreamError.
/*
* SPDY Stream Error Handling:
*
* Upon a stream error, the endpoint must send a RST_STREAM frame which contains
* the Stream-ID for the stream where the error occurred and the error getStatus which
* caused the error.
*
* After sending the RST_STREAM, the stream is closed to the sending endpoint.
*
* Note: this is only called by the worker thread
*/
private void issueStreamError(ChannelHandlerContext ctx, int streamId, SpdyStreamStatus status) {
boolean fireChannelRead = !spdySession.isRemoteSideClosed(streamId);
ChannelPromise promise = ctx.newPromise();
removeStream(streamId, promise);
SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, status);
ctx.writeAndFlush(spdyRstStreamFrame, promise);
if (fireChannelRead) {
ctx.fireChannelRead(spdyRstStreamFrame);
}
}
Aggregations