1 7 package org.jboss.cache.tests; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.TreeCache; 13 import org.jboss.cache.transaction.DummyTransactionManager; 14 import org.jgroups.util.Util; 15 16 import javax.transaction.NotSupportedException ; 17 import javax.transaction.Transaction ; 18 import javax.transaction.SystemException ; 19 import javax.transaction.RollbackException ; 20 import java.util.Set ; 21 22 28 public class TxCacheLoaderTest extends TestCase { 29 TreeCache cache1, cache2; 30 31 protected void setUp() throws Exception { 32 super.setUp(); 33 cache1=new TreeCache(); 34 cache1.setCacheMode("repl_sync"); 35 cache1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 36 cache1.createService(); 38 cache1.startService(); 39 40 cache2=new TreeCache(); 41 cache2.setCacheMode("repl_sync"); 42 cache2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 43 cache2.setLockAcquisitionTimeout(2000); 44 cache2.createService(); 46 cache2.startService(); 47 } 48 49 protected void tearDown() throws Exception { 50 super.tearDown(); 51 cache1.stopService(); 52 cache1.destroyService(); 53 cache2.stopService(); 54 cache2.destroyService(); 55 } 56 57 58 59 60 61 public void testTxPutCommit() throws Exception , NotSupportedException { 62 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 63 mgr.begin(); 64 Transaction tx=mgr.getTransaction(); 65 66 67 cache1.put("/one/two/three", "key1", "val1"); 68 cache1.put("/one/two/three/four", "key2", "val2"); 69 assertNull(cache2.get("/one/two/three", "key1")); 70 assertNull(cache2.get("/one/two/three/four", "key2")); 71 tx.commit(); 72 assertNotNull(cache1.getKeys("/one/two/three")); 73 Set children=cache1.getChildrenNames("/one"); 74 assertEquals(1, children.size()); 75 Util.sleep(100); 76 assertEquals("val1", cache2.get("/one/two/three", "key1")); 77 assertEquals("val2", cache2.get("/one/two/three/four", "key2")); 78 } 79 80 81 82 public void testTxPrepareAndRollback() throws Exception , NotSupportedException { 83 final DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 84 mgr.begin(); 85 Transaction tx1=mgr.getTransaction(); 86 87 cache1.setLockAcquisitionTimeout(1500); 88 cache2.setLockAcquisitionTimeout(1500); 89 90 91 Thread locker=new Thread () { 92 Transaction tx2=null; 93 public void run() { 94 try { 95 mgr.begin(); 96 tx2=mgr.getTransaction(); 97 cache2.put("/one/two/three", "block-key1", "block-val1"); Util.sleep(5000); 99 } 100 catch(Exception e) { 101 e.printStackTrace(); 102 } 103 finally { 104 if(tx2 != null) { 105 try { 106 tx2.rollback(); 107 } 108 catch(SystemException e) { 109 e.printStackTrace(); 110 } 111 } 112 } 113 } 114 }; 115 116 locker.start(); 117 Util.sleep(1000); 118 119 cache1.put("/one/two/three", "key1", "val1"); 120 cache1.put("/one/two/three/four", "key2", "val2"); 121 122 try { 123 tx1.commit(); fail("commit() should fail because we cannot acquire the lock on cache2"); 125 } 126 catch(RollbackException rollback) { 127 System.out.println("--- TX was rolled back (as expected)"); 128 assertTrue(true); 129 } 130 131 assertNull(cache1.get("/one/two/three", "key1")); 132 assertNull(cache1.get("/one/two/three/four", "key1")); 133 134 } 135 136 137 public void testPutAfterTxCommit() throws Exception , NotSupportedException { 138 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 139 mgr.begin(); 140 Transaction tx=mgr.getTransaction(); 141 142 cache1.put("/one/two/three", "key1", "val1"); 143 assertTrue(cache1.exists("/one/two/three")); 144 tx.commit(); 145 assertTrue(cache1.exists("/one/two/three")); 146 cache1.put("/a/b/c", null); assertTrue(cache1.exists("/a/b/c")); 148 } 149 150 public void testPutAfterTxRollback() throws Exception , NotSupportedException { 151 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 152 mgr.begin(); 153 Transaction tx=mgr.getTransaction(); 154 155 cache1.put("/one/two/three", "key1", "val1"); 156 assertTrue(cache1.exists("/one/two/three")); 157 tx.rollback(); 158 assertFalse(cache1.exists("/one/two/three")); 159 cache1.put("/a/b/c", null); assertTrue(cache1.exists("/a/b/c")); 161 } 162 163 164 165 166 public static Test suite() { 167 return new TestSuite(TxCacheLoaderTest.class); 168 } 169 170 public static void main(String [] args) { 171 junit.textui.TestRunner.run(suite()); 172 } 173 174 } 175 | Popular Tags |