Skip to content

Commit d2398ac

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 d2398ac

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
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+
InetAddress ifAddress = InetAddress.getByName("localhost");
108+
ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i, 1, ifAddress);
107109
sock.close();
108110
openPorts.add(i);
109111

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

Lines changed: 18 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,19 @@
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.InetAddress;
21+
import java.net.ServerSocket;
1922

23+
import javax.net.ServerSocketFactory;
24+
25+
import org.junit.Assert;
2026
import org.junit.Test;
2127

2228
/**
2329
* @author Gunnar Hillert
30+
* @author Gary Russell
2431
*/
2532
public class SocketUtilsTests {
2633

@@ -54,4 +61,13 @@ public void testFindAvailableUdpSocketWithNegativeSeedPort() {
5461
Assert.fail("Expected an IllegalArgumentException to be thrown.");
5562
}
5663

64+
@Test
65+
public void testLocalhost() throws Exception {
66+
int available = SocketUtils.findAvailableServerSocket();
67+
InetAddress ifAddress = InetAddress.getByName("localhost");
68+
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(available, 1, ifAddress);
69+
assertNotEquals(available, SocketUtils.findAvailableServerSocket(available));
70+
ss.close();
71+
}
72+
5773
}

0 commit comments

Comments
 (0)