Search in sources :

Example 6 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class TaskManagerStartupTest method testStartupWhenTaskmanagerActorPortIsUsed.

/**
	 * Tests that the TaskManager fails synchronously when the actor system port
	 * is in use.
	 * 
	 * @throws Throwable
	 */
@Test(expected = BindException.class)
public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException {
    ServerSocket blocker = null;
    try {
        final String localHostName = "localhost";
        final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress());
        // block some port
        blocker = new ServerSocket(0, 50, localBindAddress);
        final int port = blocker.getLocalPort();
        TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class);
        fail("This should fail with an IOException");
    } catch (IOException e) {
        // expected. validate the error message
        List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>());
        for (Throwable cause : causes) {
            if (cause instanceof BindException) {
                throw (BindException) cause;
            }
        }
        fail("This should fail with an exception caused by BindException");
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (blocker != null) {
            try {
                blocker.close();
            } catch (IOException e) {
            // no need to log here
            }
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) IOException(java.io.IOException) BindException(java.net.BindException) Test(org.junit.Test)

Example 7 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class TaskManagerStartupTest method testStartupWhenNetworkStackFailsToInitialize.

/**
	 * Tests that the task manager start-up fails if the network stack cannot be initialized.
	 * @throws Exception
	 */
@Test(expected = IOException.class)
public void testStartupWhenNetworkStackFailsToInitialize() throws Exception {
    ServerSocket blocker = null;
    try {
        blocker = new ServerSocket(0, 50, InetAddress.getByName("localhost"));
        final Configuration cfg = new Configuration();
        cfg.setString(ConfigConstants.TASK_MANAGER_HOSTNAME_KEY, "localhost");
        cfg.setInteger(ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, blocker.getLocalPort());
        cfg.setInteger(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, 1);
        TaskManager.startTaskManagerComponentsAndActor(cfg, ResourceID.generate(), null, "localhost", Option.<String>empty(), Option.<LeaderRetrievalService>empty(), false, TaskManager.class);
    } finally {
        if (blocker != null) {
            try {
                blocker.close();
            } catch (IOException e) {
            // ignore, best effort
            }
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class SocketClientSinkTest method testSocketSink.

@Test
public void testSocketSink() throws Exception {
    final ServerSocket server = new ServerSocket(0);
    final int port = server.getLocalPort();
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    Thread sinkRunner = new Thread("Test sink runner") {

        @Override
        public void run() {
            try {
                SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0);
                simpleSink.open(new Configuration());
                simpleSink.invoke(TEST_MESSAGE + '\n');
                simpleSink.close();
            } catch (Throwable t) {
                error.set(t);
            }
        }
    };
    sinkRunner.start();
    Socket sk = server.accept();
    BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    String value = rdr.readLine();
    sinkRunner.join();
    server.close();
    if (error.get() != null) {
        Throwable t = error.get();
        t.printStackTrace();
        fail("Error in spawned thread: " + t.getMessage());
    }
    assertEquals(TEST_MESSAGE, value);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 9 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class SocketClientSinkTest method testSinkAutoFlush.

@Test
public void testSinkAutoFlush() throws Exception {
    final ServerSocket server = new ServerSocket(0);
    final int port = server.getLocalPort();
    final SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0, true);
    simpleSink.open(new Configuration());
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    Thread sinkRunner = new Thread("Test sink runner") {

        @Override
        public void run() {
            try {
                // need two messages here: send a fin to cancel the client state:FIN_WAIT_2 while the server is CLOSE_WAIT
                simpleSink.invoke(TEST_MESSAGE + '\n');
            } catch (Throwable t) {
                error.set(t);
            }
        }
    };
    sinkRunner.start();
    Socket sk = server.accept();
    BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    String value = rdr.readLine();
    sinkRunner.join();
    simpleSink.close();
    server.close();
    if (error.get() != null) {
        Throwable t = error.get();
        t.printStackTrace();
        fail("Error in spawned thread: " + t.getMessage());
    }
    assertEquals(TEST_MESSAGE, value);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 10 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class SocketTextStreamFunctionTest method testSocketSourceOutputInfiniteRetries.

@Test
public void testSocketSourceOutputInfiniteRetries() throws Exception {
    ServerSocket server = new ServerSocket(0);
    Socket channel = null;
    try {
        SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", -1, 100);
        SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
        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("test1\n");
        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("check\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)

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