KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > CallbackTest


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 JavaDoc;
11 import javax.transaction.Transaction JavaDoc;
12
13 /**
14  * Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
15  *
16  * @author Bela Ban
17  * @version $Id: CallbackTest.java,v 1.14 2006/12/30 17:49:54 msurtani Exp $
18  */

19 public class CallbackTest extends TestCase
20 {
21    CacheImpl cache = null, cache2;
22    Transaction JavaDoc tx = null;
23    final Fqn FQN_A = Fqn.fromString("/a");
24    final Fqn FQN_B = Fqn.fromString("/b");
25    final String JavaDoc KEY = "key";
26    final String JavaDoc VALUE = "value";
27
28
29    protected void setUp() throws Exception JavaDoc
30    {
31       super.setUp();
32    }
33
34    protected void tearDown() throws Exception JavaDoc
35    {
36       super.tearDown();
37       if (cache != null)
38       {
39          cache.stop();
40          cache.destroy();
41          cache = null;
42       }
43       if (tx != null)
44       {
45          tx.commit();
46          tx = null;
47       }
48    }
49
50
51    public void testLocalPutCallbackWithoutTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc
52    {
53       cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
54       cache.getNotifier().addCacheListener(new PutListener(cache));
55
56       cache.put(FQN_A, null);
57       assertTrue(cache.exists(FQN_A));
58       assertTrue(cache.exists(FQN_B)); //created by callback
59
assertEquals(cache.getLockTable().size(), 0);
60       System.out.println("cache locks:\n" + cache.printLockInfo());
61       assertEquals(0, cache.getNumberOfLocksHeld());
62    }
63
64    public void testLocalGetCallbackSameFqnWithoutTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc
65    {
66       cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
67       cache.getNotifier().addCacheListener(new GetListener(cache, FQN_A));
68
69       cache.put(FQN_A, null);
70       assertTrue(cache.exists(FQN_A));
71       assertEquals(cache.getLockTable().size(), 0);
72       System.out.println("cache locks:\n" + cache.printLockInfo());
73       assertEquals(0, cache.getNumberOfLocksHeld());
74    }
75
76    public void testLocalGetCallbackDifferentFqnWithoutTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc
77    {
78       cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
79       cache.put(FQN_B, null);
80       cache.getNotifier().addCacheListener(new GetListener(cache, FQN_B));
81
82       cache.put("/a", null);
83       assertTrue(cache.exists(FQN_A));
84       assertTrue(cache.exists(FQN_B));
85       assertEquals(cache.getLockTable().size(), 0);
86       System.out.println("cache locks:\n" + cache.printLockInfo());
87       assertEquals(0, cache.getNumberOfLocksHeld());
88    }
89
90
91    public void testLocalCallbackWithTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc
92    {
93       cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
94       cache.getNotifier().addCacheListener(new PutListener(cache));
95       tx = startTransaction();
96       cache.put(FQN_A, null);
97       tx.commit();
98       assertTrue(cache.exists(FQN_A));
99       assertEquals(0, cache.getNumberOfLocksHeld());
100    }
101
102
103    public void testLocalCallbackWithException() throws Exception JavaDoc, NotSupportedException JavaDoc
104    {
105       cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
106       cache.getNotifier().addCacheListener(new ExceptionListener());
107       tx = startTransaction();
108       try
109       {
110          cache.put(FQN_A, null);
111          tx.rollback();
112       }
113       catch (RuntimeException JavaDoc ex)
114       {
115          tx.rollback();
116       }
117       assertFalse(cache.exists(FQN_A));
118       assertEquals(0, cache.getNumberOfLocksHeld());
119       // assertFalse(cache.exists("/a")); -- fix: reason is that CreateIfNotExists interceptor is *before*
120
// PessimisticLockInterceptor, which registers for TX completion, we don't even get that far
121
}
122
123    /*public void testSyncReplicationWith2Caches() throws Exception, NotSupportedException {
124       cache=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
125       cache2=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
126       cache.setSyncCommitPhase(true);
127       cache2.setSyncCommitPhase(true);
128
129       assertEquals(2, cache.getMembers().size());
130       System.out.println("view is correct: " + cache.getMembers() + " (2 members)");
131       cache2.addTreeCacheListener(new PutListener(cache2));
132
133       tx=startTransaction();
134       cache.put("/a", null);
135       tx.commit();
136
137       // 1. cache: put("/a")
138       // 2. tx.commit() in cache causes 2PC to cache2
139       // 3. cache2 is updated with "/a"
140       // 4. listener in cache2 is called, creates FQN
141       // 5. cache2 replicates FQN over to cache as part of its own 2PC protocol
142
143       assertTrue(cache.exists("/a")); // original modification to cache
144       assertTrue(cache.exists(FQN)); // result of replication from cache
145       assertTrue(cache2.exists("/a")); // result of replication from cache
146       assertTrue(cache2.exists(FQN)); // created by listener, triggered by replication of "/a" from cache
147       TransactionTable tx_table1, tx_table2;
148       tx_table1=cache.getTransactionTable();
149       tx_table2=cache2.getTransactionTable();
150       System.out.println("tx_table1=" + tx_table1 + ", tx_table2=" + tx_table2);
151       assertEquals(0, tx_table1.getNumLocalTransactions());
152       assertEquals(0, tx_table2.getNumLocalTransactions());
153       assertEquals(0, tx_table1.getNumGlobalTransactions());
154       assertEquals(0, tx_table2.getNumGlobalTransactions());
155    }*/

156
157 /* public static void main(String[] args) {
158    try {
159       new CallbackTest().testSyncReplicationWith2Caches();
160    }
161    catch(Exception e) {
162       e.printStackTrace();
163    }
164 }*/

165
166
167    CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception JavaDoc
168    {
169       CacheImpl c = new CacheImpl();
170       c.getConfiguration().setCacheMode(mode);
171       c.getConfiguration().setIsolationLevel(level);
172       c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
173       c.create();
174       c.start();
175       return c;
176    }
177
178    Transaction JavaDoc startTransaction()
179    {
180       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
181       try
182       {
183          mgr.begin();
184          return mgr.getTransaction();
185       }
186       catch (Throwable JavaDoc t)
187       {
188          return null;
189       }
190    }
191
192
193    class ExceptionListener extends AbstractCacheListener
194    {
195       public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
196       {
197          if (pre) throw new RuntimeException JavaDoc("this will cause the TX to rollback");
198       }
199    }
200
201
202    class GetListener extends AbstractCacheListener
203    {
204       CacheImpl c;
205       Fqn my_fqn;
206
207       public GetListener(CacheImpl c, Fqn my_fqn)
208       {
209          this.c = c;
210          this.my_fqn = my_fqn;
211       }
212
213       public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
214       {
215          if (!pre)
216          {
217             try
218             {
219                Node n = c.get(this.my_fqn);
220                assertNotNull(n);
221             }
222             catch (CacheException e)
223             {
224                fail("listener was unable to do a get(" + my_fqn + ") during callback: " + e);
225             }
226          }
227       }
228
229    }
230
231    class PutListener extends AbstractCacheListener
232    {
233       CacheImpl c;
234
235       public PutListener(CacheImpl c)
236       {
237          this.c = c;
238       }
239
240       public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
241       {
242          if (!pre)
243          {
244             try
245             {
246                if (!c.exists(FQN_B))
247                {
248                   System.out.println("PutListener: creating node " + FQN_B);
249                   c.put(FQN_B, KEY, VALUE);
250                   System.out.println("PutListener: created node " + FQN_B);
251                }
252             }
253             catch (CacheException e)
254             {
255                fail("listener was unable to update cache during callback: " + e);
256             }
257          }
258       }
259
260    }
261
262    public static Test suite()
263    {
264       return new TestSuite(CallbackTest.class);
265    }
266
267 }
268
Popular Tags