* selector to process all the OP events.
*/
@Test
public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool("Server", 1);
final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool, null);
server.setFilters(new MyCodec(), new Handler());
server.bind(0);
// warm up
Thread.sleep(100);
long t0 = System.currentTimeMillis();
final int port = server.getServerSocketChannel().socket().getLocalPort();
final Socket[] clients = new Socket[CLIENT_COUNT];
// connect some clients
for (int i = 0; i < CLIENT_COUNT; i++) {
// System.out.println("Creation client 2 " + i);
clients[i] = new Socket("127.0.0.1", port);
}
// does the session open message was fired ?
assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// write some messages
for (int i = 0; i < CLIENT_COUNT; i++) {
clients[i].getOutputStream().write(("test:" + i).getBytes());
clients[i].getOutputStream().flush();
}
// test is message was received by the server
assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// does response was wrote and sent ?
assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// read the echos
final byte[] buffer = new byte[1024];
for (int i = 0; i < CLIENT_COUNT; i++) {
final int bytes = clients[i].getInputStream().read(buffer);
final String text = new String(buffer, 0, bytes);
assertEquals("test:" + i, text);
}
// close the session
assertEquals(CLIENT_COUNT, closedLatch.getCount());
for (int i = 0; i < CLIENT_COUNT; i++) {
clients[i].close();
}
// does the session close event was fired ?
assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
long t1 = System.currentTimeMillis();
System.out.println("Delta = " + (t1 - t0));
server.unbind();
}