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.

Polishing
This commit is contained in:
Gary Russell
2015-08-05 11:39:14 -04:00
committed by Artem Bilan
parent 57e2fcfbca
commit 7f504efa1b
2 changed files with 51 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,12 +15,20 @@
*/
package org.springframework.integration.test.util;
import org.junit.Assert;
import static org.junit.Assert.assertNotEquals;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import javax.net.ServerSocketFactory;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Gunnar Hillert
* @author Gary Russell
*/
public class SocketUtilsTests {
@@ -54,4 +62,21 @@ public class SocketUtilsTests {
Assert.fail("Expected an IllegalArgumentException to be thrown.");
}
@Test
public void testTcpLocalhost() throws Exception {
int available = SocketUtils.findAvailableServerSocket();
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(available, 1,
InetAddress.getByName("localhost"));
assertNotEquals(available, SocketUtils.findAvailableServerSocket(available));
ss.close();
}
@Test
public void testUdpLocalhost() throws Exception {
int available = SocketUtils.findAvailableUdpSocket(2000);
DatagramSocket dgs = new DatagramSocket(available, InetAddress.getByName("localhost"));
assertNotEquals(available, SocketUtils.findAvailableUdpSocket(available));
dgs.close();
}
}