Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 68   Methods: 3
NCLOC: 52   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StripedLockTest.java 100% 100% 100% 100%
coverage
 1    package org.jboss.cache.lock;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.Fqn;
 5   
 6    import java.util.ArrayList;
 7    import java.util.HashMap;
 8    import java.util.List;
 9    import java.util.Map;
 10    import java.util.Random;
 11    import java.util.concurrent.locks.ReentrantReadWriteLock;
 12   
 13    public class StripedLockTest extends TestCase
 14    {
 15    private StripedLock stripedLock;
 16   
 17  1 protected void setUp()
 18    {
 19  1 stripedLock = new StripedLock();
 20    }
 21   
 22  1 public void testHashingDistribution()
 23    {
 24    // ensure even bucket distribution of lock stripes
 25  1 List<Fqn> fqns = createRandomFqns(1000);
 26   
 27  1 Map<ReentrantReadWriteLock, Integer> distribution = new HashMap<ReentrantReadWriteLock, Integer>();
 28   
 29  1 for (Fqn f : fqns)
 30    {
 31  1000 ReentrantReadWriteLock lock = stripedLock.getLock(f);
 32  1000 if (distribution.containsKey(lock))
 33    {
 34  968 int count = distribution.get(lock) + 1;
 35  968 distribution.put(lock, count);
 36    }
 37    else
 38    {
 39  32 distribution.put(lock, 1);
 40    }
 41    }
 42   
 43  1 System.out.println(distribution);
 44   
 45    // cannot be larger than the number of locks
 46  1 System.out.println("dist size: " + distribution.size());
 47  1 System.out.println("num shared locks: " + stripedLock.sharedLocks.length);
 48  1 assertTrue(distribution.size() <= stripedLock.sharedLocks.length);
 49    // assume at least a 2/3rd spread
 50  1 assertTrue(distribution.size() * 1.5 >= stripedLock.sharedLocks.length);
 51    }
 52   
 53  1 private List<Fqn> createRandomFqns(int number)
 54    {
 55  1 List<Fqn> f = new ArrayList<Fqn>(number);
 56  1 Random r = new Random();
 57   
 58  1 while (f.size() < number)
 59    {
 60  1000 Fqn fqn = Fqn.fromString("/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))));
 61  1000 f.add(fqn);
 62    }
 63   
 64  1 System.out.println("Fqns: " + f);
 65   
 66  1 return f;
 67    }
 68    }