1 |
| package org.jboss.cache.lock; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.jboss.cache.CacheImpl; |
7 |
| import org.jboss.cache.DefaultCacheFactory; |
8 |
| import org.jboss.cache.Fqn; |
9 |
| import org.jboss.cache.NodeSPI; |
10 |
| import org.jboss.cache.config.Configuration; |
11 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
12 |
| |
13 |
| import javax.transaction.Transaction; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class AcquireAllTest extends TestCase |
20 |
| { |
21 |
| CacheImpl cache = null, cache2; |
22 |
| Transaction tx = null; |
23 |
| final Fqn FQN = Fqn.fromString("/myNode"); |
24 |
| final String KEY = "key"; |
25 |
| final String VALUE = "value"; |
26 |
| |
27 |
| |
28 |
2
| protected void setUp() throws Exception
|
29 |
| { |
30 |
2
| super.setUp();
|
31 |
| } |
32 |
| |
33 |
2
| protected void tearDown() throws Exception
|
34 |
| { |
35 |
2
| super.tearDown();
|
36 |
2
| if (cache != null)
|
37 |
| { |
38 |
2
| cache.stop();
|
39 |
2
| cache.destroy();
|
40 |
2
| cache = null;
|
41 |
| } |
42 |
2
| if (tx != null)
|
43 |
| { |
44 |
0
| tx.commit();
|
45 |
0
| tx = null;
|
46 |
| } |
47 |
| } |
48 |
| |
49 |
| |
50 |
1
| public void testAcquireAll() throws Exception
|
51 |
| { |
52 |
1
| NodeSPI root;
|
53 |
1
| Object owner = Thread.currentThread();
|
54 |
| |
55 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
56 |
1
| cache.put("/a/b/c", null);
|
57 |
1
| cache.put("/1/2/3", null);
|
58 |
| |
59 |
1
| root = cache.getRoot();
|
60 |
1
| NodeLock lock = root.getLock();
|
61 |
| |
62 |
1
| lock.acquireAll(owner, 2000, NodeLock.LockType.READ);
|
63 |
1
| lock.releaseAll(owner);
|
64 |
| |
65 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
66 |
| |
67 |
1
| lock.acquireAll(owner, 2000, NodeLock.LockType.WRITE);
|
68 |
1
| lock.releaseAll(owner);
|
69 |
| |
70 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
71 |
| } |
72 |
| |
73 |
| |
74 |
1
| public void testAcquireAllReplicated() throws Exception
|
75 |
| { |
76 |
1
| NodeSPI root;
|
77 |
1
| Object owner = Thread.currentThread();
|
78 |
| |
79 |
1
| cache2 = createCache(Configuration.CacheMode.REPL_ASYNC, IsolationLevel.SERIALIZABLE);
|
80 |
1
| cache2.put("/a/b/c", null);
|
81 |
1
| cache2.put("/1/2/3", null);
|
82 |
| |
83 |
1
| cache = createCache(Configuration.CacheMode.REPL_ASYNC, IsolationLevel.SERIALIZABLE);
|
84 |
1
| root = cache.getRoot();
|
85 |
1
| NodeLock lock = root.getLock();
|
86 |
| |
87 |
1
| lock.acquireAll(owner, 2000, NodeLock.LockType.READ);
|
88 |
1
| lock.releaseAll(owner);
|
89 |
| |
90 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
91 |
| |
92 |
1
| lock.acquireAll(owner, 2000, NodeLock.LockType.WRITE);
|
93 |
1
| lock.releaseAll(owner);
|
94 |
| |
95 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
96 |
| } |
97 |
| |
98 |
| |
99 |
3
| CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception
|
100 |
| { |
101 |
3
| CacheImpl c = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
102 |
3
| c.getConfiguration().setCacheMode(mode);
|
103 |
3
| c.getConfiguration().setIsolationLevel(level);
|
104 |
3
| c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
105 |
3
| c.create();
|
106 |
3
| c.start();
|
107 |
3
| return c;
|
108 |
| } |
109 |
| |
110 |
0
| Transaction startTransaction()
|
111 |
| { |
112 |
0
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
113 |
0
| try
|
114 |
| { |
115 |
0
| mgr.begin();
|
116 |
0
| return mgr.getTransaction();
|
117 |
| } |
118 |
| catch (Throwable t) |
119 |
| { |
120 |
0
| return null;
|
121 |
| } |
122 |
| } |
123 |
| |
124 |
| |
125 |
1
| public static Test suite()
|
126 |
| { |
127 |
1
| return new TestSuite(AcquireAllTest.class);
|
128 |
| } |
129 |
| |
130 |
0
| public static void main(String[] args)
|
131 |
| { |
132 |
0
| junit.textui.TestRunner.run(suite());
|
133 |
| } |
134 |
| |
135 |
| } |