use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class ZlibTest method testGZIPCompressOnly0.
private void testGZIPCompressOnly0(byte[] data) throws IOException {
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.GZIP));
if (data != null) {
chEncoder.writeOutbound(Unpooled.wrappedBuffer(data));
}
assertTrue(chEncoder.finish());
ByteBuf encoded = Unpooled.buffer();
for (; ; ) {
ByteBuf buf = chEncoder.readOutbound();
if (buf == null) {
break;
}
encoded.writeBytes(buf);
buf.release();
}
ByteBuf decoded = Unpooled.buffer();
GZIPInputStream stream = new GZIPInputStream(new ByteBufInputStream(encoded, true));
try {
byte[] buf = new byte[8192];
for (; ; ) {
int readBytes = stream.read(buf);
if (readBytes < 0) {
break;
}
decoded.writeBytes(buf, 0, readBytes);
}
} finally {
stream.close();
}
if (data != null) {
assertEquals(Unpooled.wrappedBuffer(data), decoded);
} else {
assertFalse(decoded.isReadable());
}
decoded.release();
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class ZlibTest method testCompress0.
private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data) throws Exception {
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
try {
chEncoder.writeOutbound(data.retain());
chEncoder.flush();
data.resetReaderIndex();
for (; ; ) {
ByteBuf deflatedData = chEncoder.readOutbound();
if (deflatedData == null) {
break;
}
chDecoderZlib.writeInbound(deflatedData);
}
byte[] decompressed = new byte[data.readableBytes()];
int offset = 0;
for (; ; ) {
ByteBuf buf = chDecoderZlib.readInbound();
if (buf == null) {
break;
}
int length = buf.readableBytes();
buf.readBytes(decompressed, offset, length);
offset += length;
buf.release();
if (offset == decompressed.length) {
break;
}
}
assertEquals(data, Unpooled.wrappedBuffer(decompressed));
assertNull(chDecoderZlib.readInbound());
// Closing an encoder channel will generate a footer.
assertTrue(chEncoder.finish());
for (; ; ) {
Object msg = chEncoder.readOutbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
// But, the footer will be decoded into nothing. It's only for validation.
assertFalse(chDecoderZlib.finish());
data.release();
} finally {
dispose(chEncoder);
dispose(chDecoderZlib);
}
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class DelimiterBasedFrameDecoderTest method testFailSlowTooLongFrameRecovery.
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(1, true, false, Delimiters.nulDelimiter()));
for (int i = 0; i < 2; i++) {
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 }));
try {
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })));
fail(DecoderException.class.getSimpleName() + " must be raised.");
} catch (TooLongFrameException e) {
// Expected
}
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
ByteBuf buf = ch.readInbound();
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release();
}
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class DelimiterBasedFrameDecoderTest method testFailFastTooLongFrameRecovery.
@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()));
for (int i = 0; i < 2; i++) {
try {
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 })));
fail(DecoderException.class.getSimpleName() + " must be raised.");
} catch (TooLongFrameException e) {
// Expected
}
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
ByteBuf buf = ch.readInbound();
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release();
}
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class CompatibleObjectEncoderTest method testMultipleEncodeReferenceCount.
@Test
public void testMultipleEncodeReferenceCount() throws IOException, ClassNotFoundException {
EmbeddedChannel channel = new EmbeddedChannel(new CompatibleObjectEncoder());
testEncode(channel, new TestSerializable(6, 8));
testEncode(channel, new TestSerializable(10, 5));
testEncode(channel, new TestSerializable(1, 5));
assertFalse(channel.finishAndReleaseAll());
}
Aggregations