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