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 |
| |
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 |
| |
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 |
| |
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 |
| } |