[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/optimistic ...

Ben Wang bwang at jboss.com
Mon Dec 25 22:41:11 EST 2006


  User: bwang   
  Date: 06/12/25 22:41:11

  Added:       tests-50/functional/org/jboss/cache/pojo/optimistic  
                        LocalTxTest.java AbstractOptimisticTestCase.java
  Log:
  Test case for optimistic locking
  
  Revision  Changes    Path
  1.1      date: 2006/12/26 03:41:11;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/optimistic/LocalTxTest.java
  
  Index: LocalTxTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.pojo.optimistic;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.TestingUtil;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.InitialContext;
  import javax.transaction.UserTransaction;
  import javax.transaction.SystemException;
  import javax.transaction.NotSupportedException;
  import javax.transaction.Transaction;
  import javax.transaction.RollbackException;
  import java.util.Properties;
  import java.util.List;
  import java.util.ArrayList;
  
  /**
   */
  public class LocalTxTest extends AbstractOptimisticTestCase
  {
     Log log = LogFactory.getLog(LocalTxTest.class);
     PojoCache cache;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     DummyTransactionManager tx_mgr;
     Throwable t1_ex, t2_ex;
     long start = 0;
  
  
     public LocalTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
  
        cache = createCache();
  
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  
        tx_mgr = DummyTransactionManager.getInstance();
        t1_ex = t2_ex = null;
     }
  
     protected void tearDown()
     {
        super.tearDown();
        cache.stop();
  
        DummyTransactionManager.destroy();
     }
  
  //   public void testDummy() {}
  
     UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
     {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.cache.transaction.DummyContextFactory");
        return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
     }
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        cache.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test1", "Harald Gliebe", 32);
        tx.commit();
        tx.begin();
        p.setName("Benoit");
        tx.commit();
        Person p1 = (Person) cache.find("/person/test1");
        assertEquals("Benoit", p.getName());
        assertEquals("Benoit", p1.getName());
        tx.begin();
        p1.setAge(61);
        tx.commit();
        assertEquals(61, p.getAge());
        assertEquals(61, p1.getAge());
     }
  
     public void testModification() throws Exception
     {
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test2", "Harald", 32);
        p.setName("Harald Gliebe");
        tx.commit();
        Person p1 = (Person) cache.find("/person/test2");
        tx.begin();
        p1.setName("Benoit");
        tx.commit();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
        tx.begin();
        p1.setName("Harald");
        tx.rollback();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
     }
  
     public void testConcurrentSimplePuts() throws Exception
     {
        Thread t1 = new Thread("t1")
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 Person p = (Person) cache.find("/person/test6");
                 Address addr = new Address();
                 addr.setCity("San Jose");
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 p.setAddress(addr);
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread("t2")
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 Person p = (Person) cache.find("/person/test6");
                 Address addr = new Address();
                 addr.setCity("Santa Clara");
                 tx = getTransaction();
                 tx.begin();
                 p.setAddress(addr);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
  //               t2_ex = ex;
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        // This would fail because we are writing to __JBossInteral__ for every attach now.
        // As a result, there is version conflict for optimistic locking and cuasing
        // rollback.
        assertNotSame("City should be different ", "San Jose", p.getAddress().getCity());
     }
  
     public void testConcurrentPuts() throws Exception
     {
        Thread t1 = new Thread("t1")
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("German");
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread("t2")
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
        List<String> lang = new ArrayList<String>();
        lang.add("German");
        p.setLanguages(lang);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages ",
                2, size);
     }
  
     void log(String s)
     {
        long now;
        if (start == 0)
           start = System.currentTimeMillis();
        now = System.currentTimeMillis();
  
        System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalTxTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2006/12/26 03:41:11;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/optimistic/AbstractOptimisticTestCase.java
  
  Index: AbstractOptimisticTestCase.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.pojo.optimistic;
  
  import junit.framework.TestCase;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.CacheListener;
  import org.jboss.cache.CacheSPI;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.marshall.MethodCall;
  import org.jboss.cache.marshall.MethodCallFactory;
  import org.jboss.cache.marshall.MethodDeclarations;
  import org.jboss.cache.interceptors.Interceptor;
  import org.jboss.cache.interceptors.InvocationContextInterceptor;
  import org.jboss.cache.interceptors.TxInterceptor;
  import org.jboss.cache.interceptors.OptimisticReplicationInterceptor;
  import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
  import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.cache.misc.TestingUtil;
  import org.jboss.cache.lock.IsolationLevel;
  import org.jboss.cache.factories.XmlConfigurationParser;
  import org.jboss.cache.xml.XmlHelper;
  import org.jboss.cache.optimistic.TestListener;
  import org.jboss.cache.optimistic.DefaultDataVersion;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.config.CacheLoaderConfig;
  import org.w3c.dom.Element;
  
  import javax.transaction.TransactionManager;
  import javax.transaction.SystemException;
  import java.io.File;
  import java.util.Random;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Iterator;
  
  /**
   * Base test for optimistic locking. Copied from Cache counterpart.
   */
  public abstract class AbstractOptimisticTestCase extends TestCase
  {
     private int instanceNumber;
  
     // some test data shared among all the test cases
     protected Fqn fqn = Fqn.fromString("/blah");
     protected String key = "myKey", value = "myValue";
  
     protected String getTempDir()
     {
        return getTempDir("tempdir");
     }
  
     private String getTempDir(String name)
     {
        String tempDir = System.getProperty("java.io.tmpdir", "/tmp");
        tempDir = tempDir + File.separator + name;
        System.out.println("tmpdir property: " + System.getProperty("java.io.tmpdir"));
        System.out.println("Attempting to create dir [" + tempDir + "]");
        File tempDirFile = new File(tempDir);
        if (!tempDirFile.exists())
        {
           tempDirFile.mkdirs();
        }
        return tempDir;
     }
  
     public AbstractOptimisticTestCase(String name)
     {
        super(name);
     }
  
     protected PojoCache createCacheUnstarted() throws Exception
     {
        return createCacheUnstarted(true);
     }
  
     protected PojoCache createCacheUnstarted(boolean optimistic) throws Exception
     {
        Configuration c = new Configuration();
        if (optimistic) c.setNodeLockingScheme("OPTIMISTIC");
  
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        c.setCacheMode("LOCAL");
        PojoCache cache = PojoCacheFactory.createCache(c, false);
        return cache;
     }
  
     protected PojoCache createCacheWithListener() throws Exception
     {
        return createCacheWithListener(new TestListener());
     }
  
     protected PojoCache createCacheWithListener(CacheListener listener) throws Exception
     {
        PojoCache cache = createCacheUnstarted();
        cache.create();
        cache.start();
        cache.getCache().addCacheListener(listener);
        return cache;
     }
  
     /**
      * Returns a tree cache with passivation disabled in the loader.
      *
      * @return
      * @throws Exception
      */
     protected PojoCache createCacheWithLoader() throws Exception
     {
        return createCacheWithLoader(false);
     }
  
     protected CacheLoaderConfig getCacheLoaderConfig(boolean shared, String filename, boolean passivation) throws Exception
     {
        String xml = "            <config>\n" +
                "                <passivation>" + passivation + "</passivation>\n" +
                "                <preload></preload>\n" +
                "                <shared>" + shared + "</shared>\n" +
                "                <cacheloader>\n" +
                "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                "                    <properties>\n" +
                "                    </properties>\n" +
                "                    <async>false</async>\n" +
                "                    <fetchPersistentState>" + (!shared) + "</fetchPersistentState>\n" +
                "                    <ignoreModifications>false</ignoreModifications>\n" +
                "                </cacheloader>\n" +
                "            </config>";
        Element element = XmlHelper.stringToElement(xml);
        return XmlConfigurationParser.parseCacheLoaderConfig(element);
     }
  
     protected PojoCache createCacheWithLoader(boolean passivationEnabled) throws Exception
     {
        PojoCache cache = createCacheUnstarted();
        Configuration c = cache.getCache().getConfiguration();
        c.setCacheLoaderConfig(getCacheLoaderConfig(true, getTempDir(), passivationEnabled));
        cache.create();
        cache.start();
        return cache;
     }
  
     protected PojoCache createCache() throws Exception
     {
        PojoCache cache = createCacheUnstarted();
        cache.create();
        cache.start();
        return cache;
     }
  
     protected void destroyCache(PojoCache c)
     {
        c.stop();
        c.destroy();
     }
  
  
     protected PojoCache createPessimisticCache() throws Exception
     {
        Configuration c = new Configuration();
        c.setClusterName("name");
        c.setInitialStateRetrievalTimeout(5000);
        c.setClusterConfig(getDefaultProperties());
        c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
        c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        
        PojoCache cache = PojoCacheFactory.createCache(c, false);
        cache.create();
        cache.start();
  
  
        return cache;
     }
  
     protected PojoCache createPessimisticCacheLocal() throws Exception
     {
        Configuration c = new Configuration();
  
        c.setClusterName("name");
        c.setInitialStateRetrievalTimeout(5000);
        c.setClusterConfig(getDefaultProperties());
  
        c.setCacheMode(Configuration.CacheMode.LOCAL);
        c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        PojoCache cache = PojoCacheFactory.createCache(c, false);
        cache.create();
        cache.start();
  
        return cache;
     }
  
     protected String getDefaultProperties()
     {
        return "UDP(mcast_addr=228.1.2.3;mcast_port=48866;ip_ttl=32;" +
                "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;loopback=true;ip_mcast=true;bind_addr=127.0.0.1):" +
                "PING(timeout=1000;num_initial_members=2):" +
                "MERGE2(min_interval=5000;max_interval=10000):" +
                "FD_SOCK:" +
                "VERIFY_SUSPECT(timeout=1500):" +
                "pbcast.NAKACK(gc_lag=50;max_xmit_size=8192;retransmit_timeout=600,1200,2400,4800):" +
                "UNICAST(timeout=600,1200,2400,4800):" +
                "pbcast.STABLE(desired_avg_gossip=20000):" +
                "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +
                "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
                "shun=false;print_local_addr=true):" +
                "pbcast.STATE_TRANSFER";
     }
  
     protected PojoCache createReplicatedCache(Configuration.CacheMode mode) throws Exception
     {
        return createReplicatedCache("test", mode);
     }
  
     protected PojoCache createReplicatedCache(String name, Configuration.CacheMode mode) throws Exception
     {
        Configuration c = new Configuration();
  
        c.setClusterName(name);
        c.setInitialStateRetrievalTimeout(5000);
        c.setClusterConfig(getDefaultProperties());
        c.setCacheMode(mode);
        if (mode == Configuration.CacheMode.REPL_SYNC)
        {
           // make sure commits and rollbacks are sync as well
           c.setSyncCommitPhase(true);
           c.setSyncRollbackPhase(true);
        }
        c.setNodeLockingScheme("OPTIMISTIC");
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        PojoCache cache = PojoCacheFactory.createCache(c, false);
        cache.create();
        cache.start();
  
        return cache;
     }
  
     protected PojoCache createReplicatedCacheWithLoader(boolean shared, Configuration.CacheMode cacheMode) throws Exception
     {
        return createReplicatedCacheWithLoader("temp-loader", shared, cacheMode);
     }
  
     protected PojoCache createReplicatedCacheWithLoader(boolean shared) throws Exception
     {
        return createReplicatedCacheWithLoader("temp-loader", shared, Configuration.CacheMode.REPL_SYNC);
     }
  
     protected PojoCache createReplicatedCacheWithLoader(String name, boolean shared) throws Exception
     {
        return createReplicatedCacheWithLoader(name, shared, Configuration.CacheMode.REPL_SYNC);
     }
  
     protected PojoCache createReplicatedCacheWithLoader(String name, boolean shared, Configuration.CacheMode cacheMode) throws Exception
     {
        Configuration c = new Configuration();
        c.setClusterName(name);
        c.setInitialStateRetrievalTimeout(5000);
        c.setClusterConfig(getDefaultProperties());
        c.setCacheMode(cacheMode);
        c.setSyncCommitPhase(true);
        c.setSyncRollbackPhase(true);
        c.setNodeLockingScheme("OPTIMISTIC");
        c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        c.setCacheLoaderConfig(getCacheLoaderConfig(shared, shared ? getTempDir(name + "-shared") : getTempDir(name + instanceNumber++), false));
  
        PojoCache cache = PojoCacheFactory.createCache(c, false);
        cache.create();
        cache.start();
        return cache;
     }
  
     protected Random random;
  
     protected void randomSleep(int min, int max)
     {
        if (random == null) random = new Random();
        long l = -1;
        while (l < min) l = random.nextInt(max);
        TestingUtil.sleepThread(l);
     }
  
     protected void tearDown()
     {
        TransactionManager mgr = DummyTransactionManager.getInstance();
        try
        {
           if (mgr.getTransaction() != null)
           {
              mgr.rollback();
           }
        }
        catch (SystemException e)
        {
           // do nothing
        }
     }
  
     protected Interceptor getAlteredInterceptorChain(Interceptor newLast, CacheSPI spi, boolean replicated)
     {
        Interceptor ici = new InvocationContextInterceptor();
        ici.setCache(spi);
  
        Interceptor txInterceptor = new TxInterceptor();
        txInterceptor.setCache(spi);
  
        Interceptor replicationInterceptor = new OptimisticReplicationInterceptor();
        replicationInterceptor.setCache(spi);
  
        Interceptor createInterceptor = new OptimisticCreateIfNotExistsInterceptor();
        createInterceptor.setCache(spi);
  
        Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
        nodeInterceptor.setCache(spi);
  
        ici.setNext(txInterceptor);
        if (replicated)
        {
           txInterceptor.setNext(replicationInterceptor);
           replicationInterceptor.setNext(createInterceptor);
        }
        else
        {
           txInterceptor.setNext(createInterceptor);
        }
        createInterceptor.setNext(nodeInterceptor);
        nodeInterceptor.setNext(newLast);
  
        return ici;
     }
  
     public abstract class ExceptionThread extends Thread
     {
        protected Exception exception;
  
        public void setException(Exception e)
        {
           exception = e;
        }
  
        public Exception getException()
        {
           return exception;
        }
     }
  
     protected List injectDataVersion(List modifications)
     {
        List newList = new ArrayList();
        Iterator mi = modifications.iterator();
        while (mi.hasNext())
        {
           MethodCall c = (MethodCall) mi.next();
           Object[] oa = c.getArgs();
           Object[] na = new Object[oa.length + 1];
           System.out.println("*** " + oa.length);
           for (int i = 0; i < oa.length; i++) na[i] = oa[i];
           na[oa.length] = new DefaultDataVersion();
           newList.add(MethodCallFactory.create(MethodDeclarations.getVersionedMethod(c.getMethodId()), na));
        }
        return newList;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list