KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcurrentLockSystemTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
7
8 import com.tc.logging.TCLogger;
9 import com.tc.logging.TCLogging;
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.ConfigLockLevel;
12 import com.tc.object.config.DSOClientConfigHelper;
13 import com.tc.object.config.TransparencyClassSpec;
14 import com.tc.object.config.spec.SynchronizedIntSpec;
15 import com.tc.simulator.app.ApplicationConfig;
16 import com.tc.simulator.listener.ListenerProvider;
17 import com.tc.util.concurrent.ThreadUtil;
18 import com.tctest.runner.AbstractTransparentApp;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 public class ConcurrentLockSystemTestApp extends AbstractTransparentApp {
25
26   private static final TCLogger logger = TCLogging.getTestingLogger(ConcurrentLockSystemTestApp.class);
27   private final TestObject testObject = new TestObject();
28   private final SynchronizedInt participants = new SynchronizedInt(0);
29
30   public ConcurrentLockSystemTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
31     super(appId, cfg, listenerProvider);
32   }
33
34   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
35     String JavaDoc testClassname = ConcurrentLockSystemTestApp.class.getName();
36     TransparencyClassSpec spec = config.getOrCreateSpec(testClassname);
37     spec.addRoot("testObject", testClassname + ".testObject");
38     spec.addRoot("participants", testClassname + ".participants");
39
40     String JavaDoc testObjectClassname = TestObject.class.getName();
41     config.addIncludePattern(testObjectClassname);
42
43     // create locks
44
config.addWriteAutolock("* " + testClassname + ".run()");
45     config.addWriteAutolock("* " + testObjectClassname + ".populate(..)");
46     config.addReadAutolock("* " + testObjectClassname + ".isPopulated()");
47     config.addAutolock("* " + testObjectClassname + ".increment()", ConfigLockLevel.CONCURRENT);
48
49     // config for SynchronizedInt
50
new SynchronizedIntSpec().visit(visitor, config);
51   }
52
53   public void run() {
54     int participantCount = participants.increment();
55     boolean isWriter = participantCount == 1;
56     int iterations = 500;
57     int children = 50;
58     if (isWriter) {
59       testObject.populate(children);
60       for (int i = 0; i < iterations; i++) {
61         if (i % (iterations / 5) == 0) info("incrementing TestObject in " + (i + 1) + " of " + iterations
62                                             + " iterations.");
63         testObject.increment();
64       }
65     }
66
67     List JavaDoc counts;
68     while ((counts = testObject.getAllCounts()).size() != children + 1) {
69       System.err.println("Cycling until the counts list is large enough: " + counts);
70       ThreadUtil.reallySleep(1 * 500);
71     }
72
73     List JavaDoc collapsedCounts;
74     List JavaDoc expectedCounts = new LinkedList JavaDoc();
75     expectedCounts.add(new Integer JavaDoc(iterations));
76     while (true) {
77       collapsedCounts = new LinkedList JavaDoc(new HashSet JavaDoc(testObject.getAllCounts()));
78       if (expectedCounts.equals(collapsedCounts)) {
79         System.err.println("SUCCESS.");
80         return;
81       }
82       ThreadUtil.reallySleep(1 * 500);
83     }
84   }
85
86   private void info(Object JavaDoc msg) {
87     logger.info(this + ": " + msg);
88   }
89
90   private static final class TestObject {
91
92     private TestObject child;
93     private int size;
94     private int count;
95
96     public synchronized String JavaDoc toString() {
97       return "TestObject[child=" + (child == null ? "null" : "TestObject") + ", size=" + size + ", count=" + count
98              + "]";
99     }
100
101     public synchronized void populate(int populateCount) {
102       if (isPopulated()) {
103         info("TestObject already populated; not populating.");
104         return;
105       }
106       info("Populating TestObject with " + populateCount + " children...");
107       TestObject to = this;
108       for (int i = 0; i < populateCount; i++) {
109         synchronized (to) {
110           to.child = new TestObject();
111         }
112         to = to.child;
113       }
114       this.size = populateCount;
115       info("Done populating TestObject.");
116     }
117
118     public synchronized List JavaDoc getAllCounts() {
119       TestObject to = this;
120       List JavaDoc rv = new LinkedList JavaDoc();
121       while (to != null) {
122         rv.add(new Integer JavaDoc(count));
123         to = to.child;
124       }
125       return rv;
126     }
127
128     public synchronized boolean isPopulated() {
129       return this.child != null;
130     }
131
132     public void increment() {
133       TestObject to = this;
134       synchronized (this) {
135         do {
136           to.basicIncrement();
137           to = to.child;
138         } while (to != null);
139       }
140     }
141
142     private void basicIncrement() {
143       count++;
144     }
145
146     private void info(Object JavaDoc msg) {
147       logger.info(this + ": " + msg);
148     }
149
150   }
151
152 }
153
Popular Tags