1 package org.jboss.cache.transaction; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.CacheImpl; 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.config.Configuration; 11 import org.jboss.cache.factories.XmlConfigurationParser; 12 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.transaction.UserTransaction ; 16 import java.util.Properties ; 17 18 62 public class ReplicatedTransactionDeadlockTest extends TestCase 63 { 64 65 private static final int NUM_WORKERS = 2; 67 68 private static final int NUM_RUNS = 100; 70 71 private static final long LOCK_ACQUISITION_TIMEOUT = 10000; 73 74 77 private static final Properties PROPERTIES; 78 81 private static final String CONTEXT_FACTORY = 82 "org.jboss.cache.transaction.DummyContextFactory"; 83 86 private String m_contextFactory = null; 87 88 91 private static volatile Exception exception = null; 92 93 96 private CacheImpl srcCache = null; 97 100 private CacheImpl dstCache = null; 101 102 private Log log = LogFactory.getLog(ReplicatedTransactionDeadlockTest.class); 103 104 static 105 { 106 PROPERTIES = new Properties (); 107 PROPERTIES.put(Context.INITIAL_CONTEXT_FACTORY, 108 "org.jboss.cache.transaction.DummyContextFactory"); 109 } 110 111 116 public ReplicatedTransactionDeadlockTest(String name) 117 { 118 super(name); 119 } 120 121 124 protected void setUp() throws Exception 125 { 126 super.setUp(); 127 exception = null; 128 m_contextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 129 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY); 130 DummyTransactionManager.getInstance(); 131 srcCache = new CacheImpl(); 133 srcCache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); 134 srcCache.getConfiguration().setTransactionManagerLookupClass( 135 "org.jboss.cache.DummyTransactionManagerLookup"); 136 srcCache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 137 138 srcCache.getConfiguration().setSyncCommitPhase(true); 139 srcCache.getConfiguration().setLockAcquisitionTimeout(LOCK_ACQUISITION_TIMEOUT); 140 srcCache.create(); 141 srcCache.start(); 142 dstCache = new CacheImpl(); 144 dstCache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); 145 dstCache.getConfiguration().setTransactionManagerLookupClass( 146 "org.jboss.cache.DummyTransactionManagerLookup"); 147 dstCache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 148 149 dstCache.getConfiguration().setSyncCommitPhase(true); 150 dstCache.getConfiguration().setLockAcquisitionTimeout(LOCK_ACQUISITION_TIMEOUT); 151 dstCache.create(); 152 dstCache.start(); 153 } 154 155 158 protected void tearDown() throws Exception 159 { 160 super.tearDown(); 161 DummyTransactionManager.destroy(); 162 srcCache.stop(); 163 srcCache = null; 164 dstCache.stop(); 165 dstCache = null; 166 if (m_contextFactory != null) 167 { 168 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 169 m_contextFactory); 170 m_contextFactory = null; 171 } 172 } 173 174 181 public void testConcurrentReplicatedTransaction() throws Exception 182 { 183 performTest(); 184 } 185 186 191 private void performTest() throws Exception 192 { 193 for (int i = 0; i < NUM_RUNS; i++) 195 { 196 if (exception != null) 197 { 198 fail("Due to an exception: " + exception); 200 } 201 Worker[] t = new Worker[NUM_WORKERS]; 203 for (int j = 0; j < t.length; j++) 204 { 205 t[j] = new Worker("worker " + i + ":" + j); 206 t[j].start(); 207 } 208 for (Worker aT : t) aT.join(); 210 } 211 } 212 213 219 private UserTransaction getTransaction() throws Exception 220 { 221 return (UserTransaction ) new InitialContext (PROPERTIES) 222 .lookup("UserTransaction"); 223 } 224 225 232 private class Worker extends Thread 233 { 234 237 public Worker(String name) 238 { 239 super(name); 240 } 241 242 245 public void run() 246 { 247 try 248 { 249 UserTransaction tx = getTransaction(); 250 log.warn("begin"); 251 tx.begin(); 252 log.warn("put"); 253 srcCache.put(new Fqn("Node"), Boolean.FALSE, Boolean.TRUE); 254 log.warn("commit"); 255 tx.commit(); 256 log.warn("leave"); 257 } 258 catch (Exception e) 259 { 260 log.error("caught exception " + e, e); 261 exception = e; 262 } 263 } 264 } 265 266 public static Test suite() 267 { 268 return new TestSuite(ReplicatedTransactionDeadlockTest.class); 269 } 270 271 public static void main(String [] args) throws Exception 272 { 273 junit.textui.TestRunner.run(suite()); 274 } 275 } 276 | Popular Tags |