use of java.net.ServerSocket in project flink by apache.
the class SocketTextStreamFunctionTest method testSocketSourceOutputAcrossRetries.
@Test
public void testSocketSourceOutputAcrossRetries() throws Exception {
ServerSocket server = new ServerSocket(0);
Socket channel = null;
try {
SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100);
SocketSourceThread runner = new SocketSourceThread(source, "test1", "check1", "check2");
runner.start();
// first connection: nothing
channel = server.accept();
channel.close();
// second connection: first string
channel = server.accept();
OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("te");
writer.close();
channel.close();
// third connection: nothing
channel = server.accept();
channel.close();
// forth connection: second string
channel = server.accept();
writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("st1\n");
writer.write("check1\n");
writer.write("check2\n");
writer.flush();
runner.waitForNumElements(2);
runner.cancel();
runner.waitUntilDone();
} finally {
if (channel != null) {
IOUtils.closeQuietly(channel);
}
IOUtils.closeQuietly(server);
}
}
use of java.net.ServerSocket in project flink by apache.
the class SocketTextStreamFunctionTest method testSocketSourceSimpleOutput.
@Test
public void testSocketSourceSimpleOutput() throws Exception {
ServerSocket server = new ServerSocket(0);
Socket channel = null;
try {
SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0);
SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
runner.start();
channel = server.accept();
OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("test1\n");
writer.write("check\n");
writer.flush();
runner.waitForNumElements(2);
runner.cancel();
runner.interrupt();
runner.waitUntilDone();
channel.close();
} finally {
if (channel != null) {
IOUtils.closeQuietly(channel);
}
IOUtils.closeQuietly(server);
}
}
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);
}
}
Aggregations