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);
}
use of java.util.ConcurrentModificationException in project dsl-devkit by dsldevkit.
the class AbstractRecursiveScope method getSingleElement.
/**
* {@inheritDoc}
*/
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
// NOPMD NPathComplexity
if (name == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("Null name in getContentByName");
}
try {
if (DEBUG && !invocationTrace.add(name)) {
throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(name)));
}
// NOPMD UseLocaleWithCaseConversions
final QualifiedName lookupName = isIgnoreCase() ? name.toLowerCase() : name;
IEObjectDescription result = null;
result = nameCache.get(lookupName);
if (result != null) {
// NOPMD CompareObjectsWithEquals
return result == NULL_DESCRIPTION ? null : result;
}
if (contentsIterator == null) {
contentsIterator = getAllLocalElements().iterator();
}
while (result == null && contentsIterator.hasNext()) {
result = contentsIterator.next();
QualifiedName thisName = result.getName();
if (isIgnoreCase()) {
// NOPMD UseLocaleWithCaseConversions
thisName = thisName.toLowerCase();
}
if (!isIgnoreCase() || nameCache.get(thisName) == null) {
nameCache.put(thisName, result);
}
if (!lookupName.equals(thisName)) {
// NOPMD NullAssignment
result = null;
}
}
if (result == null) {
result = getParent().getSingleElement(name);
// Possibly cache this result, too. For the time being, let the outer scope use its own cache, otherwise we'll duplicate
// a lot.
}
if (result == null) {
nameCache.put(lookupName, NULL_DESCRIPTION);
// } else {
// ScopeTrace.addTrace(result, getId());
}
return result;
} catch (ConcurrentModificationException e) {
// cache invalidated: retry
reset();
// Remove name from invocation trace, otherwise the intended retry recursive call will fail
if (DEBUG) {
invocationTrace.remove(name);
}
return getSingleElement(name);
} finally {
if (DEBUG) {
invocationTrace.remove(name);
}
}
}
use of java.util.ConcurrentModificationException in project moleculer-java by moleculer-java.
the class TcpReader method disconnect.
public void disconnect() {
// Close selector thread
if (executor != null) {
try {
executor.shutdownNow();
} catch (Exception ignored) {
}
executor = null;
}
// Close server socket
if (serverChannel != null) {
try {
serverChannel.close();
} catch (Exception ignored) {
}
serverChannel = null;
}
// Close selector
if (selector != null) {
HashSet<SelectionKey> keys = new HashSet<>();
for (int i = 0; i < 5; i++) {
try {
keys.addAll(selector.keys());
break;
} catch (ConcurrentModificationException ignored) {
}
}
for (SelectionKey key : keys) {
close(key, null);
}
try {
selector.close();
} catch (Exception ignored) {
}
selector = null;
}
}
use of java.util.ConcurrentModificationException in project dyn4j by dyn4j.
the class BodyIterator method remove.
/* (non-Javadoc)
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
if (this.index < 0) {
throw new IllegalStateException();
}
if (this.index >= this.world.getBodyCount()) {
throw new IndexOutOfBoundsException();
}
try {
this.world.removeBody(this.index);
this.index--;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
use of java.util.ConcurrentModificationException in project dyn4j by dyn4j.
the class BodyIterator method next.
/* (non-Javadoc)
* @see java.util.Iterator#next()
*/
@Override
public Body next() {
if (this.index >= this.world.getBodyCount()) {
throw new IndexOutOfBoundsException();
}
try {
this.index++;
Body body = this.world.getBody(this.index);
return body;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
Aggregations