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 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 protected void setUp() throws Exception 30 { 31 super.setUp(); 32 } 33 34 protected void tearDown() throws Exception 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 , NotSupportedException 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)); 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 , NotSupportedException 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 , NotSupportedException 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 , NotSupportedException 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 , NotSupportedException 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 ex) 114 { 115 tx.rollback(); 116 } 117 assertFalse(cache.exists(FQN_A)); 118 assertEquals(0, cache.getNumberOfLocksHeld()); 119 } 122 123 156 157 165 166 167 CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception 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 startTransaction() 179 { 180 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 181 try 182 { 183 mgr.begin(); 184 return mgr.getTransaction(); 185 } 186 catch (Throwable 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 ("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 |