use of org.apache.geode.internal.statistics.LocalStatListener in project geode by apache.
the class MemoryThresholdsDUnitTest method testLocalStatListenerRegistration.
/**
* putting this test here because junit does not have host stat sampler enabled
*/
@Test
public void testLocalStatListenerRegistration() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Cache cache = getCache();
InternalDistributedSystem internalSystem = (InternalDistributedSystem) cache.getDistributedSystem();
final GemFireStatSampler sampler = internalSystem.getStatSampler();
// fix: remove infinite wait
sampler.waitForInitialization(10000);
final LocalStatListener l = new LocalStatListener() {
public void statValueChanged(double value) {
latch.countDown();
}
};
final String tenuredPoolName = HeapMemoryMonitor.getTenuredMemoryPoolMXBean().getName();
LogWriterUtils.getLogWriter().info("TenuredPoolName:" + tenuredPoolName);
final List list = internalSystem.getStatsList();
assertFalse(list.isEmpty());
// fix: found race condition here...
WaitCriterion wc = new WaitCriterion() {
public boolean done() {
int i = 0;
synchronized (list) {
for (Object o : list) {
LogWriterUtils.getLogWriter().info("List:" + (++i) + ":" + o);
if (o instanceof StatisticsImpl) {
StatisticsImpl si = (StatisticsImpl) o;
LogWriterUtils.getLogWriter().info("stat:" + si.getTextId());
if (si.getTextId().contains(tenuredPoolName)) {
sampler.addLocalStatListener(l, si, "currentUsedMemory");
return true;
}
}
}
}
return false;
}
public String description() {
return "Waiting for " + tenuredPoolName + " statistics to be added to create listener for";
}
};
Wait.waitForCriterion(wc, 5000, 10, true);
assertTrue("expected at least one stat listener, found " + sampler.getLocalListeners().size(), sampler.getLocalListeners().size() > 0);
long maxTenuredMemory = HeapMemoryMonitor.getTenuredPoolMaxMemory();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
Region r = createRegion(getUniqueName() + "region", factory.create());
// keep putting objects (of size 1% of maxTenuredMemory) and wait for stat callback
// if we don't get a callback after 75 attempts, throw exception
int count = 0;
while (true) {
count++;
if (count > 75) {
throw new AssertionError("Did not receive a stat listener callback");
}
byte[] value = new byte[(int) (maxTenuredMemory * 0.01)];
r.put("key-" + count, value);
if (latch.await(50, TimeUnit.MILLISECONDS)) {
break;
} else {
continue;
}
}
r.close();
}
Aggregations