use of org.apache.geode.StatisticsTypeFactory in project geode by apache.
the class AtomicStatsJUnitTest method testConcurrentGets.
/**
* Test for bug 41340. Do two gets at the same time of a dirty stat, and make sure we get the
* correct value for the stat.
*
* @throws Throwable
*/
@Test
public void testConcurrentGets() throws Throwable {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
// props.setProperty("statistic-sample-rate", "60000");
props.setProperty(STATISTIC_SAMPLING_ENABLED, "false");
DistributedSystem ds = DistributedSystem.connect(props);
String statName = "TestStats";
String statDescription = "Tests stats";
final String statDesc = "blah blah blah";
StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton();
StatisticsType type = f.createType(statName, statDescription, new StatisticDescriptor[] { f.createIntGauge("stat", statDesc, "bottles of beer on the wall") });
final int statId = type.nameToId("stat");
try {
final AtomicReference<Statistics> statsRef = new AtomicReference<Statistics>();
final CyclicBarrier beforeIncrement = new CyclicBarrier(3);
final CyclicBarrier afterIncrement = new CyclicBarrier(3);
Thread thread1 = new Thread("thread1") {
public void run() {
try {
while (true) {
beforeIncrement.await();
statsRef.get().incInt(statId, 1);
afterIncrement.await();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thread thread3 = new Thread("thread1") {
public void run() {
try {
while (true) {
beforeIncrement.await();
afterIncrement.await();
statsRef.get().getInt(statId);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread1.start();
thread3.start();
for (int i = 0; i < 5000; i++) {
Statistics stats = ds.createAtomicStatistics(type, "stats");
statsRef.set(stats);
beforeIncrement.await();
afterIncrement.await();
assertEquals("On loop " + i, 1, stats.getInt(statId));
stats.close();
}
} finally {
ds.disconnect();
}
}
Aggregations