1 22 23 package org.jboss.cache.lock; 24 25 import junit.framework.TestCase; 26 import org.jboss.cache.CacheImpl; 27 import org.jboss.cache.factories.XmlConfigurationParser; 28 import org.jboss.cache.misc.TestingUtil; 29 30 import javax.transaction.Synchronization ; 31 import javax.transaction.Transaction ; 32 import javax.transaction.TransactionManager ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 43 public class BreakDeadMemberLocksTest extends TestCase 44 { 45 46 private Map caches; 47 48 protected void setUp() throws Exception 49 { 50 super.setUp(); 51 52 caches = new HashMap (); 53 } 54 55 public void testBreakDeadMemberLocks() throws Exception 56 { 57 CacheImpl cacheA = createCache("A"); 58 59 cacheA.put("/1/A", "1", "A"); 60 cacheA.put("/1/A", "2", "A"); 61 cacheA.put("/2/A", "1", "A"); 62 cacheA.put("/2/A", "2", "A"); 63 cacheA.put("/1/A/I", "1", "A"); 64 cacheA.put("/1/A/I", "2", "A"); 65 66 CacheImpl cacheB = createCache("B"); 67 68 TestingUtil.blockUntilViewsReceived(new CacheImpl[]{cacheA, cacheB}, 60000); 70 71 final TransactionManager tm = cacheB.getTransactionManager(); 72 tm.begin(); 73 final Transaction tx = tm.getTransaction(); 74 75 cacheB.put("/1/A", "1", "B"); 76 cacheB.put("/1/A", "2", "B"); 77 cacheB.put("/2/A", "1", "B"); 78 cacheB.put("/2/A", "2", "B"); 79 cacheB.put("/1/A/I", "1", "B"); 80 cacheB.put("/1/A/I", "2", "B"); 81 cacheB.put("/EXISTS", "KEY", "B"); 82 83 Object monitor = new Object (); 84 HangSync sync = new HangSync(monitor); 85 tx.registerSynchronization(sync); 86 87 Thread t = new Thread () 88 { 89 public void run() 90 { 91 try 92 { 93 tm.resume(tx); 94 tx.commit(); 95 } 96 catch (Exception e) {} 97 } 98 }; 99 100 synchronized (monitor) 101 { 102 t.start(); 103 104 while (!sync.hung) 105 monitor.wait(500); 106 } 107 108 tm.suspend(); 109 110 assertTrue(cacheA.exists("/EXISTS")); 112 113 cacheB.stop(); 114 cacheB.destroy(); 115 116 while (cacheA.getMembers().size() > 1) 117 { 118 try 119 { 120 Thread.sleep(100); 121 } 122 catch (InterruptedException e) {} 123 } 124 125 assertEquals("A", cacheA.get("/1/A", "1")); 126 assertEquals("A", cacheA.get("/1/A", "2")); 127 assertEquals("A", cacheA.get("/2/A", "1")); 128 assertEquals("A", cacheA.get("/2/A", "2")); 129 assertEquals("A", cacheA.get("/1/A/I", "1")); 130 assertEquals("A", cacheA.get("/1/A/I", "2")); 131 132 if (t.isAlive()) 133 { 134 t.interrupt(); 135 } 136 } 137 138 protected CacheImpl createCache(String cacheID) throws Exception 139 { 140 if (caches.get(cacheID) != null) 141 throw new IllegalStateException (cacheID + " already created"); 142 143 CacheImpl cache = new CacheImpl(); 144 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); 145 146 cache.create(); 147 cache.start(); 148 149 caches.put(cacheID, cache); 150 151 return cache; 152 } 153 154 protected void tearDown() throws Exception 155 { 156 super.tearDown(); 157 158 Set keys = caches.keySet(); 159 String [] cacheIDs = new String [keys.size()]; 160 cacheIDs = (String []) keys.toArray(cacheIDs); 161 for (int i = 0; i < cacheIDs.length; i++) 162 { 163 stopCache(cacheIDs[i]); 164 } 165 } 166 167 protected void stopCache(String id) 168 { 169 CacheImpl cache = (CacheImpl) caches.get(id); 170 if (cache != null) 171 { 172 try 173 { 174 cache.stop(); 175 cache.destroy(); 176 caches.remove(id); 177 } 178 catch (Exception e) 179 { 180 System.out.println("Exception stopping cache " + e.getMessage()); 181 e.printStackTrace(System.out); 182 } 183 } 184 } 185 186 class HangSync implements Synchronization 187 { 188 private boolean hung = false; 189 private Object monitor; 190 191 HangSync(Object monitor) 192 { 193 this.monitor = monitor; 194 } 195 196 public void afterCompletion(int arg0) 197 { 198 } 199 200 public void beforeCompletion() 201 { 202 hung = true; 203 synchronized (monitor) 204 { 205 monitor.notifyAll(); 206 } 207 try 208 { 209 Thread.sleep(30000); 210 } 211 catch (InterruptedException e) {} 212 } 213 214 215 } 216 } 217 | Popular Tags |