Package net.engio.mbassy.common

Examples of net.engio.mbassy.common.IConcurrentSet


    @Test
    public void testIteratorCleanup() {

        // Assemble
        final HashSet<Object> permanentElements = new HashSet<Object>();
        final IConcurrentSet testSetWeak = createSet();
        final Random rand = new Random();

        for (int i = 0; i < numberOfElements; i++) {
            Object candidate = new Object();

            if (rand.nextInt() % 3 == 0) {
                permanentElements.add(candidate);
            }
            testSetWeak.add(candidate);
        }

        // Remove/Garbage collect all objects that have not
        // been inserted into the set of permanent candidates.
        runGC();

        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object testObject : testSetWeak) {
                    // do nothing
                    // just iterate to trigger automatic clean up
                    System.currentTimeMillis();
                }
            }
        }, numberOfThreads);

        // the set should have cleaned up the garbage collected elements
        // it must still contain all of the permanent objects
        // since different GC mechanisms can be used (not necessarily full, stop-the-world) not all dead objects
        // must have been collected
        assertTrue(permanentElements.size() <= testSetWeak.size() && testSetWeak.size() < numberOfElements);
        for (Object test : testSetWeak) {
            assertTrue(permanentElements.contains(test));
        }
    }
View Full Code Here


    @Test
    public void testUniqueness() {
        final LinkedList<Object> duplicates = new LinkedList<Object>();
        final HashSet<Object> distinct = new HashSet<Object>();

        final IConcurrentSet testSet = createSet();
        Random rand = new Random();

        // getAll set of distinct objects and list of duplicates
        Object candidate = new Object();
        for (int i = 0; i < numberOfElements; i++) {
            if (rand.nextInt() % 3 == 0) {
                candidate = new Object();
            }
            duplicates.add(candidate);
            distinct.add(candidate);
        }

        // insert all elements (containing duplicates) into the set
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object src : duplicates) {
                    testSet.add(src);
                }
            }
        }, numberOfThreads);

        // check that the control set and the test set contain the exact same elements
        assertEquals(distinct.size(), testSet.size());
        for (Object uniqueObject : distinct) {
            assertTrue(testSet.contains(uniqueObject));
        }
    }
View Full Code Here

    @Test
    public void testRandomRemoval() {
        final HashSet<Object> source = new HashSet<Object>();
        final HashSet<Object> toRemove = new HashSet<Object>();

        final IConcurrentSet testSet = createSet();
        // getAll set of distinct objects and mark a subset of those for removal
        for (int i = 0; i < numberOfElements; i++) {
            Object candidate = new Object();
            source.add(candidate);
            if (i % 3 == 0) {
                toRemove.add(candidate);
            }
        }

        // getAll the test set from the set of candidates
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object src : source) {
                    testSet.add(src);
                }
            }
        }, numberOfThreads);

        // remove all candidates that have previously been marked for removal from the test set
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object src : toRemove) {
                    testSet.remove(src);
                }
            }
        }, numberOfThreads);

        // ensure that the test set does not contain any of the elements that have been removed from it
        for (Object tar : testSet) {
            Assert.assertTrue(!toRemove.contains(tar));
        }
        // ensure that the test set still contains all objects from the source set that have not been marked
        // for removal
        assertEquals(source.size() - toRemove.size(), testSet.size());
        for (Object src : source) {
            if (!toRemove.contains(src)) assertTrue(testSet.contains(src));
        }
    }
View Full Code Here

    @Test
    public void testRemovalOfHead() {
        final HashSet<Object> source = new HashSet<Object>();
        final HashSet<Object> toRemove = new HashSet<Object>();

        final IConcurrentSet testSet = createSet();
        // getAll set of candidates and mark subset for removal
        for (int i = 0; i < numberOfElements; i++) {
            Object candidate = new Object();
            source.add(candidate);
            if (i % 3 == 0) {
                toRemove.add(candidate);
            }
        }

        // getAll test set by adding the candidates
        // and subsequently removing those marked for removal
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object src : source) {
                    testSet.add(src);
                    if (toRemove.contains(src))
                        testSet.remove(src);
                }
            }
        }, numberOfThreads);

        // ensure that the test set does not contain any of the elements that have been removed from it
        for (Object tar : testSet) {
            Assert.assertTrue(!toRemove.contains(tar));
        }
        // ensure that the test set still contains all objects from the source set that have not been marked
        // for removal
        assertEquals(source.size() - toRemove.size(), testSet.size());
        for (Object src : source) {
            if (!toRemove.contains(src)) assertTrue(testSet.contains(src));
        }
    }
