1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.DSOClientConfigHelper; 8 import com.tc.object.tx.ReadOnlyException; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tctest.runner.AbstractTransparentApp; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.Random ; 16 17 public class LockUpgradeSystemTestApp extends AbstractTransparentApp { 18 19 private Map root = new HashMap (); 20 21 public LockUpgradeSystemTestApp(String globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 22 super(globalId, cfg, listenerProvider); 23 } 24 25 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 26 String testClassName = LockUpgradeSystemTestApp.class.getName(); 27 28 config.addRoot(testClassName, "root", "root" + "Lock", true); 29 30 String methodExpression = "* " + testClassName + ".writeLock(..)"; 31 config.addWriteAutolock(methodExpression); 32 33 methodExpression = "* " + testClassName + ".readLock(..)"; 34 config.addReadAutolock(methodExpression); 35 } 36 37 public void run() { 38 Random random = new Random (new Random (System.currentTimeMillis() + getApplicationId().hashCode()).nextLong()); 39 40 for (int i = 0; i < 100; i++) { 41 readLock(random.nextInt(4)); 42 } 43 } 44 45 private void readLock(int depth) { 46 synchronized (root) { 47 49 writeLock(depth); 51 52 tryWrite(); 54 } 55 } 56 57 private void readLock() { 58 synchronized (root) { 59 tryWrite(); 60 } 61 } 62 63 private void tryWrite() { 64 try { 65 root.put("key", "value"); 66 throw new RuntimeException ("read-only transaction context is busted"); 67 } catch ( ReadOnlyException roe) { 68 } 70 } 71 72 private void writeLock(int depth) { 73 synchronized (root) { 74 readLock(); 75 writeLock(); 76 77 if (depth > 0) { 78 writeLock(depth - 1); 79 } 80 } 81 } 82 83 private void writeLock() { 84 synchronized (root) { 85 } 87 } 88 89 } 90 | Popular Tags |