1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.ConfigLockLevel; 8 import com.tc.object.config.DSOClientConfigHelper; 9 import com.tc.object.config.TransparencyClassSpec; 10 import com.tc.simulator.app.ApplicationConfig; 11 import com.tc.simulator.listener.ListenerProvider; 12 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 public class NestedConcurrentLockTest extends TransparentTestBase { 19 20 private static final int NODE_COUNT = 3; 21 22 public void setUp() throws Exception { 23 super.setUp(); 24 getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1); 25 initializeTestRunner(); 26 } 27 28 protected Class getApplicationClass() { 29 return NestedConcurrentLockTestApp.class; 30 } 31 32 public static class NestedConcurrentLockTestApp extends AbstractErrorCatchingTransparentApp { 33 private static final int NUM = 1000; 34 private final Object concurrentLock = new Object (); 35 private final List list = new ArrayList (); 36 37 public NestedConcurrentLockTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 38 super(appId, cfg, listenerProvider); 39 } 40 41 protected void runTest() throws Throwable { 42 for (int i = 1; i <= NUM; i++) { 43 concurrent(); 44 if ((i % 100) == 0) { 45 System.out.println(getApplicationId() + " has reached " + i); 46 } 47 } 48 } 49 50 private void concurrent() { 51 synchronized (concurrentLock) { 52 write(); 53 } 54 } 55 56 private void write() { 57 synchronized (list) { 58 int add = validate(); 59 list.add(new Integer (add)); 60 } 61 } 62 63 private int validate() { 64 int expect = 0; 65 for (Iterator iter = list.iterator(); iter.hasNext();) { 66 Integer integer = (Integer ) iter.next(); 67 if (integer.intValue() != expect) { throw new RuntimeException ("Expected " + expect + ", but was " 68 + integer.intValue() + "\n" + list); } 69 expect++; 70 } 71 72 return expect; 73 } 74 75 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 76 String testClass = NestedConcurrentLockTestApp.class.getName(); 77 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 78 79 String methodExpression = "* " + testClass + ".concurrent(..)"; 80 config.addAutolock(methodExpression, ConfigLockLevel.CONCURRENT); 81 82 methodExpression = "* " + testClass + ".write(..)"; 83 config.addAutolock(methodExpression, ConfigLockLevel.WRITE); 84 85 spec.addRoot("list", "list"); 86 spec.addRoot("concurrentLock", "concurrentLock"); 87 } 88 89 } 90 91 } 92 | Popular Tags |