Allow null sender when using DatagramPacketEncoder (#9204)

Motivation:

It is valid to use null as sender so we should support it when DatagramPacketEncoder checks if it supports the message.

Modifications:

- Add null check
- Add unit test

Result:

Fixes https://github.com/netty/netty/issues/9199.
This commit is contained in:
Norman Maurer 2019-06-03 08:44:35 +02:00
parent a63584715f
commit 4b8db65b16
2 changed files with 11 additions and 2 deletions

View File

@ -64,7 +64,7 @@ public class DatagramPacketEncoder<M> extends MessageToMessageEncoder<AddressedE
@SuppressWarnings("rawtypes")
AddressedEnvelope envelope = (AddressedEnvelope) msg;
return encoder.acceptOutboundMessage(envelope.content())
&& envelope.sender() instanceof InetSocketAddress
&& (envelope.sender() instanceof InetSocketAddress || envelope.sender() == null)
&& envelope.recipient() instanceof InetSocketAddress;
}
return false;

View File

@ -51,8 +51,17 @@ public class DatagramPacketEncoderTest {
@Test
public void testEncode() {
testEncode(false);
}
@Test
public void testEncodeWithSenderIsNull() {
testEncode(true);
}
private void testEncode(boolean senderIsNull) {
InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
InetSocketAddress sender = senderIsNull ? null : SocketUtils.socketAddress("127.0.0.1", 20000);
assertTrue(channel.writeOutbound(
new DefaultAddressedEnvelope<>("netty", recipient, sender)));
DatagramPacket packet = channel.readOutbound();