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

Manik Surtani manik at jboss.org
Tue Jun 19 09:05:19 EDT 2007


  User: msurtani
  Date: 07/06/19 09:05:19

  Added:       tests/functional/org/jboss/cache/lock  StripedLockTest.java
  Log:
  CL thread safety
  
  Revision  Changes    Path
  1.1      date: 2007/06/19 13:05:19;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/lock/StripedLockTest.java
  
  Index: StripedLockTest.java
  ===================================================================
  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