More lenient socket binding in LDAP tests (#39864)

The LDAP tests attempt to bind all interfaces,
but if for some reason an interface can't be bound
the tests will stall until the suite times out.

This modifies the tests to be a bit more lenient and allow
some binding to fail so long as at least one succeeds.
This allows the test to continue even in more antagonistic
environments.
This commit is contained in:
Michael Basnight 2019-03-12 11:00:02 -05:00 committed by Zachary Tong
parent ba40230c7f
commit 8c78fc096d
2 changed files with 24 additions and 1 deletions

View File

@ -19,4 +19,12 @@ public class InetAddressHelper {
public static InetAddress[] getAllAddresses() throws SocketException { public static InetAddress[] getAllAddresses() throws SocketException {
return NetworkUtils.getAllAddresses(); return NetworkUtils.getAllAddresses();
} }
public static InetAddress[] filterIPV4(InetAddress[] addresses){
return NetworkUtils.filterIPV4(addresses);
}
public static InetAddress[] filterIPV6(InetAddress[] addresses){
return NetworkUtils.filterIPV6(addresses);
}
} }

View File

@ -31,6 +31,8 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.NoRouteToHostException; import java.net.NoRouteToHostException;
@ -322,7 +324,13 @@ public class SessionFactoryLoadBalancingTests extends LdapTestCase {
try { try {
final boolean allSocketsOpened = awaitBusy(() -> { final boolean allSocketsOpened = awaitBusy(() -> {
try { try {
final List<InetAddress> inetAddressesToBind = Arrays.stream(InetAddressHelper.getAllAddresses()) InetAddress[] allAddresses = InetAddressHelper.getAllAddresses();
if (serverAddress instanceof Inet4Address) {
allAddresses = InetAddressHelper.filterIPV4(allAddresses);
} else {
allAddresses = InetAddressHelper.filterIPV6(allAddresses);
}
final List<InetAddress> inetAddressesToBind = Arrays.stream(allAddresses)
.filter(addr -> openedSockets.stream().noneMatch(s -> addr.equals(s.getLocalAddress()))) .filter(addr -> openedSockets.stream().noneMatch(s -> addr.equals(s.getLocalAddress())))
.filter(addr -> blacklistedAddress.contains(addr) == false) .filter(addr -> blacklistedAddress.contains(addr) == false)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -334,8 +342,15 @@ public class SessionFactoryLoadBalancingTests extends LdapTestCase {
} catch (NoRouteToHostException e) { } catch (NoRouteToHostException e) {
logger.debug(new ParameterizedMessage("blacklisting address [{}] due to:", localAddress), e); logger.debug(new ParameterizedMessage("blacklisting address [{}] due to:", localAddress), e);
blacklistedAddress.add(localAddress); blacklistedAddress.add(localAddress);
} catch (ConnectException e) {
logger.debug(new ParameterizedMessage("blacklisting address [{}] due to:", localAddress), e);
blacklistedAddress.add(localAddress);
} }
} }
if (openedSockets.size() == 0) {
logger.debug("Could not open any sockets from the available addresses");
return false;
}
return true; return true;
} catch (IOException e) { } catch (IOException e) {
logger.debug(new ParameterizedMessage("caught exception while opening socket on [{}]", portToBind), e); logger.debug(new ParameterizedMessage("caught exception while opening socket on [{}]", portToBind), e);