1 package org.jboss.cache.loader; 2 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 import org.jboss.cache.CacheImpl; 6 import org.jboss.cache.Fqn; 7 import org.jboss.cache.misc.TestingUtil; 8 import org.jboss.cache.transaction.DummyTransactionManager; 9 10 import javax.transaction.NotSupportedException ; 11 import javax.transaction.RollbackException ; 12 import javax.transaction.SystemException ; 13 import javax.transaction.Transaction ; 14 import java.io.File ; 15 import java.util.Set ; 16 17 21 public class TxCacheLoaderTest extends AbstractCacheLoaderTestBase 22 { 23 CacheImpl cache1, cache2; 24 private Fqn fqn = Fqn.fromString("/one/two/three"); 25 26 protected void setUp() throws Exception 27 { 28 super.setUp(); 29 30 String tmpLoc = System.getProperty("java.io.tmpdir", "/tmp"); 31 String location = tmpLoc + File.separator + "TxCacheLoaderTest1"; 32 33 cache1 = new CacheImpl(); 34 cache1.getConfiguration().setCacheMode("repl_sync"); 35 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 36 37 cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + location, false, false, false)); 38 cache1.create(); 40 cache1.start(); 41 42 location = tmpLoc + File.separator + "TxCacheLoaderTest2"; 43 44 cache2 = new CacheImpl(); 45 cache2.getConfiguration().setCacheMode("repl_sync"); 46 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 47 cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + location, false, false, false)); 48 cache2.getConfiguration().setLockAcquisitionTimeout(2000); 49 cache2.create(); 51 cache2.start(); 52 } 53 54 protected void tearDown() throws Exception 55 { 56 super.tearDown(); 57 58 cache1.remove(Fqn.ROOT); 60 61 cache1.stop(); 62 cache1.destroy(); 63 cache2.stop(); 64 cache2.destroy(); 65 } 66 67 68 public void testTxPutCommit() throws Exception , NotSupportedException 69 { 70 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 71 mgr.begin(); 72 Transaction tx = mgr.getTransaction(); 73 74 75 cache1.put(fqn, "key1", "val1"); 76 cache1.put("/one/two/three/four", "key2", "val2"); 77 assertNull(cache2.get(fqn, "key1")); 78 assertNull(cache2.get("/one/two/three/four", "key2")); 79 tx.commit(); 80 assertNotNull(cache1.getKeys(fqn)); 81 Set children = cache1.getChildrenNames("/one"); 82 assertEquals(1, children.size()); 83 TestingUtil.sleepThread(2000); 84 assertEquals("val1", cache2.get(fqn, "key1")); 85 assertEquals("val2", cache2.get("/one/two/three/four", "key2")); 86 } 87 88 89 public void testTxPrepareAndRollback() throws Exception , NotSupportedException 90 { 91 final DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 92 mgr.begin(); 93 Transaction tx1 = mgr.getTransaction(); 94 95 cache1.getConfiguration().setLockAcquisitionTimeout(1500); 96 cache2.getConfiguration().setLockAcquisitionTimeout(1500); 97 98 99 Thread locker = new Thread () 100 { 101 Transaction tx2 = null; 102 103 public void run() 104 { 105 try 106 { 107 mgr.begin(); 108 tx2 = mgr.getTransaction(); 109 cache2.put(fqn, "block-key1", "block-val1"); TestingUtil.sleepThread(5000); 111 } 112 catch (Exception e) 113 { 114 e.printStackTrace(); 115 } 116 finally 117 { 118 if (tx2 != null) 119 { 120 try 121 { 122 tx2.rollback(); 123 } 124 catch (SystemException e) 125 { 126 e.printStackTrace(); 127 } 128 } 129 } 130 } 131 }; 132 133 locker.start(); 134 TestingUtil.sleepThread(1000); 135 136 cache1.put(fqn, "key1", "val1"); 137 cache1.put("/one/two/three/four", "key2", "val2"); 138 139 try 140 { 141 tx1.commit(); fail("commit() should fail because we cannot acquire the lock on cache2"); 143 } 144 catch (RollbackException rollback) 145 { 146 System.out.println("--- TX was rolled back (as expected)"); 147 assertTrue(true); 148 } 149 150 assertNull(cache1.get(fqn, "key1")); 151 assertNull(cache1.get("/one/two/three/four", "key1")); 152 153 } 154 155 156 public void testPutAfterTxCommit() throws Exception , NotSupportedException 157 { 158 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 159 mgr.begin(); 160 Transaction tx = mgr.getTransaction(); 161 162 cache1.put(fqn, "key1", "val1"); 163 assertTrue(cache1.exists(fqn)); 164 tx.commit(); 165 assertTrue(cache1.exists(fqn)); 166 cache1.put("/a/b/c", null); assertTrue(cache1.exists("/a/b/c")); 168 } 169 170 public void testPutAfterTxRollback() throws Exception , NotSupportedException 171 { 172 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 173 mgr.begin(); 174 Transaction tx = mgr.getTransaction(); 175 176 cache1.put(fqn, "key1", "val1"); 177 assertTrue(cache1.exists(fqn)); 178 tx.rollback(); 179 assertFalse(cache1.getCacheLoader().exists(fqn)); 180 assertFalse(cache1.exists(fqn)); 181 cache1.put("/a/b/c", null); assertTrue(cache1.exists("/a/b/c")); 183 } 184 185 public static Test suite() 186 { 187 return new TestSuite(TxCacheLoaderTest.class); 188 } 189 190 public static void main(String [] args) 191 { 192 junit.textui.TestRunner.run(suite()); 193 } 194 195 } 196 | Popular Tags |