1 8 9 package org.jboss.cache.lock; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.apache.commons.logging.Log; 15 import org.jboss.cache.CacheImpl; 16 import org.jboss.cache.Fqn; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.transaction.UserTransaction ; 22 import java.util.Properties ; 23 import java.util.Set ; 24 25 31 public class LockReleaseTest extends TestCase 32 { 33 CacheImpl cache = null; 34 UserTransaction tx = null; 35 Log log; 36 Properties p = null; 37 String old_factory = null; 38 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 39 final Fqn NODE1 = Fqn.fromString("/test"); 40 final Fqn NODE2 = Fqn.fromString("/my/test"); 41 final String KEY = "key"; 42 final String VAL1 = "val1"; 43 final String VAL2 = "val2"; 44 45 46 public LockReleaseTest(String name) 47 { 48 super(name); 49 } 50 51 public void setUp() throws Exception 52 { 53 super.setUp(); 54 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 55 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 56 DummyTransactionManager.getInstance(); 57 if (p == null) 58 { 59 p = new Properties (); 60 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 61 } 62 tx = (UserTransaction ) new InitialContext (p).lookup("UserTransaction"); 63 } 64 65 public void tearDown() throws Exception 66 { 67 super.tearDown(); 68 if (cache != null) 69 cache.stop(); 70 71 DummyTransactionManager.destroy(); 73 if (old_factory != null) 74 { 75 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 76 old_factory = null; 77 } 78 79 if (tx != null) 80 { 81 try 82 { 83 tx.rollback(); 84 } 85 catch (Throwable t) 86 { 87 } 88 tx = null; 89 } 90 } 91 92 CacheImpl createCache(IsolationLevel level) throws Exception 93 { 94 CacheImpl c = new CacheImpl(); 95 c.getConfiguration().setClusterName("test"); 96 c.getConfiguration().setInitialStateRetrievalTimeout(10000); 97 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 98 c.getConfiguration().setLockAcquisitionTimeout(500); 99 c.getConfiguration().setIsolationLevel(level); 100 c.create(); 101 c.start(); 102 return c; 103 } 104 105 106 public void testReadWithReadUncommitted() throws Exception 107 { 108 testReadLockRelease(IsolationLevel.READ_UNCOMMITTED); 109 } 110 111 public void testWriteWithReadUncommitted() throws Exception 112 { 113 testWriteLockRelease(IsolationLevel.READ_UNCOMMITTED); 114 } 115 116 117 public void testReadWithReadCommitted() throws Exception 118 { 119 testReadLockRelease(IsolationLevel.READ_COMMITTED); 120 } 121 122 public void testWriteWithReadCommitted() throws Exception 123 { 124 testWriteLockRelease(IsolationLevel.READ_COMMITTED); 125 } 126 127 128 public void testReadWithRepeatableRead() throws Exception 129 { 130 testReadLockRelease(IsolationLevel.REPEATABLE_READ); 131 } 132 133 public void testWriteWithRepeatableRead() throws Exception 134 { 135 testWriteLockRelease(IsolationLevel.REPEATABLE_READ); 136 } 137 138 139 public void testReadWithSerialzable() throws Exception 140 { 141 testReadLockRelease(IsolationLevel.SERIALIZABLE); 142 } 143 144 public void testWriteWithSerializable() throws Exception 145 { 146 testWriteLockRelease(IsolationLevel.SERIALIZABLE); 147 } 148 149 150 public void testGetKeys() throws Exception 151 { 152 cache = createCache(IsolationLevel.REPEATABLE_READ); 153 cache.put(NODE1, KEY, VAL1); 155 cache.put(NODE2, KEY, VAL1); 156 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 157 158 Set keys = cache.getKeys(NODE1); 159 System.out.println("keys of " + NODE1 + " are " + keys); 160 assertEquals("getKeys() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 161 162 tx.begin(); 163 keys = cache.getKeys(NODE1); 164 assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld()); 165 keys = cache.getKeys(NODE2); 166 assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld()); 167 tx.commit(); 168 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 169 } 170 171 172 public void testGetChildrenNames() throws Exception 173 { 174 cache = createCache(IsolationLevel.REPEATABLE_READ); 175 cache.put(NODE1, KEY, VAL1); 177 cache.put(NODE2, KEY, VAL1); 178 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 179 180 Set keys = cache.getChildrenNames(NODE2); 181 System.out.println("keys of " + NODE2 + " are " + keys); 182 assertEquals("getChildrenNames() called outside the TX should have released all locks", 0, 183 cache.getNumberOfLocksHeld()); 184 185 tx.begin(); 186 keys = cache.getChildrenNames(NODE1); 187 assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld()); 188 keys = cache.getChildrenNames(NODE2); 189 assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld()); 190 tx.commit(); 191 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 192 } 193 194 public void testPrint() throws Exception 195 { 196 cache = createCache(IsolationLevel.REPEATABLE_READ); 197 cache.put(NODE1, KEY, VAL1); 199 cache.put(NODE2, KEY, VAL1); 200 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 201 202 cache.print(NODE1); 203 assertEquals("print() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 204 205 tx.begin(); 206 cache.print(NODE1); 207 assertEquals("we should hold 1 read locks now (for print()): ", 2, cache.getNumberOfLocksHeld()); 208 cache.print(NODE2); 209 assertEquals("we should hold 3 read locks now (for print()): ", 4, cache.getNumberOfLocksHeld()); 210 tx.commit(); 211 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 212 } 213 214 215 void testReadLockRelease(IsolationLevel level) throws Exception 216 { 217 cache = createCache(level); 218 cache.put(NODE1, KEY, VAL1); 220 cache.put(NODE2, KEY, VAL1); 221 222 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 223 224 tx.begin(); 225 assertEquals(VAL1, cache.get(NODE1, KEY)); 226 assertEquals(VAL1, cache.get(NODE2, KEY)); 227 assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld()); 228 tx.commit(); 229 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 230 } 231 232 void testWriteLockRelease(IsolationLevel level) throws Exception 233 { 234 cache = createCache(level); 235 cache.put(NODE1, KEY, VAL1); 237 cache.put(NODE2, KEY, VAL1); 238 239 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 240 241 tx.begin(); 242 cache.put(NODE1, KEY, VAL1); 243 cache.put(NODE2, KEY, VAL1); 244 assertEquals("we should hold 3 write locks now: ", 4, cache.getNumberOfLocksHeld()); 245 tx.commit(); 246 assertEquals("we should have released all 3 write locks: ", 0, cache.getNumberOfLocksHeld()); 247 } 248 249 public static Test suite() throws Exception 250 { 251 return new TestSuite(LockReleaseTest.class); 252 } 253 254 public static void main(String [] args) throws Exception 255 { 256 junit.textui.TestRunner.run(suite()); 257 } 258 259 260 } 261 | Popular Tags |