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 committed by GitHub
parent b91889c3db
commit 7817827324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -63,7 +63,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<String, InetSocketAddress>("netty", recipient, sender)));
DatagramPacket packet = channel.readOutbound();