volatile boolean fail = false;
Random r = new Random();
@Test
public void testAccumulatorConcurrent() throws Exception {
final ValueAccumulator ac = new ValueAccumulator();
ExecutorService executor = Executors.newFixedThreadPool(10);
Future []jobs = new Future[10];
for (int i = 0; i <10; i++) {
jobs[i] = executor.submit(new Runnable() {
@Override
public void run() {
ac.initializeAllCounters();
long val = 0;
while (!Thread.interrupted()) {
ac.addToCounter(AccumulatorName.UnitTestTime, 1);
long newVal = ac.getCounter(AccumulatorName.UnitTestTime);
if (newVal != val + 1) {
log.error("Should have incremented by 1. Expoected val:" + (val + 1) + " instead got:" + newVal);
fail = true;
fail("Should have incremented by 1. Expoected val:" + (val + 1) + " instead got:" + newVal);
}