Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 70479 invoked from network); 17 Jun 2009 09:26:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 17 Jun 2009 09:26:23 -0000 Received: (qmail 36963 invoked by uid 500); 17 Jun 2009 09:26:29 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 36885 invoked by uid 500); 17 Jun 2009 09:26:29 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 36867 invoked by uid 99); 17 Jun 2009 09:26:27 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Jun 2009 09:26:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Jun 2009 09:26:23 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A1A5023888D4; Wed, 17 Jun 2009 09:26:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r785523 - /commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestLRUMap.java Date: Wed, 17 Jun 2009 09:26:02 -0000 To: commits@commons.apache.org From: joehni@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090617092602.A1A5023888D4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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();