Search in sources :

Example 6 with Visitor

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);
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) File(java.io.File) Test(org.junit.Test)

Example 7 with Visitor

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());
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) WeakReference(java.lang.ref.WeakReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

Visitor (org.apache.cassandra.utils.concurrent.Ref.Visitor)7 Test (org.junit.Test)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 File (java.io.File)1 WeakReference (java.lang.ref.WeakReference)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1