@Test
public void testConcurrentIncrement() throws Exception {
final MetricsTable table = getTable("testConcurrentIncrement");
final int rounds = 500;
Map<byte[], Long> inc1 = ImmutableMap.of(X, 1L, Y, 2L);
Map<byte[], Long> inc2 = ImmutableMap.of(Y, 1L, Z, 2L);
Collection<? extends Thread> threads = ImmutableList.of(new IncThread(table, A, inc1, rounds),
new IncThread(table, A, inc2, rounds),
new IncAndGetThread(table, A, Z, 5, rounds),
new IncAndGetThread(table, A, Z, 2, rounds));
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
if (t instanceof Closeable) {
((Closeable) t).close();
}
}
Assert.assertEquals(rounds + 10L, table.incrementAndGet(A, X, 10L));
Assert.assertEquals(3 * rounds - 20L, table.incrementAndGet(A, Y, -20L));
Assert.assertEquals(9 * rounds, table.incrementAndGet(A, Z, 0L));
}