You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oz...@apache.org on 2004/06/02 22:59:23 UTC

cvs commit: jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory TransactionalMapWrapper.java

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 <a href="mailto:ozeigermann@apache.org">Oliver Zeigermann</a>
    */
   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 @@
    * <br>
    * <em>Caution:</em> 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)}.
  + * <br>
  + * <em>Note:</em> This wrapper guarantees isolation level <code>READ COMMITTED</code> only. I.e. as soon a value
  + * is committed in one transaction it will be immediately visible in all other concurrent transactions.
    * 
    * @author <a href="mailto:ozeigermann@apache.org">Oliver Zeigermann</a>
    * @version $Revision$
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org