Search in sources :

Example 36 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project robovm by robovm.

the class ConcurrentModTest method testAdd.

/*
     * Test method for 'java.util.AbstractList.subList(int, int)'
     */
public void testAdd() {
    AbstractList al = new ArrayList();
    Double one = new Double(1.0);
    Double two = new Double(2.0);
    Double three = new Double(3.0);
    Double four = new Double(4.0);
    al.add(one);
    al.add(two);
    al.add(three);
    al.add(four);
    List sub = al.subList(1, 3);
    assertEquals(2, sub.size());
    // the sub.get(1) is 3.0
    assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0);
    assertTrue(((Double) sub.get(1)).doubleValue() > 2.0);
    // remove the 2.0
    al.remove(1);
    try {
        // illegal call the subList's method Add(int,Object).
        sub.add(1, two);
        fail("It should throws ConcurrentModificationException.");
    } catch (ConcurrentModificationException e) {
        return;
    }
}
Also used : AbstractList(java.util.AbstractList) ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) List(java.util.List) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList)

Example 37 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project cdap by caskdata.

the class MessageCacheTest method testAddError.

@Test
public void testAddError() throws Exception {
    // Test to verify various error situations are being safeguarded
    final MessageCache<Integer> cache = new MessageCache<>(new IntComparator(), new UnitWeigher<Integer>(), new MessageCache.Limits(5, 7, 10), NOOP_METRICS);
    // 1. Adding out of order should result in error
    try {
        cache.addAll(Arrays.asList(5, 2, 3, 4).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 2. Adding entries that are smaller than the largest one in the cache
    cache.addAll(Arrays.asList(5, 6, 7, 8).iterator());
    try {
        cache.addAll(Arrays.asList(1, 2, 3, 4).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 3. Adding duplicate entry
    try {
        cache.addAll(Arrays.asList(1, 1).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 4. Adding entry that is already exist in the cache
    cache.addAll(Arrays.asList(1, 2).iterator());
    try {
        cache.addAll(Arrays.asList(2, 3).iterator());
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 5. Multiple threads calling addAll at the same time
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch produceLatch = new CountDownLatch(1);
    // Starts the the first thread and block inside the addAll call
    new Thread() {

        @Override
        public void run() {
            cache.addAll(new AbstractIterator<Integer>() {

                private boolean produced;

                @Override
                protected Integer computeNext() {
                    try {
                        barrier.await();
                    } catch (Exception e) {
                    // This shouldn't happen
                    }
                    Uninterruptibles.awaitUninterruptibly(produceLatch);
                    if (!produced) {
                        produced = true;
                        return 1;
                    }
                    return endOfData();
                }
            });
        }
    }.start();
    // Starts the second thread that tries to call addAll after the first thread is blocked inside the addAll call
    final BlockingQueue<Exception> exception = new ArrayBlockingQueue<>(1);
    new Thread() {

        @Override
        public void run() {
            try {
                barrier.await();
            } catch (Exception e) {
            // This shouldn't happen
            }
            try {
                cache.addAll(Arrays.asList(1, 2, 3).iterator());
            } catch (Exception e) {
                exception.add(e);
            }
        }
    }.start();
    Exception e = exception.poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(e);
    Assert.assertTrue(e instanceof ConcurrentModificationException);
    // Unblock the first thread
    produceLatch.countDown();
    final MessageFilter<Integer> filter = MessageFilter.alwaysAccept();
    Tasks.waitFor(Collections.singletonList(1), new Callable<List<Integer>>() {

        @Override
        public List<Integer> call() throws Exception {
            try (MessageCache.Scanner<Integer> scanner = cache.scan(0, true, 10, filter)) {
                return Lists.newArrayList(scanner);
            }
        }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentModificationException(java.util.ConcurrentModificationException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) AbstractIterator(com.google.common.collect.AbstractIterator) Test(org.junit.Test)

Example 38 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project CorfuDB by CorfuDB.

the class SMRMap method forEach.

/**
     * Performs the given action for each entry in this map until all entries
     * have been processed or the action throws an exception.   Unless
     * otherwise specified by the implementing class, actions are performed in
     * the order of entry set iteration (if an iteration order is specified.)
     * Exceptions thrown by the action are relayed to the caller.
     *
     * @param action The action to be performed for each entry
     * @throws NullPointerException            if the specified action is null
     * @throws ConcurrentModificationException if an entry is found to be
     *                                         removed during iteration
     * @implSpec The default implementation is equivalent to, for this {@code map}:
     * <pre> {@code
     * for (Map.Entry<K, V> entry : map.entrySet())
     *     action.accept(entry.getKey(), entry.getValue());
     * }</pre>
     * <p>
     * The default implementation makes no guarantees about synchronization
     * or atomicity properties of this method. Any implementation providing
     * atomicity guarantees must override this method and document its
     * concurrency properties.
     * @since 1.8
     */
@Override
@TransactionalMethod
public void forEach(BiConsumer<? super K, ? super V> action) {
    Objects.requireNonNull(action);
    for (Map.Entry<K, V> entry : entrySet()) {
        K k;
        V v;
        try {
            k = entry.getKey();
            v = entry.getValue();
        } catch (IllegalStateException ise) {
            // this usually means the entry is no longer in the map.
            throw new ConcurrentModificationException(ise);
        }
        action.accept(k, v);
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) TransactionalMethod(org.corfudb.annotations.TransactionalMethod)

Example 39 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project webservices-axiom by apache.

the class AbstractNodeIterator method computeNext.

private void computeNext(Axis axis) {
    CoreNode node = currentNode;
    if (node instanceof CoreChildNode && ((CoreChildNode) node).coreGetParent() != currentParent) {
        throw new ConcurrentModificationException("The current node has been removed using a method other than Iterator#remove()");
    }
    try {
        while (true) {
            // Get to the next node
            switch(axis) {
                case CHILDREN:
                    if (node == null) {
                        node = startNode.coreGetFirstChild();
                    } else {
                        node = ((CoreChildNode) node).coreGetNextSibling();
                    }
                    break;
                case DESCENDANTS:
                case DESCENDANTS_OR_SELF:
                    if (node == null) {
                        if (axis == Axis.DESCENDANTS) {
                            node = startNode.coreGetFirstChild();
                            depth++;
                        } else {
                            node = startNode;
                        }
                    } else {
                        boolean visitChildren = true;
                        while (true) {
                            if (visitChildren && node instanceof CoreParentNode && semantics.isParentNode(node.coreGetNodeType())) {
                                CoreChildNode firstChild = ((CoreParentNode) node).coreGetFirstChild();
                                if (firstChild != null) {
                                    depth++;
                                    node = firstChild;
                                    break;
                                }
                            }
                            if (depth == 0) {
                                node = null;
                                break;
                            }
                            CoreChildNode nextSibling = ((CoreChildNode) node).coreGetNextSibling();
                            if (nextSibling != null) {
                                node = nextSibling;
                                break;
                            }
                            depth--;
                            node = ((CoreChildNode) node).coreGetParent();
                            visitChildren = false;
                        }
                    }
            }
            if (node == null) {
                nextNode = null;
                break;
            }
            if (type.isInstance(node)) {
                T candidate = type.cast(node);
                if (matches(candidate)) {
                    nextNode = candidate;
                    break;
                }
            }
        }
    } catch (CoreModelException ex) {
        throw semantics.toUncheckedException(ex);
    }
    hasNext = true;
}
Also used : CoreParentNode(org.apache.axiom.core.CoreParentNode) ConcurrentModificationException(java.util.ConcurrentModificationException) CoreModelException(org.apache.axiom.core.CoreModelException) CoreChildNode(org.apache.axiom.core.CoreChildNode) CoreNode(org.apache.axiom.core.CoreNode)

Example 40 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project webservices-axiom by apache.

the class TestGetChildElementsConcurrentModification method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement parent = factory.createOMElement("parent", null);
    factory.createOMElement("child1", null, parent);
    factory.createOMElement("child2", null, parent);
    factory.createOMElement("child3", null, parent);
    Iterator<OMElement> it = parent.getChildElements();
    it.next();
    OMElement child2 = it.next();
    child2.detach();
    try {
        it.next();
        fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) ConcurrentModificationException(java.util.ConcurrentModificationException) OMElement(org.apache.axiom.om.OMElement)

Aggregations

ConcurrentModificationException (java.util.ConcurrentModificationException)98 Iterator (java.util.Iterator)24 HashSet (java.util.HashSet)19 Set (java.util.Set)18 ResultSet (java.sql.ResultSet)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 GameLocal (org.apache.openejb.test.entity.cmr.manytomany.GameLocal)8 PlatformLocal (org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal)8 ArtistLocal (org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal)8 SongLocal (org.apache.openejb.test.entity.cmr.onetomany.SongLocal)8 List (java.util.List)7 Map (java.util.Map)6 AbstractList (java.util.AbstractList)5 Collection (java.util.Collection)5 NoSuchElementException (java.util.NoSuchElementException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 MultiMapRecord (com.hazelcast.multimap.impl.MultiMapRecord)3