use of org.nustaq.net.TCPObjectServer in project kontraktor by RuedigerMoeller.
the class AsyncServerSocketTest method serialSync.
@Test
public void serialSync() throws Exception {
AtomicInteger COUNT = new AtomicInteger(0);
TCPObjectServer tcpObjectServer = new TCPObjectServer(8082);
RateMeasure measure = new RateMeasure("count");
tcpObjectServer.start((client) -> {
try {
while (true) {
Object request = client.readObject();
if (request == null)
// connection closed
return;
COUNT.incrementAndGet();
measure.count();
}
} catch (EOFException eof) {
// e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
});
Thread.sleep(1000);
HashMap testMap = new HashMap();
testMap.put("pok", 13);
testMap.put("yes", "no");
testMap.put("true", "maybe");
ExecutorService executorService = Executors.newCachedThreadPool();
int MSG_COUNT = 10_000_000;
int NUM_CLIENTS = 100;
for (int ii = 0; ii < NUM_CLIENTS; ii++) {
final int finalIi = ii;
executorService.execute(() -> {
TCPObjectSocket sock = null;
try {
sock = new TCPObjectSocket("localhost", 8082);
// System.out.println("start "+finalIi);
for (int i = 0; i < MSG_COUNT / NUM_CLIENTS; i++) {
sock.writeObject(testMap);
sock.flush();
}
sock.close();
// System.out.println("finished "+ finalIi);
} catch (Exception e) {
e.printStackTrace();
}
});
}
Thread.sleep(3000);
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.DAYS);
int to = 0;
while (to < 7 && COUNT.get() < MSG_COUNT) {
Thread.sleep(1000);
System.out.println("COUNT " + COUNT.get());
to++;
}
Assert.assertTrue(COUNT.get() == MSG_COUNT);
}
Aggregations