Fix UDP tests to rely on a random port
* Iterate over addresses in NI for a real one * Use `0` for `ackPort` * Use real `InetAddress` from the `NetworkInterface`
This commit is contained in:
@@ -25,6 +25,7 @@ import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -97,7 +98,21 @@ public class DatagramPacketMulticastSendingHandlerTests {
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
NetworkInterface nic = this.multicastRule.getNic();
|
||||
if (nic != null) {
|
||||
handler.setLocalAddress(nic.getInetAddresses().nextElement().getHostName());
|
||||
String hostName = null;
|
||||
Enumeration<InetAddress> addressesFromNetworkInterface = nic.getInetAddresses();
|
||||
while (addressesFromNetworkInterface.hasMoreElements()) {
|
||||
InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
|
||||
if (inetAddress.isSiteLocalAddress()
|
||||
&& !inetAddress.isAnyLocalAddress()
|
||||
&& !inetAddress.isLinkLocalAddress()
|
||||
&& !inetAddress.isLoopbackAddress()) {
|
||||
|
||||
hostName = inetAddress.getHostName();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
handler.setLocalAddress(hostName);
|
||||
}
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(MessageBuilder.withPayload(payload).build());
|
||||
@@ -119,11 +134,12 @@ public class DatagramPacketMulticastSendingHandlerTests {
|
||||
final int testPort = socket.getLocalPort();
|
||||
final AtomicInteger ackPort = new AtomicInteger();
|
||||
|
||||
final String multicastAddress = "225.6.7.8";
|
||||
final String multicastAddress = this.multicastRule.getGroup();
|
||||
final String payload = "foobar";
|
||||
final CountDownLatch listening = new CountDownLatch(2);
|
||||
final CountDownLatch ackListening = new CountDownLatch(1);
|
||||
final CountDownLatch ackSent = new CountDownLatch(2);
|
||||
NetworkInterface nic = this.multicastRule.getNic();
|
||||
Runnable catcher = () -> {
|
||||
try {
|
||||
byte[] buffer = new byte[1000];
|
||||
@@ -149,8 +165,21 @@ public class DatagramPacketMulticastSendingHandlerTests {
|
||||
Message<byte[]> message = mapper.toMessage(receivedPacket);
|
||||
Object id = message.getHeaders().get(IpHeaders.ACK_ID);
|
||||
byte[] ack = id.toString().getBytes();
|
||||
InetAddress inetAddress = null;
|
||||
Enumeration<InetAddress> addressesFromNetworkInterface = nic.getInetAddresses();
|
||||
while (addressesFromNetworkInterface.hasMoreElements()) {
|
||||
InetAddress address = addressesFromNetworkInterface.nextElement();
|
||||
if (address.isSiteLocalAddress()
|
||||
&& !address.isAnyLocalAddress()
|
||||
&& !address.isLinkLocalAddress()
|
||||
&& !address.isLoopbackAddress()) {
|
||||
|
||||
inetAddress = address;
|
||||
break;
|
||||
}
|
||||
}
|
||||
DatagramPacket ackPack = new DatagramPacket(ack, ack.length,
|
||||
new InetSocketAddress(multicastRule.getNic().getInetAddresses().nextElement(), ackPort.get()));
|
||||
new InetSocketAddress(inetAddress, ackPort.get()));
|
||||
DatagramSocket out = new DatagramSocket();
|
||||
out.send(ackPack);
|
||||
out.close();
|
||||
@@ -168,7 +197,24 @@ public class DatagramPacketMulticastSendingHandlerTests {
|
||||
assertThat(listening.await(10000, TimeUnit.MILLISECONDS)).isTrue();
|
||||
MulticastSendingMessageHandler handler =
|
||||
new MulticastSendingMessageHandler(multicastAddress, testPort, true, true, "localhost", 0, 10000);
|
||||
handler.setLocalAddress(this.multicastRule.getNic().getInetAddresses().nextElement().getHostName());
|
||||
|
||||
if (nic != null) {
|
||||
String hostName = null;
|
||||
Enumeration<InetAddress> addressesFromNetworkInterface = nic.getInetAddresses();
|
||||
while (addressesFromNetworkInterface.hasMoreElements()) {
|
||||
InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
|
||||
if (inetAddress.isSiteLocalAddress()
|
||||
&& !inetAddress.isAnyLocalAddress()
|
||||
&& !inetAddress.isLinkLocalAddress()
|
||||
&& !inetAddress.isLoopbackAddress()) {
|
||||
|
||||
hostName = inetAddress.getHostName();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
handler.setLocalAddress(hostName);
|
||||
}
|
||||
handler.setMinAcksForSuccess(2);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
@@ -23,8 +23,10 @@ import static org.mockito.Mockito.mock;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -34,8 +36,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledOnJre;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -49,6 +49,7 @@ import org.springframework.integration.ip.util.SocketTestUtils;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -220,7 +221,7 @@ public class UdpChannelAdapterTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testUnicastSender() throws Exception {
|
||||
public void testUnicastSender() {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(0);
|
||||
adapter.setBeanName("test");
|
||||
@@ -246,7 +247,6 @@ public class UdpChannelAdapterTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@EnabledOnJre(JRE.JAVA_8)
|
||||
public void testMulticastReceiver() throws Exception {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
MulticastReceivingChannelAdapter adapter =
|
||||
@@ -264,7 +264,24 @@ public class UdpChannelAdapterTests {
|
||||
DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
|
||||
DatagramPacket packet = mapper.fromMessage(message);
|
||||
packet.setSocketAddress(new InetSocketAddress(this.multicastRule.getGroup(), port));
|
||||
DatagramSocket datagramSocket = new DatagramSocket(0, nic.getInetAddresses().nextElement());
|
||||
InetAddress inetAddress = null;
|
||||
if (nic != null) {
|
||||
Enumeration<InetAddress> addressesFromNetworkInterface = nic.getInetAddresses();
|
||||
while (addressesFromNetworkInterface.hasMoreElements()) {
|
||||
InetAddress address = addressesFromNetworkInterface.nextElement();
|
||||
if (address.isSiteLocalAddress()
|
||||
&& !address.isAnyLocalAddress()
|
||||
&& !address.isLinkLocalAddress()
|
||||
&& !address.isLoopbackAddress()) {
|
||||
|
||||
inetAddress = address;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
DatagramSocket datagramSocket =
|
||||
new DatagramSocket(SocketUtils.findAvailableUdpPort(), inetAddress);
|
||||
datagramSocket.send(packet);
|
||||
datagramSocket.close();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user