1 package org.jboss.cache.loader.deadlock; 2 3 import EDU.oswego.cs.dl.util.concurrent.Latch; 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.config.Configuration; 9 import org.jboss.cache.factories.XmlConfigurationParser; 10 import org.jboss.cache.loader.AbstractCacheLoaderTestBase; 11 import org.jboss.cache.transaction.DummyTransactionManager; 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 ConcurrentCreationDeadlockTest extends AbstractCacheLoaderTestBase 63 { 64 67 private static final int NUM_WORKERS = 10; 68 71 private static final int NUM_RUNS = 100; 72 75 private static final int NUM_FQNS_PER_RUN = 10; 76 77 80 private static final Properties PROPERTIES; 81 84 private static final String CONTEXT_FACTORY = 85 "org.jboss.cache.transaction.DummyContextFactory"; 86 89 private String m_contextFactory = null; 90 91 94 private static volatile Exception mcl_exception = null; 95 96 99 private CacheImpl cache = null; 100 101 static 102 { 103 PROPERTIES = new Properties (); 104 PROPERTIES.put(Context.INITIAL_CONTEXT_FACTORY, 105 "org.jboss.cache.transaction.DummyContextFactory"); 106 } 107 108 113 public ConcurrentCreationDeadlockTest(String name) 114 { 115 super(name); 116 } 117 118 121 public void setUp() throws Exception 122 { 123 super.setUp(); 124 mcl_exception = null; 125 m_contextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 126 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY); 127 DummyTransactionManager.getInstance(); 128 cache = new CacheImpl(); 129 130 XmlConfigurationParser parser = new XmlConfigurationParser(); 131 Configuration c = parser.parseFile("META-INF/replSync-service.xml"); 132 cache.setConfiguration(c); 133 134 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 135 } 136 137 140 public void tearDown() throws Exception 141 { 142 super.tearDown(); 143 DummyTransactionManager.destroy(); 144 cache.stop(); 145 cache = null; 146 if (m_contextFactory != null) 147 { 148 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 149 m_contextFactory); 150 m_contextFactory = null; 151 } 152 } 153 154 161 private void startCache(Configuration.CacheMode cacheMode, String cacheLoaderClass) 162 throws Exception 163 { 164 cache.getConfiguration().setCacheMode(cacheMode); 165 if (cacheLoaderClass != null) 166 { 167 cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", cacheLoaderClass, "", false, false, false)); 168 } 169 cache.create(); 170 cache.start(); 171 } 172 173 180 public void disabledtestLocalNoCacheLoader1Modification() throws Exception 181 { 182 startCache(Configuration.CacheMode.LOCAL, null); 183 performTest(1); 184 } 185 186 193 public void disabledtestLocalNoCacheLoader2Modifications() 194 throws Exception 195 { 196 startCache(Configuration.CacheMode.LOCAL, null); 197 performTest(2); 198 } 199 200 207 public void disabledtestLocalCacheLoader1Modification() throws Exception 208 { 209 startCache(Configuration.CacheMode.LOCAL, "org.jboss.cache.loader.DummyCacheLoader"); 210 performTest(1); 211 } 212 213 220 public void testLocalCacheLoader2Modifications() throws Exception 221 { 222 startCache(Configuration.CacheMode.LOCAL, "org.jboss.cache.loader.DummyCacheLoader"); 223 performTest(2); 224 } 225 226 233 public void disabledtestReplAsyncNoCacheLoader1Modification() 234 throws Exception 235 { 236 startCache(Configuration.CacheMode.REPL_ASYNC, null); 237 performTest(1); 238 } 239 240 247 public void disabledtestReplAsyncNoCacheLoader2Modifications() 248 throws Exception 249 { 250 startCache(Configuration.CacheMode.REPL_ASYNC, null); 251 performTest(2); 252 } 253 254 261 public void disabledtestReplAsyncCacheLoader1Modification() 262 throws Exception 263 { 264 startCache(Configuration.CacheMode.REPL_ASYNC, "org.jboss.cache.loader.DummyCacheLoader"); 265 performTest(1); 266 } 267 268 276 public void disabledtestReplAsyncCacheLoader2Modifications() 277 throws Exception 278 { 279 startCache(Configuration.CacheMode.REPL_ASYNC, "org.jboss.cache.loader.DummyCacheLoader"); 280 performTest(2); 281 } 282 283 290 public void disabledtestReplSyncNoCacheLoader1Modification() 291 throws Exception 292 { 293 startCache(Configuration.CacheMode.REPL_SYNC, null); 294 performTest(1); 295 } 296 297 304 public void disabledtestReplSyncNoCacheLoader2Modifications() 305 throws Exception 306 { 307 startCache(Configuration.CacheMode.REPL_SYNC, null); 308 performTest(2); 309 } 310 311 318 public void disabledtestReplSyncCacheLoader1Modification() throws Exception 319 { 320 startCache(Configuration.CacheMode.REPL_SYNC, "org.jboss.cache.loader.DummyCacheLoader"); 321 performTest(1); 322 } 323 324 331 public void testReplSyncCacheLoader2Modifications() 332 throws Exception 333 { 334 startCache(Configuration.CacheMode.REPL_SYNC, "org.jboss.cache.loader.DummyCacheLoader"); 335 performTest(2); 336 } 337 338 344 private void performTest(int modificationsPerTx) throws Exception 345 { 346 for (int i = 0; i < NUM_RUNS; i++) 347 { 348 if (mcl_exception != null) 349 { 350 fail("Due to an exception: " + mcl_exception); 352 } 353 Worker[] t = new Worker[NUM_WORKERS]; 355 Latch latch = new Latch(); 356 for (int j = 0; j < t.length; j++) 357 { 358 t[j] = new Worker(latch, NUM_FQNS_PER_RUN * i, 359 NUM_FQNS_PER_RUN, modificationsPerTx); 360 t[j].start(); 361 } 362 latch.release(); 364 for (int j = 0; j < t.length; j++) 366 { 367 t[j].join(); 368 } 369 } 370 } 371 372 378 private UserTransaction getTransaction() throws Exception 379 { 380 return (UserTransaction ) new InitialContext (PROPERTIES) 381 .lookup("UserTransaction"); 382 } 383 384 389 private void log(String msg) 390 { 391 System.out.println(System.currentTimeMillis() + " " 392 + Thread.currentThread() + " " + msg); 393 } 394 395 402 private class Worker extends Thread 403 { 404 407 private final Latch m_latch; 408 411 private final int m_start; 412 415 private final int m_count; 416 419 private final int m_modificationsPerTx; 420 421 424 private int m_state; 425 426 435 public Worker(Latch latch, int start, int count, int modificationsPerTx) 436 { 437 m_latch = latch; 438 m_start = start; 439 m_count = count; 440 m_state = -1; 441 m_modificationsPerTx = modificationsPerTx; 442 } 443 444 447 public void run() 448 { 449 try 450 { 451 m_latch.acquire(); 453 for (int i = m_start; i < m_start + m_count; i++) 454 { 455 m_state = -1; 456 log("enter " + i); 457 if (checkAndSetState()) 458 { 459 return; 460 } 461 long time = System.currentTimeMillis(); 462 UserTransaction tx = getTransaction(); 463 tx.begin(); 464 if (checkAndSetState()) 465 { 466 return; 467 } 468 Fqn fqn = new Fqn(new Fqn("NODE"), i); 471 for (int m = 0; m < m_modificationsPerTx; m++) 472 { 473 cache.put(fqn, new Integer (m), new Integer (i)); 474 if (checkAndSetState()) 475 { 476 return; 477 } 478 } 479 tx.commit(); 480 if (checkAndSetState()) 481 { 482 return; 483 } 484 time = System.currentTimeMillis() - time; 485 log("leave " + i + " took " + time + " msec"); 486 } 487 } 488 catch (Exception e) 489 { 490 log("caught exception in state " + m_state + ": " + e); 491 mcl_exception = e; 492 } 493 } 494 495 500 private boolean checkAndSetState() 501 { 502 if (mcl_exception != null) 503 { 504 log("detected failure in state " + m_state); 506 return true; 507 } 508 m_state++; 509 return false; 510 } 511 } 512 513 public static Test suite() 514 { 515 516 return new TestSuite(ConcurrentCreationDeadlockTest.class); 517 518 } 519 520 521 public static void main(String [] args) throws Exception 522 { 523 524 junit.textui.TestRunner.run(suite()); 525 526 } 527 } 528 | Popular Tags |