use of java.net.ServerSocket in project hadoop by apache.
the class TestServer method testBindError.
@Test
public void testBindError() throws Exception {
Configuration conf = new Configuration();
ServerSocket socket = new ServerSocket();
InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
socket.bind(address);
try {
int min = socket.getLocalPort();
conf.set("TestRange", min + "-" + min);
ServerSocket socket2 = new ServerSocket();
InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
boolean caught = false;
try {
Server.bind(socket2, address2, 10, conf, "TestRange");
} catch (BindException e) {
caught = true;
} finally {
socket2.close();
}
assertTrue("Failed to catch the expected bind exception", caught);
} finally {
socket.close();
}
}
use of java.net.ServerSocket in project hadoop by apache.
the class AuthenticatorTestCase method getLocalPort.
protected int getLocalPort() throws Exception {
ServerSocket ss = new ServerSocket(0);
int ret = ss.getLocalPort();
ss.close();
return ret;
}
use of java.net.ServerSocket in project flink by apache.
the class ConnectionUtilsTest method testReturnLocalHostAddressUsingHeuristics.
@Test
public void testReturnLocalHostAddressUsingHeuristics() throws Exception {
try (ServerSocket blocker = new ServerSocket(0, 1, InetAddress.getLocalHost())) {
// the "blocker" server socket simply does not accept connections
// this address is consequently "unreachable"
InetSocketAddress unreachable = new InetSocketAddress("localhost", blocker.getLocalPort());
final long start = System.nanoTime();
InetAddress add = ConnectionUtils.findConnectingAddress(unreachable, 2000, 400);
// check that it did not take forever (max 30 seconds)
// this check can unfortunately not be too tight, or it will be flaky on some CI infrastructure
assertTrue(System.nanoTime() - start < 30_000_000_000L);
// we should have found a heuristic address
assertNotNull(add);
// make sure that we returned the InetAddress.getLocalHost as a heuristic
assertEquals(InetAddress.getLocalHost(), add);
}
}
use of java.net.ServerSocket in project flink by apache.
the class ConnectionUtilsTest method testFindConnectingAddressWhenGetLocalHostThrows.
@Test
public void testFindConnectingAddressWhenGetLocalHostThrows() throws Exception {
PowerMockito.mockStatic(InetAddress.class);
Mockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()).thenCallRealMethod();
final InetAddress loopbackAddress = Inet4Address.getByName("127.0.0.1");
Thread socketServerThread;
try (ServerSocket socket = new ServerSocket(0, 1, loopbackAddress)) {
// Make sure that the thread will eventually die even if something else goes wrong
socket.setSoTimeout(10_000);
socketServerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
socket.accept();
} catch (IOException e) {
// ignore
}
}
});
socketServerThread.start();
final InetSocketAddress socketAddress = new InetSocketAddress(loopbackAddress, socket.getLocalPort());
final InetAddress address = ConnectionUtils.findConnectingAddress(socketAddress, 2000, 400);
PowerMockito.verifyStatic();
// Make sure we got an address via alternative means
assertNotNull(address);
}
}
use of java.net.ServerSocket in project flink by apache.
the class IPv6HostnamesITCase method getLocalIPv6Address.
private Inet6Address getLocalIPv6Address() {
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface netInterface = e.nextElement();
// for each address of the network interface
Enumeration<InetAddress> ee = netInterface.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress addr = ee.nextElement();
if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) {
// see if it is possible to bind to the address
InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
try {
log.info("Considering address " + addr);
// test whether we can bind a socket to that address
log.info("Testing whether sockets can bind to " + addr);
ServerSocket sock = new ServerSocket();
sock.bind(socketAddress);
sock.close();
// test whether Akka's netty can bind to the address
log.info("Testing whether Akka can use " + addr);
int port = NetUtils.getAvailablePort();
ActorSystem as = AkkaUtils.createActorSystem(new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port)));
as.shutdown();
log.info("Using address " + addr);
return (Inet6Address) addr;
} catch (IOException ignored) {
// fall through the loop
}
}
}
}
return null;
} catch (Exception e) {
return null;
}
}
Aggregations