1 |
| package org.jboss.cache; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.jboss.cache.config.Configuration; |
7 |
| import org.jboss.cache.lock.IsolationLevel; |
8 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
9 |
| |
10 |
| import javax.transaction.NotSupportedException; |
11 |
| import javax.transaction.Transaction; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class CallbackTest extends TestCase |
20 |
| { |
21 |
| CacheImpl cache = null, cache2; |
22 |
| Transaction tx = null; |
23 |
| final Fqn FQN_A = Fqn.fromString("/a"); |
24 |
| final Fqn FQN_B = Fqn.fromString("/b"); |
25 |
| final String KEY = "key"; |
26 |
| final String VALUE = "value"; |
27 |
| |
28 |
| |
29 |
5
| protected void setUp() throws Exception
|
30 |
| { |
31 |
5
| super.setUp();
|
32 |
| } |
33 |
| |
34 |
5
| protected void tearDown() throws Exception
|
35 |
| { |
36 |
5
| super.tearDown();
|
37 |
5
| if (cache != null)
|
38 |
| { |
39 |
5
| cache.stop();
|
40 |
5
| cache.destroy();
|
41 |
5
| cache = null;
|
42 |
| } |
43 |
5
| if (tx != null)
|
44 |
| { |
45 |
2
| tx.commit();
|
46 |
2
| tx = null;
|
47 |
| } |
48 |
| } |
49 |
| |
50 |
| |
51 |
1
| public void testLocalPutCallbackWithoutTransaction() throws Exception, NotSupportedException
|
52 |
| { |
53 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
54 |
1
| cache.getNotifier().addCacheListener(new PutListener(cache));
|
55 |
| |
56 |
1
| cache.put(FQN_A, null);
|
57 |
1
| assertTrue(cache.exists(FQN_A));
|
58 |
1
| assertTrue(cache.exists(FQN_B));
|
59 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
60 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
61 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
62 |
| } |
63 |
| |
64 |
1
| public void testLocalGetCallbackSameFqnWithoutTransaction() throws Exception, NotSupportedException
|
65 |
| { |
66 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
67 |
1
| cache.getNotifier().addCacheListener(new GetListener(cache, FQN_A));
|
68 |
| |
69 |
1
| cache.put(FQN_A, null);
|
70 |
1
| assertTrue(cache.exists(FQN_A));
|
71 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
72 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
73 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
74 |
| } |
75 |
| |
76 |
1
| public void testLocalGetCallbackDifferentFqnWithoutTransaction() throws Exception, NotSupportedException
|
77 |
| { |
78 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
79 |
1
| cache.put(FQN_B, null);
|
80 |
1
| cache.getNotifier().addCacheListener(new GetListener(cache, FQN_B));
|
81 |
| |
82 |
1
| cache.put("/a", null);
|
83 |
1
| assertTrue(cache.exists(FQN_A));
|
84 |
1
| assertTrue(cache.exists(FQN_B));
|
85 |
1
| assertEquals(cache.getLockTable().size(), 0);
|
86 |
1
| System.out.println("cache locks:\n" + cache.printLockInfo());
|
87 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
88 |
| } |
89 |
| |
90 |
| |
91 |
1
| public void testLocalCallbackWithTransaction() throws Exception, NotSupportedException
|
92 |
| { |
93 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
94 |
1
| cache.getNotifier().addCacheListener(new PutListener(cache));
|
95 |
1
| tx = startTransaction();
|
96 |
1
| cache.put(FQN_A, null);
|
97 |
1
| tx.commit();
|
98 |
1
| assertTrue(cache.exists(FQN_A));
|
99 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
100 |
| } |
101 |
| |
102 |
| |
103 |
1
| public void testLocalCallbackWithException() throws Exception, NotSupportedException
|
104 |
| { |
105 |
1
| cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
|
106 |
1
| cache.getNotifier().addCacheListener(new ExceptionListener());
|
107 |
1
| tx = startTransaction();
|
108 |
1
| try
|
109 |
| { |
110 |
1
| cache.put(FQN_A, null);
|
111 |
0
| tx.rollback();
|
112 |
| } |
113 |
| catch (RuntimeException ex) |
114 |
| { |
115 |
1
| tx.rollback();
|
116 |
| } |
117 |
1
| assertFalse(cache.exists(FQN_A));
|
118 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
119 |
| |
120 |
| |
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
5
| CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception
|
168 |
| { |
169 |
5
| Configuration c = new Configuration();
|
170 |
5
| c.setCacheMode(mode);
|
171 |
5
| c.setIsolationLevel(level);
|
172 |
5
| c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
173 |
5
| return (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
|
174 |
| } |
175 |
| |
176 |
2
| Transaction startTransaction()
|
177 |
| { |
178 |
2
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
179 |
2
| try
|
180 |
| { |
181 |
2
| mgr.begin();
|
182 |
2
| return mgr.getTransaction();
|
183 |
| } |
184 |
| catch (Throwable t) |
185 |
| { |
186 |
0
| return null;
|
187 |
| } |
188 |
| } |
189 |
| |
190 |
| |
191 |
| class ExceptionListener extends AbstractCacheListener |
192 |
| { |
193 |
1
| public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
|
194 |
| { |
195 |
1
| if (pre) throw new RuntimeException("this will cause the TX to rollback");
|
196 |
| } |
197 |
| } |
198 |
| |
199 |
| |
200 |
| class GetListener extends AbstractCacheListener |
201 |
| { |
202 |
| CacheImpl c; |
203 |
| Fqn my_fqn; |
204 |
| |
205 |
2
| public GetListener(CacheImpl c, Fqn my_fqn)
|
206 |
| { |
207 |
2
| this.c = c;
|
208 |
2
| this.my_fqn = my_fqn;
|
209 |
| } |
210 |
| |
211 |
4
| public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
|
212 |
| { |
213 |
4
| if (!pre)
|
214 |
| { |
215 |
2
| try
|
216 |
| { |
217 |
2
| Node n = c.get(this.my_fqn);
|
218 |
2
| assertNotNull(n);
|
219 |
| } |
220 |
| catch (CacheException e) |
221 |
| { |
222 |
0
| fail("listener was unable to do a get(" + my_fqn + ") during callback: " + e);
|
223 |
| } |
224 |
| } |
225 |
| } |
226 |
| |
227 |
| } |
228 |
| |
229 |
| class PutListener extends AbstractCacheListener |
230 |
| { |
231 |
| CacheImpl c; |
232 |
| |
233 |
2
| public PutListener(CacheImpl c)
|
234 |
| { |
235 |
2
| this.c = c;
|
236 |
| } |
237 |
| |
238 |
8
| public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
|
239 |
| { |
240 |
8
| if (!pre)
|
241 |
| { |
242 |
4
| try
|
243 |
| { |
244 |
4
| if (!c.exists(FQN_B))
|
245 |
| { |
246 |
2
| System.out.println("PutListener: creating node " + FQN_B);
|
247 |
2
| c.put(FQN_B, KEY, VALUE);
|
248 |
2
| System.out.println("PutListener: created node " + FQN_B);
|
249 |
| } |
250 |
| } |
251 |
| catch (CacheException e) |
252 |
| { |
253 |
0
| fail("listener was unable to update cache during callback: " + e);
|
254 |
| } |
255 |
| } |
256 |
| } |
257 |
| |
258 |
| } |
259 |
| |
260 |
1
| public static Test suite()
|
261 |
| { |
262 |
1
| return new TestSuite(CallbackTest.class);
|
263 |
| } |
264 |
| |
265 |
| } |