Inspired by the code above, I created ( GoogleCode project )
It includes a simple unit test:
SeriServer server = new SeriServer(6001, nthreads); final SeriClient client[] = new SeriClient[nclients]; //write the data with multiple threads to flood the server for (int cnt = 0; cnt < nclients; cnt++) { final int counterVal = cnt; client[cnt] = new SeriClient("localhost", 6001); Thread t = new Thread(new Runnable() { public void run() { try { for (int cnt2 = 0; cnt2 < nsends; cnt2++) { String msg = "[" + counterVal + "]"; client[counterVal].send(msg); } } catch (IOException e) { e.printStackTrace(); fail(); } } }); t.start(); } HashMap<String, Integer> counts = new HashMap<String, Integer>(); int nullCounts = 0; for (int cnt = 0; cnt < nsends * nclients;) { //read the data from a vector (that the server pool automatically fills SeriDataPackage data = server.read(); if (data == null) { nullCounts++; System.out.println("NULL"); continue; } if (counts.containsKey(data.getObject())) { Integer c = counts.get(data.getObject()); counts.put((String) data.getObject(), c + 1); } else { counts.put((String) data.getObject(), 1); } cnt++; System.out.println("Received: " + data.getObject()); } // asserts the results Collection<Integer> values = counts.values(); for (Integer value : values) { int ivalue = value; assertEquals(nsends, ivalue); System.out.println(value); } assertEquals(counts.size(), nclients); System.out.println(counts.size()); System.out.println("Finishing"); server.shutdown();
source share