View Full Code Here

    }

    @Test
    public void testCompleteRemoval() {
        final HashSet<Object> source = new HashSet<Object>();
        final IConcurrentSet testSet = createSet();

        // getAll set of candidates and mark subset for removal
        for (int i = 0; i < numberOfElements; i++) {
            Object candidate = new Object();
            source.add(candidate);
            testSet.add(candidate);
        }

        // getAll test set by adding the candidates
        // and subsequently removing those marked for removal
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                for (Object src : source) {
                    testSet.remove(src);
                }
            }
        }, numberOfThreads);


        // ensure that the test set still contains all objects from the source set that have not been marked
        // for removal
        assertEquals(0, testSet.size());
        for(Object src : source){
            assertFalse(testSet.contains(src));
        }
    }
View Full Code Here

    }

    @Test
    public void testRemovalViaIterator() {
        final HashSet<Object> source = new HashSet<Object>();
        final IConcurrentSet setUnderTest = createSet();

        // getAll set of candidates and mark subset for removal
        for (int i = 0; i < numberOfElements; i++) {
            Object candidate = new Object();
            source.add(candidate);
            setUnderTest.add(candidate);
        }

        // getAll test set by adding the candidates
        // and subsequently removing those marked for removal
        ConcurrentExecutor.runConcurrent(new Runnable() {
            @Override
            public void run() {
                Iterator<Object> iterator = setUnderTest.iterator();
                while(iterator.hasNext()){
                    iterator.remove();
                }
            }
        }, numberOfThreads);


        // ensure that the test set still contains all objects from the source set that have not been marked
        // for removal
        assertEquals(0, setUnderTest.size());
        for(Object src : source){
            assertFalse(setUnderTest.contains(src));
        }
    }
View Full Code Here

     *
     * Thanks to Ivan Koblik (http://koblik.blogspot.com) for contributing initial code and idea
     */
    @Test
    public void testConcurrentAddRemove() {
        final IConcurrentSet testSet = createSet();
        // a set of unique integers that will stay permanently in the test set
        final List permanentObjects = new ArrayList();
        // a set of objects that will be added and removed at random to the test set to force rehashing
        final List volatileObjects = new ArrayList();
        permanentObjects.addAll(createWithRandomIntegers(80, null));
        volatileObjects.addAll(createWithRandomIntegers(10000, permanentObjects));
        final CopyOnWriteArraySet missing = new CopyOnWriteArraySet();
        final int mutatorThreshold = 1000;

        // Add elements that will not be touched by the constantly running mutating thread
        for (Object permanent : permanentObjects) {
            testSet.add(permanent);
        }

        // Adds and removes items
        // thus forcing constant rehashing of the backing hashtable
        Runnable rehasher = new Runnable() {
            public void run() {
                Random rand = new Random();
                for(int times = 0; times < 1000 ; times++){
                    HashSet elements = new HashSet(mutatorThreshold);

                    for (int i = 0; i < mutatorThreshold; i++) {
                        Object volatileObject = volatileObjects.get(Math.abs(rand.nextInt()) % volatileObjects.size());
                        testSet.add(volatileObject);
                        elements.add(volatileObject);
                    }
                    for (Object volObj : elements) {
                        testSet.remove(volObj);
                    }
                }
            };
        };

        Runnable lookup = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    for (Object permanent : permanentObjects) {
                        // permanent items are never touched,
                        // --> set.contains(j) should always return true
                        if(!testSet.contains(permanent))
                            missing.add(permanent);
                    }
                }
            }
        };
View Full Code Here

TOP

Related Classes of net.engio.mbassy.common.IConcurrentSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.