1 8 9 package org.jboss.cache.tests.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.Fqn; 16 import org.jboss.cache.TreeCache; 17 import org.jboss.cache.lock.IsolationLevel; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.transaction.UserTransaction ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 26 32 public class LockReleaseTest extends TestCase { 33 TreeCache 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 super(name); 48 } 49 50 public void setUp() throws Exception { 51 super.setUp(); 52 old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 53 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 54 DummyTransactionManager.getInstance(); 55 if(p == null) { 56 p=new Properties (); 57 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 58 } 59 tx=(UserTransaction )new InitialContext (p).lookup("UserTransaction"); 60 } 61 62 public void tearDown() throws Exception { 63 super.tearDown(); 64 if(cache != null) 65 cache.stopService(); 66 67 DummyTransactionManager.destroy(); 69 if(old_factory != null) { 70 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 71 old_factory=null; 72 } 73 74 if(tx != null) { 75 try { 76 tx.rollback(); 77 } 78 catch(Throwable t) { 79 } 80 tx=null; 81 } 82 } 83 84 TreeCache createCache(IsolationLevel level) throws Exception { 85 TreeCache c=new TreeCache("test", null, 10000); 86 c.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 87 c.setLockAcquisitionTimeout(500); 88 c.setIsolationLevel(level); 89 c.createService(); 90 c.startService(); 91 return c; 92 } 93 94 95 96 public void testReadWithReadUncommitted() throws Exception { 97 testReadLockRelease(IsolationLevel.READ_UNCOMMITTED); 98 } 99 100 public void testWriteWithReadUncommitted() throws Exception { 101 testWriteLockRelease(IsolationLevel.READ_UNCOMMITTED); 102 } 103 104 105 public void testReadWithReadCommitted() throws Exception { 106 testReadLockRelease(IsolationLevel.READ_COMMITTED); 107 } 108 109 public void testWriteWithReadCommitted() throws Exception { 110 testWriteLockRelease(IsolationLevel.READ_COMMITTED); 111 } 112 113 114 public void testReadWithRepeatableRead() throws Exception { 115 testReadLockRelease(IsolationLevel.REPEATABLE_READ); 116 } 117 118 public void testWriteWithRepeatableRead() throws Exception { 119 testWriteLockRelease(IsolationLevel.REPEATABLE_READ); 120 } 121 122 123 public void testReadWithSerialzable() throws Exception { 124 testReadLockRelease(IsolationLevel.SERIALIZABLE); 125 } 126 127 public void testWriteWithSerializable() throws Exception { 128 testWriteLockRelease(IsolationLevel.SERIALIZABLE); 129 } 130 131 132 public void testGetKeys() throws Exception { 133 cache=createCache(IsolationLevel.REPEATABLE_READ); 134 cache.put(NODE1, KEY, VAL1); 136 cache.put(NODE2, KEY, VAL1); 137 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 138 139 Set keys=cache.getKeys(NODE1); 140 System.out.println("keys of " + NODE1 + " are " + keys); 141 assertEquals("getKeys() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 142 143 tx.begin(); 144 keys=cache.getKeys(NODE1); 145 assertEquals("we should hold 1 read locks now: ", 1, cache.getNumberOfLocksHeld()); 146 keys=cache.getKeys(NODE2); 147 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 148 tx.commit(); 149 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 150 } 151 152 153 public void testGetChildrenNames() throws Exception { 154 cache=createCache(IsolationLevel.REPEATABLE_READ); 155 cache.put(NODE1, KEY, VAL1); 157 cache.put(NODE2, KEY, VAL1); 158 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 159 160 Set keys=cache.getChildrenNames(NODE2); 161 System.out.println("keys of " + NODE2 + " are " + keys); 162 assertEquals("getChildrenNames() called outside the TX should have released all locks", 0, 163 cache.getNumberOfLocksHeld()); 164 165 tx.begin(); 166 keys=cache.getChildrenNames(NODE1); 167 assertEquals("we should hold 1 read locks now: ", 1, cache.getNumberOfLocksHeld()); 168 keys=cache.getChildrenNames(NODE2); 169 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 170 tx.commit(); 171 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 172 } 173 174 public void testPrint() throws Exception { 175 cache=createCache(IsolationLevel.REPEATABLE_READ); 176 cache.put(NODE1, KEY, VAL1); 178 cache.put(NODE2, KEY, VAL1); 179 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 180 181 cache.print(NODE1); 182 assertEquals("print() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld()); 183 184 tx.begin(); 185 cache.print(NODE1); 186 assertEquals("we should hold 1 read locks now (for print()): ", 1, cache.getNumberOfLocksHeld()); 187 cache.print(NODE2); 188 assertEquals("we should hold 3 read locks now (for print()): ", 3, cache.getNumberOfLocksHeld()); 189 tx.commit(); 190 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 191 } 192 193 194 void testReadLockRelease(IsolationLevel level) throws Exception { 195 cache=createCache(level); 196 cache.put(NODE1, KEY, VAL1); 198 cache.put(NODE2, KEY, VAL1); 199 200 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 201 202 tx.begin(); 203 assertEquals(VAL1, cache.get(NODE1, KEY)); 204 assertEquals(VAL1, cache.get(NODE2, KEY)); 205 assertEquals("we should hold 3 read locks now: ", 3, cache.getNumberOfLocksHeld()); 206 tx.commit(); 207 assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld()); 208 } 209 210 void testWriteLockRelease(IsolationLevel level) throws Exception { 211 cache=createCache(level); 212 cache.put(NODE1, KEY, VAL1); 214 cache.put(NODE2, KEY, VAL1); 215 216 assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld()); 217 218 tx.begin(); 219 cache.put(NODE1, KEY, VAL1); 220 cache.put(NODE2, KEY, VAL1); 221 assertEquals("we should hold 3 write locks now: ", 3, cache.getNumberOfLocksHeld()); 222 tx.commit(); 223 assertEquals("we should have released all 3 write locks: ", 0, cache.getNumberOfLocksHeld()); 224 } 225 226 void log(String msg) { 227 log.info("-- [" + Thread.currentThread() + "]: " + msg); 228 } 229 230 231 public static Test suite() throws Exception { 232 return new TestSuite(LockReleaseTest.class); 233 } 234 235 public static void main(String [] args) throws Exception { 236 junit.textui.TestRunner.run(suite()); 237 } 238 239 240 } 241 | Popular Tags |