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);
}
Aggregations