Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 79335 invoked from network); 2 Jun 2004 20:59:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 2 Jun 2004 20:59:32 -0000 Received: (qmail 3413 invoked by uid 500); 2 Jun 2004 20:59:43 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 3303 invoked by uid 500); 2 Jun 2004 20:59:42 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 3289 invoked by uid 500); 2 Jun 2004 20:59:42 -0000 Received: (qmail 3284 invoked by uid 99); 2 Jun 2004 20:59:42 -0000 Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Wed, 02 Jun 2004 13:59:42 -0700 Received: (qmail 79230 invoked by uid 1773); 2 Jun 2004 20:59:23 -0000 Date: 2 Jun 2004 20:59:23 -0000 Message-ID: <20040602205923.79229.qmail@minotaur.apache.org> From: ozeigermann@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory TransactionalMapWrapper.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N ozeigermann 2004/06/02 13:59:23 Modified: transaction/src/test/org/apache/commons/transaction/memory MapWrapperTest.java transaction/src/java/org/apache/commons/transaction/memory TransactionalMapWrapper.java Log: More tests for map wrapper. *This is work in progress* Revision Changes Path 1.2 +91 -47 jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/memory/MapWrapperTest.java Index: MapWrapperTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/test/org/apache/commons/transaction/memory/MapWrapperTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MapWrapperTest.java 2 Jun 2004 14:58:01 -0000 1.1 +++ MapWrapperTest.java 2 Jun 2004 20:59:23 -0000 1.2 @@ -31,23 +31,30 @@ import org.apache.commons.transaction.util.Jdk14Logger; import org.apache.commons.transaction.util.LoggerFacade; +import org.apache.commons.transaction.util.RendezvousBarrier; /** - * Tests for FileResourceManager. + * Tests for map wrapper. * * @author Oliver Zeigermann */ public class MapWrapperTest extends TestCase { - private static final Logger logger = Logger.getLogger(MapWrapperTest.class.getName()); - private static final LoggerFacade sLogger = new Jdk14Logger(logger); + private static final Logger logger = + Logger.getLogger(MapWrapperTest.class.getName()); + private static final LoggerFacade sLogger = new Jdk14Logger(logger); private static final long BARRIER_TIMEOUT = 2000; // XXX need this, as JUnit seems to print only part of these strings private static void report(String should, String is) { if (!should.equals(is)) { - fail("\nWrong output:\n'" + is + "'\nShould be:\n'" + should + "'\n"); + fail( + "\nWrong output:\n'" + + is + + "'\nShould be:\n'" + + should + + "'\n"); } } @@ -65,51 +72,88 @@ } public void testBasic() throws Throwable { - final Map map1 = new HashMap(); - - final TransactionalMapWrapper txMap1 = new TransactionalMapWrapper(map1); - - // make sure changes are propagated to wrapped map outside tx - txMap1.put("key1", "value1"); - report("value1", (String) map1.get("key1")); - - // make sure changes are progated to wrapped map only after commit - txMap1.startTransaction(); - txMap1.put("key1", "value2"); - report("value1", (String) map1.get("key1")); - report("value2", (String) txMap1.get("key1")); - txMap1.commitTransaction(); - report("value2", (String) map1.get("key1")); - report("value2", (String) txMap1.get("key1")); - // make sure changes are reverted after rollback - txMap1.startTransaction(); - txMap1.put("key1", "value3"); - txMap1.rollbackTransaction(); - report("value2", (String) map1.get("key1")); - report("value2", (String) txMap1.get("key1")); + logger.info("Checking basic transaction features"); + + final Map map1 = new HashMap(); + + final TransactionalMapWrapper txMap1 = + new TransactionalMapWrapper(map1); + + // make sure changes are propagated to wrapped map outside tx + txMap1.put("key1", "value1"); + report("value1", (String) map1.get("key1")); + + // make sure changes are progated to wrapped map only after commit + txMap1.startTransaction(); + txMap1.put("key1", "value2"); + report("value1", (String) map1.get("key1")); + report("value2", (String) txMap1.get("key1")); + txMap1.commitTransaction(); + report("value2", (String) map1.get("key1")); + report("value2", (String) txMap1.get("key1")); + + // make sure changes are reverted after rollback + txMap1.startTransaction(); + txMap1.put("key1", "value3"); + txMap1.rollbackTransaction(); + report("value2", (String) map1.get("key1")); + report("value2", (String) txMap1.get("key1")); } - public void xxxtestMulti() throws Throwable { - final Map map1 = new HashMap(); - final Map map2 = new HashMap(); - - final TransactionalMapWrapper txMap1 = new TransactionalMapWrapper(map1); - final TransactionalMapWrapper txMap2 = new TransactionalMapWrapper(map2); - - Thread thread1 = new Thread(new Runnable() { - public void run() { - } - }, "Thread1"); + public void testMulti() throws Throwable { + logger.info("Checking concurrent transaction features"); + + final Map map1 = new HashMap(); + + final TransactionalMapWrapper txMap1 = + new TransactionalMapWrapper(map1); + + final RendezvousBarrier commitBarrier = + new RendezvousBarrier("Commit", 2, BARRIER_TIMEOUT, sLogger); + final RendezvousBarrier startBarrier = + new RendezvousBarrier("Start", 2, BARRIER_TIMEOUT, sLogger); + + Thread thread1 = new Thread(new Runnable() { + public void run() { + txMap1.startTransaction(); + try { + txMap1.put("key1", "value2"); + startBarrier.meet(); + txMap1.commitTransaction(); + commitBarrier.meet(); + } catch (InterruptedException e) { + logger.log(Level.WARNING, "Thread interrupted", e); + startBarrier.reset(); + commitBarrier.reset(); + } + } + }, "Thread1"); - // make sure changes are propagated to wrapped map outside tx txMap1.put("key1", "value1"); - String value = (String) map1.get("key1"); - assertEquals(value, "value1"); - - // make sure changes are progated to wrapped map only after commit - txMap1.startTransaction(); + + thread1.start(); + + txMap1.startTransaction(); + startBarrier.meet(); + // before commit of other thread + report("value1", (String) txMap1.get("key1")); + commitBarrier.meet(); + // we have read committed as isolation level, that's why I will see the new value of the other thread now + report("value2", (String) txMap1.get("key1")); + // now when I override it it should of course be my value again + txMap1.put("key1", "value3"); + report("value3", (String) txMap1.get("key1")); - } + // after rollback it must be the value written by the other thread again + txMap1.rollbackTransaction(); + report("value2", (String) txMap1.get("key1")); + } + + public void testTxControl() throws Throwable { + logger.info("Checking advanced transaction control (heavily used in JCA implementation)"); + + } + } 1.9 +7 -4 jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java Index: TransactionalMapWrapper.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/TransactionalMapWrapper.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- TransactionalMapWrapper.java 2 Jun 2004 14:11:57 -0000 1.8 +++ TransactionalMapWrapper.java 2 Jun 2004 20:59:23 -0000 1.9 @@ -40,6 +40,9 @@ *
* Caution: Do not modify values retrieved by {@link #get(Object)} as this will circumvent the transactional mechanism. * Rather clone the value or copy it in a way you see fit and store it back using {@link #put(Object, Object)}. + *
+ * Note: This wrapper guarantees isolation level READ COMMITTED only. I.e. as soon a value + * is committed in one transaction it will be immediately visible in all other concurrent transactions. * * @author Oliver Zeigermann * @version $Revision$ --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org