use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testArray.
@Test
public void testArray() throws Exception {
final Object[] objects = new Object[entryCount];
for (int i = 0; i < entryCount; i += 2) objects[i] = new Object();
File f = File.createTempFile("foo", "bar");
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = objects;
//Checking we don't get an infinite loop out of traversing file refs
File fileRef = f;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<Object[]>(objects), tidier);
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("Array visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations);
//Should iterate the elements in the array and get a unique object from every other one
Assert.assertTrue(visitor.lastVisitedCount > (entryCount / 2) && visitor.lastVisitedCount < (entryCount / 2) + fudgeFactor);
//Should iterate over the array touching roughly the same number of objects as entries
Assert.assertTrue(visitor.iterations > (entryCount / 2) && visitor.iterations < (entryCount / 2) + fudgeFactor);
}
use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testWeakRef.
//Make sure a weak ref is ignored by the visitor looking for strong ref leaks
@Test
public void testWeakRef() throws Exception {
AtomicReference dontRefMe = new AtomicReference();
WeakReference<Object> weakRef = new WeakReference(dontRefMe);
RefCounted.Tidy tidier = new RefCounted.Tidy() {
WeakReference<Object> ref = weakRef;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(dontRefMe, tidier);
dontRefMe.set(ref);
Visitor visitor = new Visitor();
visitor.haveLoops = new HashSet<>();
visitor.run();
ref.close();
Assert.assertTrue(visitor.haveLoops.isEmpty());
}
Aggregations