You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jo...@apache.org on 2009/06/17 11:26:02 UTC
svn commit: r785523 -
/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java
Author: joehni
Date: Wed Jun 17 09:26:01 2009
New Revision: 785523
URL: http://svn.apache.org/viewvc?rev=785523&view=rev
Log:
Reproduce COLLECTIONS-3.
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java?rev=785523&r1=785522&r2=785523&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java Wed Jun 17 09:26:01 2009
@@ -17,6 +17,7 @@
package org.apache.commons.collections.map;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -453,6 +454,249 @@
fail();
} catch (IndexOutOfBoundsException ex) {}
}
+
+ public void testSynchronizedRemoveFromEntrySet() throws InterruptedException {
+
+ final Map map = new LRUMap(10000);
+
+ final Map exceptions = new HashMap();
+ final ThreadGroup tg = new ThreadGroup(getName()) {
+ public void uncaughtException(Thread t, Throwable e) {
+ exceptions.put(e, t.getName());
+ super.uncaughtException(t, e);
+ }
+ };
+
+ final int[] counter = new int[1];
+ counter[0] = 0;
+ final Thread[] threads = new Thread[50];
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i] = new Thread(tg, "JUnit Thread " + i) {
+
+ public void run() {
+ int i = 0;
+ try {
+ synchronized (this) {
+ notifyAll();
+ wait();
+ }
+ Thread thread = Thread.currentThread();
+ while (i < 1000 && !interrupted()) {
+ synchronized (map) {
+ map.put(thread.getName() + "[" + ++i + "]", thread);
+ }
+ }
+ synchronized (map) {
+ for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry)iter.next();
+ if (entry.getValue() == this) {
+ iter.remove();
+ }
+ }
+ }
+ } catch (InterruptedException e) {
+ fail("Unexpected InterruptedException");
+ }
+ if (i > 0) {
+ synchronized (counter) {
+ counter[0]++;
+ }
+ }
+ }
+
+ };
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].start();
+ threads[i].wait();
+ }
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].notifyAll();
+ }
+ }
+
+ Thread.sleep(1000);
+
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i].interrupt();
+ }
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].join();
+ }
+ }
+
+ assertEquals("Exceptions have been thrown: " + exceptions, 0, exceptions.size());
+ assertTrue("Each thread should have put at least 1 element into the map, but only "
+ + counter[0] + " did succeed", counter[0] >= threads.length);
+ }
+
+ // TODO: COLLECTIONS-3
+ public void todoTestSynchronizedRemoveFromKeySet() throws InterruptedException {
+
+ final Map map = new LRUMap(10000);
+
+ final Map exceptions = new HashMap();
+ final ThreadGroup tg = new ThreadGroup(getName()) {
+ public void uncaughtException(Thread t, Throwable e) {
+ exceptions.put(e, t.getName());
+ super.uncaughtException(t, e);
+ }
+ };
+
+ final int[] counter = new int[1];
+ counter[0] = 0;
+ final Thread[] threads = new Thread[50];
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i] = new Thread(tg, "JUnit Thread " + i) {
+
+ public void run() {
+ int i = 0;
+ try {
+ synchronized (this) {
+ notifyAll();
+ wait();
+ }
+ Thread thread = Thread.currentThread();
+ while (i < 1000 && !interrupted()) {
+ synchronized (map) {
+ map.put(thread.getName() + "[" + ++i + "]", thread);
+ }
+ }
+ synchronized (map) {
+ for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
+ String name = (String)iter.next();
+ if (map.get(name) == this) {
+ iter.remove();
+ }
+ }
+ }
+ } catch (InterruptedException e) {
+ fail("Unexpected InterruptedException");
+ }
+ if (i > 0) {
+ synchronized (counter) {
+ counter[0]++;
+ }
+ }
+ }
+
+ };
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].start();
+ threads[i].wait();
+ }
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].notifyAll();
+ }
+ }
+
+ Thread.sleep(1000);
+
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i].interrupt();
+ }
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].join();
+ }
+ }
+
+ assertEquals("Exceptions have been thrown: " + exceptions, 0, exceptions.size());
+ assertTrue("Each thread should have put at least 1 element into the map, but only "
+ + counter[0] + " did succeed", counter[0] >= threads.length);
+ }
+
+ public void testSynchronizedRemoveFromValues() throws InterruptedException {
+
+ final Map map = new LRUMap(10000);
+
+ final Map exceptions = new HashMap();
+ final ThreadGroup tg = new ThreadGroup(getName()) {
+ public void uncaughtException(Thread t, Throwable e) {
+ exceptions.put(e, t.getName());
+ super.uncaughtException(t, e);
+ }
+ };
+
+ final int[] counter = new int[1];
+ counter[0] = 0;
+ final Thread[] threads = new Thread[50];
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i] = new Thread(tg, "JUnit Thread " + i) {
+
+ public void run() {
+ int i = 0;
+ try {
+ synchronized (this) {
+ notifyAll();
+ wait();
+ }
+ Thread thread = Thread.currentThread();
+ while (i < 1000 && !interrupted()) {
+ synchronized (map) {
+ map.put(thread.getName() + "[" + ++i + "]", thread);
+ }
+ }
+ synchronized (map) {
+ for (Iterator iter = map.values().iterator(); iter.hasNext();) {
+ if (iter.next() == this) {
+ iter.remove();
+ }
+ }
+ }
+ } catch (InterruptedException e) {
+ fail("Unexpected InterruptedException");
+ }
+ if (i > 0) {
+ synchronized (counter) {
+ counter[0]++;
+ }
+ }
+ }
+
+ };
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].start();
+ threads[i].wait();
+ }
+ }
+
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].notifyAll();
+ }
+ }
+
+ Thread.sleep(1000);
+
+ for (int i = 0; i < threads.length; ++i) {
+ threads[i].interrupt();
+ }
+ for (int i = 0; i < threads.length; ++i) {
+ synchronized (threads[i]) {
+ threads[i].join();
+ }
+ }
+
+ assertEquals("Exceptions have been thrown: " + exceptions, 0, exceptions.size());
+ assertTrue("Each thread should have put at least 1 element into the map, but only "
+ + counter[0] + " did succeed", counter[0] >= threads.length);
+ }
// public void testCreate() throws Exception {
// resetEmpty();