From 48197f6f40030861dd0cec56a8446e94c533a8a5 Mon Sep 17 00:00:00 2001 From: "nikolce.mihajlovski" Date: Sat, 25 Apr 2015 21:55:02 +0200 Subject: [PATCH] Added more TCP client/server tests. --- .../test/java/org/rapidoid/TcpClientTest.java | 82 ++++++++++++++----- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/rapidoid-net/src/test/java/org/rapidoid/TcpClientTest.java b/rapidoid-net/src/test/java/org/rapidoid/TcpClientTest.java index ce6055e372..c2fdc5a3a2 100644 --- a/rapidoid-net/src/test/java/org/rapidoid/TcpClientTest.java +++ b/rapidoid-net/src/test/java/org/rapidoid/TcpClientTest.java @@ -36,38 +36,80 @@ @Since("2.0.0") public class TcpClientTest extends NetTestCommons { + private final Protocol UPPERCASE_SERVER = new Protocol() { + @Override + public void process(Channel ctx) { + if (ctx.isInitial()) { + return; + } + ctx.writeln(ctx.readln().toUpperCase()); + } + }; + + private final Protocol HI_CLIENT = new Protocol() { + @Override + public void process(Channel ctx) { + if (ctx.isInitial()) { + ctx.writeln("Hi there!"); + return; + } + + eq(ctx.readln(), "HI THERE!"); + } + }; + @Test - public void testTCPClient() { + public void testTCPClientWithDefaultConnections() { Log.setLogLevel(LogLevel.DEBUG); - TCPClient client = TCP.client().host("localhost").port(8080).connections(1).protocol(new Protocol() { - @Override - public void process(Channel ctx) { - if (ctx.isInitial()) { - ctx.writeln("Hi there!"); - return; - } + TCPClient client = TCP.client().host("localhost").port(8080).connections(1).protocol(HI_CLIENT).build().start(); - eq(ctx.readln(), "HI THERE!"); - } - }).build().start(); + // let the clients wait + UTILS.sleep(3000); + + TCPServer server = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start(); UTILS.sleep(3000); - TCPServer server = TCP.server().port(8080).protocol(new Protocol() { - @Override - public void process(Channel ctx) { - if (ctx.isInitial()) { - return; - } - ctx.writeln(ctx.readln().toUpperCase()); - } - }).build().start(); + client.shutdown(); + server.shutdown(); + } + + @Test + public void testTCPClientWithCustomConnections() { + Log.setLogLevel(LogLevel.DEBUG); + TCPClient client = TCP.client().build().start(); + client.connect("localhost", 8080, HI_CLIENT, 1); + + // let the clients wait + UTILS.sleep(3000); + + TCPServer server = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start(); UTILS.sleep(3000); client.shutdown(); server.shutdown(); } + @Test + public void testTCPClientWithDefaultAndCustomConnections() { + Log.setLogLevel(LogLevel.DEBUG); + + TCPClient client = TCP.client().host("127.0.0.1").port(8080).connections(3).protocol(HI_CLIENT).build().start(); + client.connect("localhost", 9090, HI_CLIENT, 1); + + // let the clients wait + UTILS.sleep(3000); + + TCPServer server1 = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start(); + TCPServer server2 = TCP.server().port(9090).protocol(UPPERCASE_SERVER).build().start(); + + UTILS.sleep(3000); + + client.shutdown(); + server1.shutdown(); + server2.shutdown(); + } + }