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

Jimmy Wilson jawilson at redhat.com
Sun Jun 24 16:22:54 EDT 2007


  User: jawilson
  Date: 07/06/24 16:22:54

  Added:       tests/functional/org/jboss/cache/lock  Tag:
                        Branch_JBossCache_1_4_0 StripedLockTest.java
  Log:
  Fix for [JBCACHE-1103].  Cache Loaders need to control locking when needed, not their interceptor counterpart.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +68 -0     JBossCache/tests/functional/org/jboss/cache/lock/StripedLockTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: StripedLockTest.java
  ===================================================================
  RCS file: StripedLockTest.java
  diff -N StripedLockTest.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ StripedLockTest.java	24 Jun 2007 20:22:54 -0000	1.1.2.2
  @@ -0,0 +1,68 @@
  +package org.jboss.cache.lock;
  +
  +import junit.framework.TestCase;
  +import org.jboss.cache.Fqn;
  +
  +import java.util.ArrayList;
  +import java.util.HashMap;
  +import java.util.List;
  +import java.util.Map;
  +import java.util.Random;
  +import java.util.concurrent.locks.ReentrantReadWriteLock;
  +
  +public class StripedLockTest extends TestCase
  +{
  +   private StripedLock stripedLock;
  +
  +   protected void setUp()
  +   {
  +      stripedLock = new StripedLock();
  +   }
  +
  +   public void testHashingDistribution()
  +   {
  +      // ensure even bucket distribution of lock stripes
  +      List<Fqn> fqns = createRandomFqns(1000);
  +
  +      Map<ReentrantReadWriteLock, Integer> distribution = new HashMap<ReentrantReadWriteLock, Integer>();
  +
  +      for (Fqn f : fqns)
  +      {
  +         ReentrantReadWriteLock lock = stripedLock.getLock(f);
  +         if (distribution.containsKey(lock))
  +         {
  +            int count = distribution.get(lock) + 1;
  +            distribution.put(lock, count);
  +         }
  +         else
  +         {
  +            distribution.put(lock, 1);
  +         }
  +      }
  +
  +      System.out.println(distribution);
  +
  +      // cannot be larger than the number of locks
  +      System.out.println("dist size: " + distribution.size());
  +      System.out.println("num shared locks: " + stripedLock.sharedLocks.length);
  +      assertTrue(distribution.size() <= stripedLock.sharedLocks.length);
  +      // assume at least a 2/3rd spread
  +      assertTrue(distribution.size() * 1.5 >= stripedLock.sharedLocks.length);
  +   }
  +
  +   private List<Fqn> createRandomFqns(int number)
  +   {
  +      List<Fqn> f = new ArrayList<Fqn>(number);
  +      Random r = new Random();
  +
  +      while (f.size() < number)
  +      {
  +         Fqn fqn = Fqn.fromString("/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))));
  +         f.add(fqn);
  +      }
  +
  +      System.out.println("Fqns: " + f);
  +
  +      return f;
  +   }
  +}
  
  
  



More information about the jboss-cvs-commits mailing list