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

Manik Surtani manik at jboss.org
Sun Jul 29 11:24:08 EDT 2007


  User: msurtani
  Date: 07/07/29 11:24:08

  Added:       tests/functional/org/jboss/cache/notifications 
                        ConcurrentNotificationTest.java
  Log:
  added test
  
  Revision  Changes    Path
  1.1      date: 2007/07/29 15:24:08;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/notifications/ConcurrentNotificationTest.java
  
  Index: ConcurrentNotificationTest.java
  ===================================================================
  package org.jboss.cache.notifications;
  
  import junit.framework.TestCase;
  import org.jboss.cache.Cache;
  import org.jboss.cache.DefaultCacheFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.notifications.annotation.CacheListener;
  import org.jboss.cache.notifications.annotation.NodeCreated;
  import org.jboss.cache.notifications.annotation.NodeModified;
  import org.jboss.cache.notifications.annotation.NodeRemoved;
  import org.jboss.cache.notifications.annotation.NodeVisited;
  import org.jboss.cache.notifications.event.Event;
  
  import java.util.LinkedList;
  import java.util.List;
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.atomic.AtomicInteger;
  
  /**
   * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
   * @since 2.0.0
   */
  public class ConcurrentNotificationTest extends TestCase
  {
     private Cache cache;
     private Listener listener;
     private Fqn fqn = Fqn.fromString("/a/b/c");
  
     protected void setUp()
     {
        cache = DefaultCacheFactory.getInstance().createCache();
        listener = new Listener();
        cache.addCacheListener(listener);
     }
  
     protected void tearDown()
     {
        cache.stop();
     }
  
     public void testThreads() throws Exception
     {
        Thread workers[] = new Thread[20];
        final List<Exception> exceptions = new LinkedList<Exception>();
        final int loops = 100;
        final CountDownLatch latch = new CountDownLatch(1);
  
        for (int i = 0; i < workers.length; i++)
        {
           workers[i] = new Thread()
           {
              public void run()
              {
                 try
                 {
                    latch.await();
                 }
                 catch (InterruptedException e)
                 {
                 }
  
                 for (int j = 0; j < loops; j++)
                 {
                    try
                    {
                       cache.put(fqn, "key", "value");
                    }
                    catch (Exception e)
                    {
                       exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a put()", e));
                    }
  
                    try
                    {
                       cache.remove(fqn, "key");
                    }
                    catch (Exception e)
                    {
                       exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a remove()", e));
                    }
  
                    try
                    {
                       cache.get(fqn, "key");
                    }
                    catch (Exception e)
                    {
                       exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a get()", e));
                    }
                 }
              }
           };
  
           workers[i].start();
        }
  
        latch.countDown();
  
        for (Thread t : workers) t.join();
  
        for (Exception e : exceptions) throw e;
  
        assertEquals(3 * loops * workers.length + 3, listener.counter.get());
     }
  
     @CacheListener
     public class Listener
     {
        private AtomicInteger counter = new AtomicInteger(0);
  
        @NodeModified
        @NodeRemoved
        @NodeVisited
        @NodeCreated
        public void catchEvent(Event e)
        {
           if (e.isPre()) counter.getAndIncrement();
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list