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.object.tx.ReadOnlyException; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tctest.runner.AbstractTransparentApp; 14 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 public class WaitNotifyUpgradedLocksTestApp extends AbstractTransparentApp { 21 22 private static final int NODES = 2; 23 24 private final Set set = new HashSet (); 25 private final Map map = new HashMap (); 26 27 public WaitNotifyUpgradedLocksTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 28 super(appId, cfg, listenerProvider); 29 30 if (getParticipantCount() != NODES) { throw new RuntimeException ("only use " + NODES + " nodes with this test"); } 31 } 32 33 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 34 String testClass = WaitNotifyUpgradedLocksTestApp.class.getName(); 35 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 36 config.addIncludePattern(testClass + "$*"); 37 38 String methodExpression = "* " + testClass + ".run()"; 39 config.addWriteAutolock(methodExpression); 40 41 methodExpression = "* " + testClass + "$UpgradeWait.run()"; 42 config.addWriteAutolock(methodExpression); 43 44 methodExpression = "* " + testClass + ".runRead(..)"; 45 config.addAutolock(methodExpression, ConfigLockLevel.READ); 46 47 methodExpression = "* " + testClass + ".runWrite(..)"; 48 config.addAutolock(methodExpression, ConfigLockLevel.WRITE); 49 50 spec.addRoot("set", "setLock"); 51 spec.addRoot("map", "mapLock"); 52 } 53 54 public void run() { 55 final boolean first; 56 57 synchronized (set) { 58 first = set.size() == 0; 59 if (first) set.add(new Object ()); 60 } 61 62 if (first) { 63 runUpgrade(map, new UpgradeWait()); 64 } else { 65 synchronized (set) { 66 while (set.size() != NODES) { 67 try { 68 set.wait(); 69 } catch (InterruptedException e) { 70 throw new RuntimeException (e); 71 } 72 } 73 } 74 75 runUpgrade(map, new Runnable () { 76 public void run() { 77 map.notify(); 78 } 79 }); 80 } 81 } 82 83 public void runUpgrade(final Map m, final Runnable action) { 84 runRead(m, new Runnable () { 85 public void run() { 86 runWrite(m, action); 87 } 88 }); 89 } 90 91 public void runRead(Map m, Runnable action) { 92 synchronized (m) { 93 action.run(); 94 95 try { 96 m.put(new Object (), null); 97 throw new RuntimeException ("Not in a read only context"); 98 } catch (ReadOnlyException r) { 99 } 101 } 102 } 103 104 public void runWrite(Map m, Runnable action) { 105 synchronized (m) { 106 action.run(); 107 } 108 } 109 110 private class UpgradeWait implements Runnable { 111 public void run() { 112 synchronized (set) { 114 set.add(new Object ()); 115 set.notify(); 116 } 117 118 try { 119 map.wait(); 120 } catch (InterruptedException e) { 121 throw new RuntimeException (e); 122 } 123 } 124 } 125 126 } 127
| Popular Tags
|