Search in sources :

Example 11 with Socket

use of java.net.Socket 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 12 with Socket

use of java.net.Socket 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)

Example 13 with Socket

use of java.net.Socket 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 14 with Socket

use of java.net.Socket 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 15 with Socket

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

the class TestingFailingBlobServer method run.

@Override
public void run() {
    // we do properly the first operation (PUT)
    try {
        new BlobServerConnection(getServerSocket().accept(), this).start();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    // do some failing operations
    for (int num = 0; num < numFailures && !isShutdown(); num++) {
        Socket socket = null;
        try {
            socket = getServerSocket().accept();
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            // just abort everything
            is.close();
            os.close();
            socket.close();
        } catch (IOException e) {
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (Throwable t) {
                }
            }
        }
    }
    // regular runs
    super.run();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1553 IOException (java.io.IOException)664 ServerSocket (java.net.ServerSocket)548 OutputStream (java.io.OutputStream)364 InetSocketAddress (java.net.InetSocketAddress)350 Test (org.junit.Test)348 InputStream (java.io.InputStream)249 InputStreamReader (java.io.InputStreamReader)170 BufferedReader (java.io.BufferedReader)151 SocketException (java.net.SocketException)132 SSLSocket (javax.net.ssl.SSLSocket)103 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)84 ConnectException (java.net.ConnectException)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)75 InetAddress (java.net.InetAddress)73 OutputStreamWriter (java.io.OutputStreamWriter)70 ServletOutputStream (javax.servlet.ServletOutputStream)68 DataOutputStream (java.io.DataOutputStream)67 CountDownLatch (java.util.concurrent.CountDownLatch)64