CounterFactory implements Initialisable { public synchronized Counter getCounter(final String id) { Counter counter = _id2counter.get(id); if (counter == null) { counter = new Counter(id); _id2counter.put(id, counter); } return counter; } private  CounterFactory(); static CounterFactory getInstance(); synchronized void close(); synchronized void destroy(); synchronized Counter getCounter(final String id); synchronized void removeCounter(final String id); @Override void initialise(final DataAccessLayerFactory factory);  }
@Test public void createCounterTest() throws Exception { for (String id : _test_ids) { final Counter c = _counter_factory.getCounter(id); assertNotNull(c); assertEquals(0l, c.current()); } }
@Test public void updateSingleTest() throws Exception { for (int i = 0; i < _test_ids.size(); i++) { Counter c = _counter_factory.getCounter(_test_ids.get(i)); Client client = new Client(c, _test_numOfIncrements.get(i)); client.start(); client.join(); assertEquals((int) _test_numOfIncrements.get(i), client .getCollectedValues().size()); assertEquals((int) _test_numOfIncrements.get(i), c.current()); } }
@Test public void updateMultiTest() throws InterruptedException { for (int i = 0; i < _test_ids.size(); i++) { Counter c = _counter_factory.getCounter(_test_ids.get(i)); List<Client> clients = new LinkedList<Client>(); for (int j = 0; j < NUM_OF_CLIENTS; j++) { Client client = new Client(c, _test_numOfIncrements.get(i)); client.start(); clients.add(client); } for (Client client : clients) { client.join(); } Set<Long> collectedValues = new HashSet<Long>(); for (Client client : clients) { collectedValues.addAll(client.getCollectedValues()); assertEquals((int) _test_numOfIncrements.get(i), client .getCollectedValues().size()); } assertEquals(_test_numOfIncrements.get(i) * NUM_OF_CLIENTS, collectedValues.size()); } }