DatagramPacketEncoder|Decoder should take into account if wrapped handler is sharable

Motivation:

DatagramPacketEncoder|Decoder should respect if the wrapped handler is sharable or not and depending on that be sharable or not.

Modifications:

- Delegate isSharable() to wrapped handler
- Add test-cases

Result:

Correct behavior
This commit is contained in:
Norman Maurer 2017-02-22 08:44:04 +01:00
parent 325cc84a2e
commit 4f78bae2eb
4 changed files with 92 additions and 0 deletions

View File

@ -108,4 +108,9 @@ public class DatagramPacketDecoder extends MessageToMessageDecoder<DatagramPacke
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
decoder.handlerRemoved(ctx); decoder.handlerRemoved(ctx);
} }
@Override
public boolean isSharable() {
return decoder.isSharable();
}
} }

View File

@ -140,4 +140,9 @@ public class DatagramPacketEncoder<M> extends MessageToMessageEncoder<AddressedE
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
encoder.exceptionCaught(ctx, cause); encoder.exceptionCaught(ctx, cause);
} }
@Override
public boolean isSharable() {
return encoder.isSharable();
}
} }

View File

@ -17,6 +17,7 @@ package io.netty.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
@ -27,6 +28,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -56,4 +58,39 @@ public class DatagramPacketDecoderTest {
assertTrue(channel.writeInbound(new DatagramPacket(content, recipient, sender))); assertTrue(channel.writeInbound(new DatagramPacket(content, recipient, sender)));
assertEquals("netty", channel.readInbound()); assertEquals("netty", channel.readInbound());
} }
@Test
public void testIsNotSharable() {
testIsSharable(false);
}
@Test
public void testIsSharable() {
testIsSharable(true);
}
private static void testIsSharable(boolean sharable) {
MessageToMessageDecoder<ByteBuf> wrapped = new TestMessageToMessageDecoder(sharable);
DatagramPacketDecoder decoder = new DatagramPacketDecoder(wrapped);
assertEquals(wrapped.isSharable(), decoder.isSharable());
}
private static final class TestMessageToMessageDecoder extends MessageToMessageDecoder<ByteBuf> {
private final boolean sharable;
TestMessageToMessageDecoder(boolean sharable) {
this.sharable = sharable;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
// NOOP
}
@Override
public boolean isSharable() {
return sharable;
}
}
} }

View File

@ -15,6 +15,9 @@
*/ */
package io.netty.handler.codec; package io.netty.handler.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultAddressedEnvelope; import io.netty.channel.DefaultAddressedEnvelope;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.DatagramPacket;
@ -26,6 +29,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -82,4 +86,45 @@ public class DatagramPacketEncoderTest {
assertTrue(channel.writeOutbound(netty)); assertTrue(channel.writeOutbound(netty));
assertSame(netty, channel.readOutbound()); assertSame(netty, channel.readOutbound());
} }
@Test
public void testIsNotSharable() {
testSharable(false);
}
@Test
public void testIsSharable() {
testSharable(true);
}
private static void testSharable(boolean sharable) {
MessageToMessageEncoder<AddressedEnvelope<ByteBuf, InetSocketAddress>> wrapped =
new TestMessageToMessageEncoder(sharable);
DatagramPacketEncoder<AddressedEnvelope<ByteBuf, InetSocketAddress>> encoder =
new DatagramPacketEncoder<AddressedEnvelope<ByteBuf, InetSocketAddress>>(wrapped);
assertEquals(wrapped.isSharable(), encoder.isSharable());
}
private static final class TestMessageToMessageEncoder
extends MessageToMessageEncoder<AddressedEnvelope<ByteBuf, InetSocketAddress>> {
private final boolean sharable;
TestMessageToMessageEncoder(boolean sharable) {
this.sharable = sharable;
}
@Override
protected void encode(
ChannelHandlerContext ctx, AddressedEnvelope<ByteBuf,
InetSocketAddress> msg, List<Object> out) {
// NOOP
}
@Override
public boolean isSharable() {
return sharable;
}
}
} }