use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class Http2FrameRoundtripTest method captureWrites.
private ByteBuf captureWrites() {
ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
verify(ctx, atLeastOnce()).write(captor.capture(), isA(ChannelPromise.class));
CompositeByteBuf composite = releaseLater(Unpooled.compositeBuffer());
for (ByteBuf buf : captor.getAllValues()) {
buf = releaseLater(buf.retain());
composite.addComponent(true, buf);
}
return composite;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class DatagramUnicastTest method testSimpleSendCompositeHeapByteBuf.
public void testSimpleSendCompositeHeapByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 0, 2));
buf.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2));
testSimpleSend(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2));
testSimpleSend(sb, cb, buf2, true, BYTES, 4);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class DatagramUnicastTest method testSimpleSendCompositeMixedByteBuf.
public void testSimpleSendCompositeMixedByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2));
testSimpleSend(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2));
testSimpleSend(sb, cb, buf2, true, BYTES, 4);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class SocketGatheringWriteTest method testGatheringWrite0.
private void testGatheringWrite0(ServerBootstrap sb, Bootstrap cb, byte[] data, boolean composite, boolean autoRead) throws Throwable {
sb.childOption(ChannelOption.AUTO_READ, autoRead);
cb.option(ChannelOption.AUTO_READ, autoRead);
Promise<Void> serverDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
final TestServerHandler sh = new TestServerHandler(autoRead, serverDonePromise, data.length);
final TestHandler ch = new TestHandler(autoRead);
cb.handler(ch);
sb.childHandler(sh);
Channel sc = sb.bind().sync().channel();
Channel cc = cb.connect(sc.localAddress()).sync().channel();
for (int i = 0; i < data.length; ) {
int length = Math.min(random.nextInt(1024 * 8), data.length - i);
if (composite && i % 2 == 0) {
int firstBufLength = length / 2;
CompositeByteBuf comp = compositeBuffer();
comp.addComponent(true, wrappedBuffer(data, i, firstBufLength)).addComponent(true, wrappedBuffer(data, i + firstBufLength, length - firstBufLength));
cc.write(comp);
} else {
cc.write(wrappedBuffer(data, i, length));
}
i += length;
}
ChannelFuture cf = cc.writeAndFlush(Unpooled.EMPTY_BUFFER);
assertNotEquals(cc.voidPromise(), cf);
try {
assertTrue(cf.await(60000));
cf.sync();
} catch (Throwable t) {
// TODO: Remove this once we fix this test.
TestUtils.dump(StringUtil.simpleClassName(this));
throw t;
}
serverDonePromise.sync();
sh.channel.close().sync();
ch.channel.close().sync();
sc.close().sync();
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
throw sh.exception.get();
}
if (sh.exception.get() != null) {
throw sh.exception.get();
}
if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
throw ch.exception.get();
}
if (ch.exception.get() != null) {
throw ch.exception.get();
}
ByteBuf expected = wrappedBuffer(data);
assertEquals(expected, sh.received);
expected.release();
sh.received.release();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.
private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
String url = "ws://localhost:9999/ws";
final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, -1) {
@Override
protected FullHttpRequest newHandshakeRequest() {
return shaker.newHandshakeRequest();
}
@Override
protected void verify(FullHttpResponse response) {
// Not do any verification, so we not need to care sending the correct headers etc in the test,
// which would just make things more complicated.
}
@Override
protected WebSocketFrameDecoder newWebsocketDecoder() {
return shaker.newWebsocketDecoder();
}
@Override
protected WebSocketFrameEncoder newWebSocketEncoder() {
return shaker.newWebSocketEncoder();
}
};
// use randomBytes helper from utils to check that it functions properly
byte[] data = WebSocketUtil.randomBytes(24);
// Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
// to test the actual handshaker.
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
FullHttpRequest request = shaker.newHandshakeRequest();
WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(request);
request.release();
EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
for (; ; ) {
ByteBuf frameBytes = websocketChannel.readOutbound();
if (frameBytes == null) {
break;
}
compositeByteBuf.addComponent(true, frameBytes);
}
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
handshaker.finishHandshake(ctx.channel(), msg);
ctx.pipeline().remove(this);
}
});
if (codec) {
ch.pipeline().addFirst(new HttpClientCodec());
} else {
ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
}
// We need to first write the request as HttpClientCodec will fail if we receive a response before a request
// was written.
shaker.handshake(ch).syncUninterruptibly();
for (; ; ) {
// Just consume the bytes, we are not interested in these.
ByteBuf buf = ch.readOutbound();
if (buf == null) {
break;
}
buf.release();
}
assertTrue(ch.writeInbound(compositeByteBuf));
assertTrue(ch.finish());
BinaryWebSocketFrame frame = ch.readInbound();
ByteBuf expect = Unpooled.wrappedBuffer(data);
try {
assertEquals(expect, frame.content());
assertTrue(frame.isFinalFragment());
assertEquals(0, frame.rsv());
} finally {
expect.release();
frame.release();
}
}
Aggregations