use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project cassandra by apache.
the class EntireSSTableStreamingCorrectFilesCountTest method createMockNettyChannel.
private EmbeddedChannel createMockNettyChannel(ByteBuf serializedFile) {
WritableByteChannel wbc = new WritableByteChannel() {
private boolean isOpen = true;
public int write(ByteBuffer src) {
int size = src.limit();
serializedFile.writeBytes(src);
return size;
}
public boolean isOpen() {
return isOpen;
}
public void close() {
isOpen = false;
}
};
return new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
((SharedDefaultFileRegion) msg).transferTo(wbc, 0);
super.write(ctx, msg, promise);
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project flink by apache.
the class ClientTransportErrorHandlingTest method testExceptionOnWrite.
/**
* Verifies that failed client requests via {@link PartitionRequestClient} are correctly
* attributed to the respective {@link RemoteInputChannel}.
*/
@Test
public void testExceptionOnWrite() throws Exception {
NettyProtocol protocol = new NettyProtocol(mock(ResultPartitionProvider.class), mock(TaskEventDispatcher.class)) {
@Override
public ChannelHandler[] getServerChannelHandlers() {
return new ChannelHandler[0];
}
};
// We need a real server and client in this test, because Netty's EmbeddedChannel is
// not failing the ChannelPromise of failed writes.
NettyServerAndClient serverAndClient = initServerAndClient(protocol, createConfig());
Channel ch = connect(serverAndClient);
NetworkClientHandler handler = getClientHandler(ch);
// Last outbound handler throws Exception after 1st write
ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
int writeNum = 0;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (writeNum >= 1) {
throw new RuntimeException("Expected test exception.");
}
writeNum++;
ctx.write(msg, promise);
}
});
PartitionRequestClient requestClient = new NettyPartitionRequestClient(ch, handler, mock(ConnectionID.class), mock(PartitionRequestClientFactory.class));
// Create input channels
RemoteInputChannel[] rich = new RemoteInputChannel[] { createRemoteInputChannel(), createRemoteInputChannel() };
final CountDownLatch sync = new CountDownLatch(1);
// Do this with explicit synchronization. Otherwise this is not robust against slow timings
// of the callback (e.g. we cannot just verify that it was called once, because there is
// a chance that we do this too early).
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
sync.countDown();
return null;
}
}).when(rich[1]).onError(isA(LocalTransportException.class));
// First request is successful
requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[0], 0);
// Second request is *not* successful
requestClient.requestSubpartition(new ResultPartitionID(), 0, rich[1], 0);
// Wait for the notification and it could confirm all the request operations are done
if (!sync.await(TestingUtils.TESTING_DURATION.toMillis(), TimeUnit.MILLISECONDS)) {
fail("Timed out after waiting for " + TestingUtils.TESTING_DURATION.toMillis() + " ms to be notified about the channel error.");
}
// Only the second channel should be notified about the error
verify(rich[0], times(0)).onError(any(LocalTransportException.class));
shutdown(serverAndClient);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter 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 org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter 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 org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter 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);
}
Aggregations