001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.concurrent.ThreadUtil;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.util.HashSet;
018: import java.util.Set;
019:
020: public class DistributedMethodCallGCTest extends GCTestBase {
021:
022: public void doSetUp(TransparentTestIface t) throws Exception {
023: super .doSetUp(t);
024: t.getTransparentAppConfig().setAttribute("gc-interval-ms",
025: new Long(getGCIntervalInSeconds() * 1000));
026: }
027:
028: protected Class getApplicationClass() {
029: return App.class;
030: }
031:
032: public static class App extends AbstractErrorCatchingTransparentApp {
033:
034: private static long DURATION = 3 * 60 * 1000;
035: private static long END = System.currentTimeMillis() + DURATION;
036:
037: private final Set root = new HashSet();
038:
039: public App(String appId, ApplicationConfig cfg,
040: ListenerProvider listenerProvider) {
041: super (appId, cfg, listenerProvider);
042: DMITarget.setGCTime(((Long) cfg
043: .getAttributeObject("gc-interval-ms")).longValue());
044:
045: if (getParticipantCount() < 2) {
046: throw new AssertionError();
047: }
048: }
049:
050: protected void runTest() throws Throwable {
051:
052: DMITarget.setAppThread(Thread.currentThread());
053:
054: while (!shouldEnd()) {
055: makeDMICall();
056: }
057: }
058:
059: private void makeDMICall() {
060: DMITarget t = new DMITarget();
061: synchronized (root) {
062: root.add(t);
063: root.remove(t);
064: }
065:
066: t.foo();
067: t.foo("asdf", 42);
068: }
069:
070: private static boolean shouldEnd() {
071: // slow down for the monkeys
072: ThreadUtil.reallySleep(50);
073:
074: return System.currentTimeMillis() > END;
075: }
076:
077: public static void visitL1DSOConfig(ConfigVisitor visitor,
078: DSOClientConfigHelper config) {
079: String testClassName = App.class.getName();
080: TransparencyClassSpec spec = config
081: .getOrCreateSpec(testClassName);
082: spec.addRoot("root", "root");
083:
084: config.addWriteAutolock("* " + testClassName + ".*(..)");
085:
086: spec = config.getOrCreateSpec(DMITarget.class.getName());
087: spec.addDistributedMethodCall("foo", "()V", true);
088: spec.addDistributedMethodCall("foo",
089: "(Ljava/lang/String;I)V", true);
090: }
091:
092: private static class DMITarget {
093:
094: private static Thread localAppThread;
095: private static final SynchronizedInt count = new SynchronizedInt(
096: 0);
097: static volatile long gcTime = 0;
098:
099: static synchronized void setGCTime(long l) {
100: gcTime = l;
101: }
102:
103: static synchronized void setAppThread(Thread t) {
104: if (localAppThread != null) {
105: throw new AssertionError();
106: }
107: localAppThread = t;
108: }
109:
110: void foo() {
111: output();
112: }
113:
114: void foo(String s, int i) {
115: output();
116: }
117:
118: private void output() {
119: if (Thread.currentThread() != localAppThread) {
120:
121: int c = count.increment();
122: if ((c % 100) == 0) {
123: System.err.println("remote DMI executed: " + c);
124: }
125: if ((c % 1000) == 0) {
126: System.gc();
127: ThreadUtil.reallySleep(gcTime + 2000);
128: }
129: }
130: }
131: }
132:
133: }
134:
135: }
|