1 package org.jboss.cache.transaction; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheImpl; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.config.Configuration; 7 import org.jboss.cache.factories.DefaultCacheFactory; 8 9 import javax.transaction.SystemException ; 10 import javax.transaction.TransactionManager ; 11 12 18 public class AsyncRollbackTxTest extends TestCase 19 { 20 private CacheImpl cache; 21 private TransactionManager tm; 22 private Fqn fqn = Fqn.fromString("/test"); 23 private long sleepTime = 2500; 25 private int txTimeout = 2; 27 28 protected void setUp() throws Exception 29 { 30 Configuration c = new Configuration(); 31 c.setTransactionManagerLookupClass("org.jboss.cache.transaction.AsyncRollbackTransactionManagerLookup"); 32 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c); 33 tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager(); 34 tm.setTransactionTimeout(txTimeout); 35 } 36 37 protected void tearDown() 38 { 39 try 40 { 41 if (tm != null && tm.getTransaction() != null) 42 { 43 try 44 { 45 tm.rollback(); 46 } 47 catch (SystemException e) 48 { 49 } 51 } 52 } 53 catch (SystemException e) 54 { 55 } 57 if (cache != null) cache.stop(); 58 cache = null; 59 tm = null; 60 } 61 62 public void testCommitCreationInSameTx() throws Exception 63 { 64 assertEquals(0, cache.getNumberOfLocksHeld()); 65 tm.begin(); 66 cache.put(fqn, "k", "v"); 67 assertEquals(2, cache.getNumberOfLocksHeld()); 68 Thread.sleep(sleepTime); 69 tm.commit(); 70 assertEquals(0, cache.getNumberOfLocksHeld()); 71 } 72 73 public void testRollbackCreationInSameTx() throws Exception 74 { 75 assertEquals(0, cache.getNumberOfLocksHeld()); 76 tm.begin(); 77 cache.put(fqn, "k", "v"); 78 assertEquals(2, cache.getNumberOfLocksHeld()); 79 Thread.sleep(sleepTime); 80 tm.rollback(); 81 assertEquals(0, cache.getNumberOfLocksHeld()); 82 assertFalse(cache.exists(fqn)); 84 assertNull(cache.peek(fqn)); 86 } 87 88 89 private void doTest(boolean commit, boolean writeLock) throws Exception 90 { 91 assertEquals(0, cache.getNumberOfLocksHeld()); 92 cache.put(fqn, "k", "v"); assertEquals(0, cache.getNumberOfLocksHeld()); 94 SeparateThread t = new SeparateThread(commit, writeLock); 95 t.start(); 96 t.join(); 97 if (t.getException() != null) 98 { 99 throw t.getException(); 100 } 101 assertEquals(0, cache.getNumberOfLocksHeld()); 102 assertEquals("v", cache.get(fqn, "k")); 103 } 104 105 public void testRollbackCreationInDifferentTxReadLock() throws Exception 106 { 107 doTest(false, false); 108 } 109 110 public void testCommitCreationInDifferentTxReadLock() throws Exception 111 { 112 doTest(true, false); 113 } 114 115 public void testRollbackCreationInDifferentTxWriteLock() throws Exception 116 { 117 doTest(false, true); 118 } 119 120 public void testCommitCreationInDifferentTxWriteLock() throws Exception 121 { 122 doTest(true, true); 123 } 124 125 public void testTxTimeoutAndPutAfter() throws Exception 126 { 127 assertEquals(0, cache.getNumberOfLocksHeld()); 128 tm.begin(); 129 cache.put(fqn, "k", "v"); 130 assertEquals(2, cache.getNumberOfLocksHeld()); 131 assertNotNull(tm.getTransaction()); 132 Thread.sleep(sleepTime); 133 tm.rollback(); 134 assertNull(tm.getTransaction()); 135 assertEquals(0, cache.getNumberOfLocksHeld()); 136 137 assertFalse(cache.exists(fqn)); 139 assertNull(cache.peek(fqn)); 141 142 cache.put(fqn, "k", "v"); 145 assertEquals(0, cache.getNumberOfLocksHeld()); 146 } 147 148 149 public void testTxTimeoutAndPutGetAfter() throws Exception 150 { 151 assertEquals(0, cache.getNumberOfLocksHeld()); 152 tm.begin(); 153 cache.put(fqn, "k", "v"); 154 assertEquals(2, cache.getNumberOfLocksHeld()); 155 assertNotNull(tm.getTransaction()); 156 Thread.sleep(sleepTime); 157 tm.rollback(); 158 assertFalse(cache.exists(fqn)); 160 assertNull(cache.peek(fqn)); 162 assertNull(tm.getTransaction()); 163 assertEquals(0, cache.getNumberOfLocksHeld()); 164 165 cache.put(fqn, "k", "v"); 168 cache.get(fqn, "k"); 169 170 SeparateThread t = new SeparateThread(false, false); 172 t.start(); 173 t.join(); 174 if (t.getException() != null) 175 { 176 throw t.getException(); 177 } 178 } 179 180 181 private class SeparateThread extends Thread 182 { 183 Exception e = null; 184 boolean commit, writeLock; 185 186 187 public SeparateThread(boolean commit, boolean writeLock) 188 { 189 this.commit = commit; 190 this.writeLock = writeLock; 191 } 192 193 public Exception getException() 194 { 195 return e; 196 } 197 198 public void run() 199 { 200 try 201 { 202 tm.begin(); 203 if (writeLock) 204 { 205 cache.put(fqn, "k", "v2"); } 207 else 208 { 209 cache.get(fqn, "k"); } 211 212 sleep(2500); 214 215 if (commit) 216 { 217 tm.commit(); 218 } 219 else 220 { 221 tm.rollback(); 222 } 223 224 assertEquals(0, cache.getNumberOfLocksHeld()); 225 226 } 227 catch (Exception e) 228 { 229 this.e = e; 230 } 231 } 232 } 233 234 ; 235 } 236 237 | Popular Tags |