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

Manik Surtani manik at jboss.org
Wed Jul 18 09:20:46 EDT 2007


  User: msurtani
  Date: 07/07/18 09:20:46

  Added:       tests/functional/org/jboss/cache/optimistic 
                        ConcurrentCreationTest.java
  Log:
  test for JBCACHE-1026
  
  Revision  Changes    Path
  1.2       +98 -0     JBossCache/tests/functional/org/jboss/cache/optimistic/ConcurrentCreationTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ConcurrentCreationTest.java
  ===================================================================
  RCS file: ConcurrentCreationTest.java
  diff -N ConcurrentCreationTest.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ ConcurrentCreationTest.java	18 Jul 2007 13:20:46 -0000	1.2
  @@ -0,0 +1,98 @@
  +package org.jboss.cache.optimistic;
  +
  +import junit.framework.TestCase;
  +import org.jboss.cache.CacheSPI;
  +import org.jboss.cache.DefaultCacheFactory;
  +import org.jboss.cache.Fqn;
  +import org.jboss.cache.InvocationContext;
  +import org.jboss.cache.config.Configuration;
  +import org.jboss.cache.interceptors.Interceptor;
  +import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
  +import org.jboss.cache.misc.TestingUtil;
  +import org.jboss.cache.transaction.DummyTransactionManagerLookup;
  +
  +import java.util.List;
  +import java.util.concurrent.CopyOnWriteArrayList;
  +
  +/**
  + * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
  + * @since 2.0.0
  + */
  +public class ConcurrentCreationTest extends TestCase
  +{
  +   private Fqn f = Fqn.fromString("/a/b");
  +   private CacheSPI c;
  +   private List<Exception> exceptions = new CopyOnWriteArrayList<Exception>();
  +
  +   protected void setUp()
  +   {
  +      c = (CacheSPI) DefaultCacheFactory.getInstance().createCache(false);
  +      c.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
  +      c.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
  +      c.start();
  +   }
  +
  +   protected void tearDown()
  +   {
  +      c.stop();
  +   }
  +
  +   public class Putter extends Thread
  +   {
  +      public Putter(String name)
  +      {
  +         super(name);
  +      }
  +
  +      public void run()
  +      {
  +         try
  +         {
  +            c.put(new Fqn(f, getName()), "a", "b");
  +         }
  +         catch (Exception e)
  +         {
  +            exceptions.add(e);
  +         }
  +      }
  +   }
  +
  +   public void testConcurrentPut() throws Exception
  +   {
  +      Interceptor in = new Interceptor()
  +      {
  +         public Object invoke(InvocationContext ctx) throws Throwable
  +         {
  +            if (Thread.currentThread().getName().equals("SLOWPUT")) Thread.sleep(1000);
  +            return super.invoke(ctx);
  +         }
  +      };
  +
  +      List interceptors = c.getInterceptorChain();
  +      int i = 0;
  +      for (i = 0; i < interceptors.size(); i++)
  +      {
  +         if (interceptors.get(i) instanceof OptimisticCreateIfNotExistsInterceptor) break;
  +      }
  +
  +      c.addInterceptor(in, i + 1);
  +
  +      System.out.println(c.getInterceptorChain());
  +
  +      // now create 2 threads to do concurrent puts.
  +      Putter slow = new Putter("SLOWPUT");
  +      Putter fast = new Putter("FASTPUT");
  +
  +      // start the slow putter first
  +      slow.start();
  +      TestingUtil.sleepThread(200);
  +      fast.start();
  +
  +      fast.join();
  +      slow.join();
  +
  +      for (Exception e : exceptions) e.printStackTrace();
  +      assertEquals(0, exceptions.size());
  +   }
  +
  +}
  
  
  



More information about the jboss-cvs-commits mailing list