1 5 package com.tctest; 6 7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 9 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.DSOClientConfigHelper; 12 import com.tc.object.config.DistributedMethodSpec; 13 import com.tc.object.config.TransparencyClassSpec; 14 import com.tc.object.config.spec.CyclicBarrierSpec; 15 import com.tc.object.config.spec.SynchronizedIntSpec; 16 import com.tc.simulator.app.ApplicationConfig; 17 import com.tc.simulator.listener.ListenerProvider; 18 import com.tctest.runner.AbstractTransparentApp; 19 20 24 public class DistributedMethodCallExpressionTestApp extends AbstractTransparentApp { 25 26 private final SharedModel model = new SharedModel(); 27 private final int initialNodeCount = getParticipantCount(); 28 private final CyclicBarrier nodeBarrier = new CyclicBarrier(initialNodeCount); 29 private final CyclicBarrier staticNodeBarrier = new CyclicBarrier(initialNodeCount); 30 31 public DistributedMethodCallExpressionTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 32 super(appId, cfg, listenerProvider); 33 } 34 35 public void run() { 36 try { 37 callNonStaticMethod(); 38 callStaticMethod(); 39 } catch (Throwable e) { 40 notifyError(e); 41 } 42 } 43 44 public void callNonStaticMethod() throws Throwable { 45 final boolean masterNode = nodeBarrier.barrier() == 0; 46 synchronized (model) { 47 if (masterNode) { 48 model.nonStaticMethod(null, 0, 0, null, null, false); 49 } 50 } 51 nodeBarrier.barrier(); 52 System.err.println("\n### initialNodeCount = " + initialNodeCount); 53 synchronized (model) { 54 checkCountTimed(model.nonStaticCallCount, initialNodeCount, 10, 5 * 1000, "Non-Static Call Count"); 55 } 56 nodeBarrier.barrier(); 57 } 58 59 public void callStaticMethod() throws Throwable { 60 final boolean masterNode = staticNodeBarrier.barrier() == 0; 61 synchronized (model) { 62 if (masterNode) { 63 SharedModel.staticMethod(); 64 } 65 } 66 staticNodeBarrier.barrier(); 67 System.err.println("### -> sleeping before checking static method calls"); 68 Thread.sleep(30000); 69 synchronized (model) { 70 checkCountTimed(SharedModel.staticCallCount, (masterNode) ? 1 : 0, 1, 10000, "Static Call Count"); 71 } 72 staticNodeBarrier.barrier(); 73 } 74 75 public static class SharedModel { 76 public final SynchronizedInt nonStaticCallCount = new SynchronizedInt(0); 77 public static final SynchronizedInt staticCallCount = new SynchronizedInt(0); 78 79 public static void staticMethod() { 80 staticCallCount.increment(); 81 } 82 83 public void nonStaticMethod(Object obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b) 84 throws Throwable { 85 nonStaticCallCount.increment(); 86 } 87 } 88 89 private void checkCountTimed(SynchronizedInt actualSI, final int expected, final int slices, final long sliceMillis, 90 String msg) throws InterruptedException { 91 int actual = 0; 93 int i; 94 for (i = 0; i < slices; i++) { 95 actual = actualSI.get(); 96 if (actual > expected || actual < 0) { 97 notifyError("Wrong Count: expected=" + expected + ", actual=" + actual); 98 } 99 if (actual < expected) { 100 Thread.sleep(sliceMillis); 101 } else { 102 break; 103 } 104 } 105 if (i == slices) { 106 notifyError("Wrong Count: expected=" + expected + ", actual=" + actual); 107 } 108 System.err.println("\n### -> check '" + msg + "' passed in " + i + " slices"); 109 } 110 111 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 112 try { 113 new CyclicBarrierSpec().visit(visitor, config); 114 new SynchronizedIntSpec().visit(visitor, config); 115 116 TransparencyClassSpec spec = config.getOrCreateSpec(FooObject.class.getName()); 117 String testClassName = DistributedMethodCallExpressionTestApp.class.getName(); 118 spec = config.getOrCreateSpec(testClassName); 119 spec.addRoot("model", "model"); 120 spec.addRoot("nodeBarrier", "nodeBarrier"); 121 spec.addRoot("staticNodeBarrier", "staticNodeBarrier"); 122 String methodExpression = "* " + testClassName + "*.*(..)"; 123 config.addWriteAutolock(methodExpression); 124 125 spec = config.getOrCreateSpec(SharedModel.class.getName()); 126 spec.addDistributedMethodCall("staticMethod", "()V", true); 127 try { 128 spec.addDistributedMethodCall("<init>", "()V", true); 129 throw new AssertionError ("Should have thrown an AssertionError."); 130 } catch (AssertionError e) { 131 } 133 134 config 135 .addDistributedMethodCall(new DistributedMethodSpec("* com.tctest.DistributedMethodCallExpressionTestApp$SharedModel.nonStaticMethod(..)", true)); 136 } catch (Exception e) { 137 throw new AssertionError (e); 138 } 139 } 140 } 141 | Popular Tags |