Skip to content

Commit 6e7f818

Browse files
committed
INT-3794: SocketUtils - Bind to Loopback Adapter
JIRA: https://jira.spring.io/browse/INT-3794 On OSX, if a server socket is in use on the loopback adapter, but not on others, a BindException is not thrown; instead the socket is bound to other adapters. This causes test failures because tests, generally, expect the socket to be bound to localhost. Change SocketUtils to attempt to bind to localhost.
1 parent 8273edd commit 6e7f818

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-integration-test/src/main/java/org/springframework/integration/test/util/SocketUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.integration.test.util;
1717

1818
import java.net.DatagramSocket;
19+
import java.net.InetAddress;
1920
import java.net.ServerSocket;
2021
import java.util.ArrayList;
2122
import java.util.List;
@@ -103,7 +104,8 @@ public static List<Integer> findAvailableServerSockets(int seed, int numberOfReq
103104

104105
for (int i = seed; i < seed+200; i++) {
105106
try {
106-
ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i);
107+
ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i, 1,
108+
InetAddress.getByName("localhost"));
107109
sock.close();
108110
openPorts.add(i);
109111

@@ -166,7 +168,7 @@ public static List<Integer> findAvailableUdpSockets(int seed, int numberOfReques
166168

167169
for (int i = seed; i < seed+200; i++) {
168170
try {
169-
DatagramSocket sock = new DatagramSocket(i);
171+
DatagramSocket sock = new DatagramSocket(i, InetAddress.getByName("localhost"));
170172
sock.close();
171173
Thread.sleep(100);
172174

spring-integration-test/src/test/java/org/springframework/integration/test/util/SocketUtilsTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,20 @@
1515
*/
1616
package org.springframework.integration.test.util;
1717

18-
import org.junit.Assert;
18+
import static org.junit.Assert.assertNotEquals;
19+
20+
import java.net.DatagramSocket;
21+
import java.net.InetAddress;
22+
import java.net.ServerSocket;
23+
24+
import javax.net.ServerSocketFactory;
1925

26+
import org.junit.Assert;
2027
import org.junit.Test;
2128

2229
/**
2330
* @author Gunnar Hillert
31+
* @author Gary Russell
2432
*/
2533
public class SocketUtilsTests {
2634

@@ -54,4 +62,21 @@ public void testFindAvailableUdpSocketWithNegativeSeedPort() {
5462
Assert.fail("Expected an IllegalArgumentException to be thrown.");
5563
}
5664

65+
@Test
66+
public void testTcpLocalhost() throws Exception {
67+
int available = SocketUtils.findAvailableServerSocket();
68+
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(available, 1,
69+
InetAddress.getByName("localhost"));
70+
assertNotEquals(available, SocketUtils.findAvailableServerSocket(available));
71+
ss.close();
72+
}
73+
74+
@Test
75+
public void testUdpLocalhost() throws Exception {
76+
int available = SocketUtils.findAvailableUdpSocket(2000);
77+
DatagramSocket dgs = new DatagramSocket(available, InetAddress.getByName("localhost"));
78+
assertNotEquals(available, SocketUtils.findAvailableUdpSocket(available));
79+
dgs.close();
80+
}
81+
5782
}

0 commit comments

Comments
 (0)