Skip to content

Commit

Permalink
Added more TCP client/server tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Apr 25, 2015
1 parent e70a3cc commit 48197f6
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions rapidoid-net/src/test/java/org/rapidoid/TcpClientTest.java
Expand Up @@ -36,38 +36,80 @@
@Since("2.0.0") @Since("2.0.0")
public class TcpClientTest extends NetTestCommons { 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 @Test
public void testTCPClient() { public void testTCPClientWithDefaultConnections() {
Log.setLogLevel(LogLevel.DEBUG); Log.setLogLevel(LogLevel.DEBUG);


TCPClient client = TCP.client().host("localhost").port(8080).connections(1).protocol(new Protocol() { TCPClient client = TCP.client().host("localhost").port(8080).connections(1).protocol(HI_CLIENT).build().start();
@Override
public void process(Channel ctx) {
if (ctx.isInitial()) {
ctx.writeln("Hi there!");
return;
}


eq(ctx.readln(), "HI THERE!"); // let the clients wait
} UTILS.sleep(3000);
}).build().start();
TCPServer server = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start();


UTILS.sleep(3000); UTILS.sleep(3000);


TCPServer server = TCP.server().port(8080).protocol(new Protocol() { client.shutdown();
@Override server.shutdown();
public void process(Channel ctx) { }
if (ctx.isInitial()) {
return; @Test
} public void testTCPClientWithCustomConnections() {
ctx.writeln(ctx.readln().toUpperCase()); Log.setLogLevel(LogLevel.DEBUG);
}
}).build().start();


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); UTILS.sleep(3000);


client.shutdown(); client.shutdown();
server.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();
}

} }

0 comments on commit 48197f6

Please sign in to comment.