Search in sources :

Example 1 with RequestResponseNioChannel

use of suite.net.nio.NioChannelFactory.RequestResponseNioChannel in project suite by stupidsing.

the class NioDispatcherTest method testRequestResponse.

@Test
public void testRequestResponse() throws IOException, InterruptedException {
    RequestResponseMatcher matcher = new RequestResponseMatcher();
    ThreadPoolExecutor executor = Thread_.newExecutor();
    Iterate<Bytes> handler = request -> request;
    NioDispatcher<RequestResponseNioChannel> dispatcher = new NioDispatcherImpl<>(() -> NioChannelFactory.requestResponse(new RequestResponseNioChannel(), matcher, executor, handler));
    dispatcher.start();
    try (Closeable closeServer = dispatcher.listen(5151)) {
        InetAddress localHost = InetAddress.getLocalHost();
        InetSocketAddress address = new InetSocketAddress(localHost, 5151);
        RequestResponseNioChannel client = dispatcher.connect(address);
        for (String s : new String[] { "ABC", "WXYZ", "" }) {
            byte[] bs = s.getBytes(Constants.charset);
            Bytes request = Bytes.of(bs);
            Bytes response = matcher.requestForResponse(client, request);
            assertEquals(request, response);
            System.out.println("Request '" + s + "' is okay");
        }
    } finally {
        dispatcher.stop();
    }
    executor.awaitTermination(0, TimeUnit.SECONDS);
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Socket(java.net.Socket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Constants(suite.Constants) Source(suite.util.FunUtil.Source) Bytes(suite.primitive.Bytes) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) Iterate(suite.util.FunUtil.Iterate) Charset(java.nio.charset.Charset) Closeable(java.io.Closeable) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) InetSocketAddress(java.net.InetSocketAddress) Closeable(java.io.Closeable) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) Bytes(suite.primitive.Bytes) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Charset (java.nio.charset.Charset)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 Constants (suite.Constants)1 BufferedNioChannel (suite.net.nio.NioChannelFactory.BufferedNioChannel)1 NioChannel (suite.net.nio.NioChannelFactory.NioChannel)1 RequestResponseNioChannel (suite.net.nio.NioChannelFactory.RequestResponseNioChannel)1 Bytes (suite.primitive.Bytes)1