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

Manik Surtani manik at jboss.org
Tue Jun 19 10:27:48 EDT 2007


  User: msurtani
  Date: 07/06/19 10:27:48

  Modified:    tests/functional/org/jboss/cache/loader 
                        ClusteredCacheLoaderTest.java
  Log:
  CCL thread safety
  
  Revision  Changes    Path
  1.16      +161 -0    JBossCache/tests/functional/org/jboss/cache/loader/ClusteredCacheLoaderTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ClusteredCacheLoaderTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/ClusteredCacheLoaderTest.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -b -r1.15 -r1.16
  --- ClusteredCacheLoaderTest.java	14 Jun 2007 15:30:15 -0000	1.15
  +++ ClusteredCacheLoaderTest.java	19 Jun 2007 14:27:48 -0000	1.16
  @@ -14,9 +14,15 @@
   import org.jboss.cache.Fqn;
   import org.jboss.cache.Region;
   import org.jboss.cache.config.Configuration;
  +import org.jboss.cache.lock.TimeoutException;
   
  +import java.util.ArrayList;
  +import java.util.List;
   import java.util.Map;
  +import java.util.Random;
   import java.util.Set;
  +import java.util.concurrent.CopyOnWriteArraySet;
  +import java.util.concurrent.CountDownLatch;
   
   /**
    * Tests ClusteredCacheLoader
  @@ -68,6 +74,7 @@
   
      protected void tearDown()
      {
  +      System.out.println("***** TEARING DOWN ***** ");
         if (cache1 != null)
         {
            cache1.stop();
  @@ -199,4 +206,158 @@
         // and now loader2 should see it
         Assert.assertTrue("should exist", loader2.exists(fqn));
      }
  +
  +   public void testCacheLoaderThreadSafety() throws Exception
  +   {
  +      threadSafetyTest(true);
  +   }
  +
  +   public void testCacheLoaderThreadSafetyMultipleFqns() throws Exception
  +   {
  +      threadSafetyTest(false);
  +   }
  +
  +   protected void threadSafetyTest(final boolean singleFqn) throws Exception
  +   {
  +      final CountDownLatch latch = new CountDownLatch(1);
  +      final Fqn fqn = Fqn.fromString("/a/b/c");
  +      final List<Fqn> fqns = new ArrayList<Fqn>(30);
  +      final Random r = new Random();
  +      if (!singleFqn)
  +      {
  +         for (int i = 0; i < 30; i++)
  +         {
  +            Fqn f = Fqn.fromString("/a/b/c/" + i);
  +            fqns.add(f);
  +            cache2.put(f, "k", "v");
  +            cache1.evict(f);
  +         }
  +      }
  +      else
  +      {
  +         cache2.put(fqn, "k", "v");
  +         cache1.evict(fqn);
  +      }
  +      final int loops = 10000;
  +      final Set<Exception> exceptions = new CopyOnWriteArraySet<Exception>();
  +
  +      Thread evictor = new Thread("Evictor")
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               latch.await();
  +               for (int i = 0; i < loops; i++)
  +               {
  +                  Fqn f = singleFqn ? fqn : fqns.get(r.nextInt(fqns.size()));
  +                  cache1.evict(f);
  +               }
  +            }
  +            catch (TimeoutException te)
  +            {
  +               // doesn't matter if we hit these on occasion
  +            }
  +            catch (Exception e)
  +            {
  +               exceptions.add(e);
  +            }
  +         }
  +      };
  +
  +      evictor.start();
  +
  +      Thread writer = new Thread("Writer")
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               latch.await();
  +               for (int i = 0; i < loops; i++)
  +               {
  +                  Fqn f = singleFqn ? fqn : fqns.get(r.nextInt(fqns.size()));
  +                  cache2.put(f, "k", "v");
  +               }
  +            }
  +            catch (Exception e)
  +            {
  +               exceptions.add(e);
  +            }
  +         }
  +      };
  +
  +      writer.start();
  +
  +
  +      Thread reader1 = new Thread("Reader-1")
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               latch.await();
  +               for (int i = 0; i < loops; i++)
  +               {
  +                  loader1.get(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
  +               }
  +            }
  +            catch (Exception e)
  +            {
  +               exceptions.add(e);
  +            }
  +         }
  +      };
  +      reader1.start();
  +
  +      Thread reader2 = new Thread("Reader-2")
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               latch.await();
  +               for (int i = 0; i < loops; i++)
  +               {
  +                  loader1.getChildrenNames(singleFqn ? fqn.getParent() : fqns.get(r.nextInt(fqns.size())).getParent());
  +               }
  +            }
  +            catch (Exception e)
  +            {
  +               exceptions.add(e);
  +            }
  +         }
  +      };
  +      reader2.start();
  +
  +      Thread reader3 = new Thread("Reader-3")
  +      {
  +         public void run()
  +         {
  +            try
  +            {
  +               latch.await();
  +               for (int i = 0; i < loops; i++)
  +               {
  +                  loader1.getChildrenNames(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
  +               }
  +            }
  +            catch (Exception e)
  +            {
  +               exceptions.add(e);
  +            }
  +         }
  +      };
  +      reader3.start();
  +
  +      latch.countDown();
  +      reader1.join();
  +      reader2.join();
  +      reader3.join();
  +      evictor.join();
  +      writer.join();
  +
  +      for (Exception e : exceptions) throw e;
  +   }
  +
   }
  
  
  



More information about the jboss-cvs-commits mailing list