Search in sources :

Example 11 with ServerSocket

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);
    }
}
Also used : ServerSocket(java.net.ServerSocket) OutputStreamWriter(java.io.OutputStreamWriter) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 12 with ServerSocket

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);
    }
}
Also used : ServerSocket(java.net.ServerSocket) OutputStreamWriter(java.io.OutputStreamWriter) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 13 with ServerSocket

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;
}
Also used : ServerSocket(java.net.ServerSocket)

Example 14 with ServerSocket

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);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with ServerSocket

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);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ServerSocket (java.net.ServerSocket)655 IOException (java.io.IOException)306 Socket (java.net.Socket)254 InetSocketAddress (java.net.InetSocketAddress)126 Test (org.junit.Test)94 SocketException (java.net.SocketException)54 InputStream (java.io.InputStream)48 SocketTimeoutException (java.net.SocketTimeoutException)42 OutputStream (java.io.OutputStream)40 InetAddress (java.net.InetAddress)39 BindException (java.net.BindException)27 URL (java.net.URL)26 InputStreamReader (java.io.InputStreamReader)23 File (java.io.File)22 UnknownHostException (java.net.UnknownHostException)22 SSLSocket (javax.net.ssl.SSLSocket)21 BufferedReader (java.io.BufferedReader)20 SSLServerSocket (javax.net.ssl.SSLServerSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)15 SocketChannel (java.nio.channels.SocketChannel)14