KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > hook > impl > ArrayManagerTest


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.tc.object.bytecode.hook.impl;
5
6 import com.tc.exception.ImplementMe;
7 import com.tc.object.ObjectID;
8 import com.tc.object.TCClass;
9 import com.tc.object.TCObject;
10 import com.tc.object.dna.api.DNA;
11 import com.tc.object.dna.api.DNAException;
12 import com.tc.object.dna.api.DNAWriter;
13 import com.tc.util.concurrent.ThreadUtil;
14
15 import gnu.trove.TLinkable;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.IdentityHashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Random JavaDoc;
22
23 import junit.framework.TestCase;
24
25 public class ArrayManagerTest extends TestCase {
26
27   private final Map JavaDoc registerdPairs = new IdentityHashMap JavaDoc();
28   private static final List JavaDoc errors = new ArrayList JavaDoc();
29   private Object JavaDoc[] arrays;
30
31   protected void setUp() throws Exception JavaDoc {
32     for (int i = 0; i < 100; i++) {
33       TCObject tco = new FakeTCObject();
34       Object JavaDoc array = new Object JavaDoc[] {};
35       registerdPairs.put(array, tco); // do this before register to always have strong ref
36
ArrayManager.register(array, tco);
37     }
38
39     arrays = registerdPairs.keySet().toArray();
40   }
41
42   protected void tearDown() throws Exception JavaDoc {
43     super.tearDown();
44
45     synchronized (errors) {
46       if (errors.size() > 0) {
47         errors.clear();
48         fail();
49       }
50     }
51   }
52
53   static void addError(Throwable JavaDoc t) {
54     t.printStackTrace();
55     synchronized (errors) {
56       errors.add(t);
57     }
58   }
59
60   public void testCache() {
61     Random JavaDoc r = new Random JavaDoc();
62     for (int i = 0; i < 100000; i++) {
63       Object JavaDoc array = arrays[r.nextInt(arrays.length)];
64       TCObject expected = (TCObject) registerdPairs.get(array);
65       assertEquals(expected, ArrayManager.getObject(array));
66     }
67   }
68
69   public void testReplaceCachedNegative() {
70     List JavaDoc refs = new ArrayList JavaDoc();
71     for (int i = 0; i < 50000; i++) {
72       Object JavaDoc array = new Object JavaDoc[] {};
73       refs.add(array);
74       assertNull(ArrayManager.getObject(array));
75       TCObject tco = new FakeTCObject();
76       ArrayManager.register(array, tco);
77       assertEquals(tco, ArrayManager.getObject(array));
78       assertEquals(tco, ArrayManager.getObject(array)); // do it twice
79
}
80   }
81
82   public void testThreadsNoGC() throws Exception JavaDoc {
83     testThreads(false);
84   }
85
86   public void testThreads() throws Exception JavaDoc {
87     testThreads(true);
88   }
89
90   private void testThreads(boolean withGC) throws Exception JavaDoc {
91     AddNew addNew = new AddNew();
92     Thread JavaDoc adder = new Thread JavaDoc(addNew, "Adder");
93
94     Query[] query = new Query[2];
95     for (int i = 0; i < query.length; i++) {
96       query[i] = new Query(arrays, registerdPairs, withGC);
97     }
98     Thread JavaDoc[] queries = new Thread JavaDoc[query.length];
99     for (int i = 0; i < queries.length; i++) {
100       queries[i] = new Thread JavaDoc(query[i]);
101       queries[i].setName("Query #" + i);
102       queries[i].start();
103     }
104
105     adder.start();
106
107     ThreadUtil.reallySleep(30000);
108
109     addNew.stop();
110     adder.join();
111
112     for (int i = 0; i < queries.length; i++) {
113       query[i].stop();
114     }
115     for (int i = 0; i < queries.length; i++) {
116       queries[i].join();
117     }
118
119   }
120
121   private static abstract class Base implements Runnable JavaDoc {
122     private volatile boolean stop = false;
123     private int count = 0;
124
125     public void run() {
126       try {
127         while (!stop) {
128           work();
129           count++;
130         }
131       } catch (Throwable JavaDoc t) {
132         addError(t);
133       } finally {
134         System.err.println(Thread.currentThread().getName() + " made " + count + " loops");
135       }
136     }
137
138     void stop() {
139       stop = true;
140     }
141
142     abstract void work() throws Throwable JavaDoc;
143   }
144
145   private static class Query extends Base {
146     private final Random JavaDoc r = new Random JavaDoc();
147     private final Map JavaDoc pairs;
148     private final Object JavaDoc[] arrays;
149     private final boolean withGC;
150
151     Query(Object JavaDoc[] arrays, Map JavaDoc pairs, boolean withGC) {
152       this.arrays = arrays;
153       this.pairs = pairs;
154       this.withGC = withGC;
155     }
156
157     void work() throws Throwable JavaDoc {
158       if (r.nextBoolean()) {
159         Object JavaDoc array = arrays[r.nextInt(arrays.length)];
160         TCObject expect = (TCObject) pairs.get(array);
161         if (expect != ArrayManager.getObject(array)) { throw new AssertionError JavaDoc("wrong mapping returned"); }
162       } else {
163         if (ArrayManager.getObject(new Object JavaDoc[] {}) != null) { throw new AssertionError JavaDoc(
164                                                                                         "found object for brand new array"); }
165       }
166
167       if (withGC && (System.currentTimeMillis() % 255) == 0) {
168         System.out.println(Thread.currentThread().getName() + " doing GC");
169         System.gc();
170       }
171
172     }
173   }
174
175   private class AddNew extends Base {
176     void work() {
177       Object JavaDoc newArray = new Object JavaDoc[] {};
178       ArrayManager.getObject(newArray);
179       ArrayManager.register(newArray, new FakeTCObject());
180
181       for (int i = 0; i < 10; i++) {
182         // this strange test should make it impossible for the runtime to eliminate this code
183
if (hashCode() == System.currentTimeMillis()) {
184           System.out.println("BONK -- This is harmless ;-)");
185         }
186       }
187
188     }
189   }
190
191   private static class FakeTCObject implements TCObject {
192
193     public boolean autoLockingDisabled() {
194       throw new ImplementMe();
195     }
196
197     public void booleanFieldChanged(String JavaDoc classname, String JavaDoc fieldname, boolean newValue, int index) {
198       throw new ImplementMe();
199     }
200
201     public void byteFieldChanged(String JavaDoc classname, String JavaDoc fieldname, byte newValue, int index) {
202       throw new ImplementMe();
203     }
204
205     public void charFieldChanged(String JavaDoc classname, String JavaDoc fieldname, char newValue, int index) {
206       throw new ImplementMe();
207     }
208
209     public void clearReference(String JavaDoc fieldName) {
210       throw new ImplementMe();
211     }
212
213     public int clearReferences(int toClear) {
214       throw new ImplementMe();
215     }
216
217     public void dehydrate(DNAWriter writer) throws DNAException {
218       throw new ImplementMe();
219     }
220
221     public void disableAutoLocking() {
222       throw new ImplementMe();
223     }
224
225     public void doubleFieldChanged(String JavaDoc classname, String JavaDoc fieldname, double newValue, int index) {
226       throw new ImplementMe();
227     }
228
229     public void floatFieldChanged(String JavaDoc classname, String JavaDoc fieldname, float newValue, int index) {
230       throw new ImplementMe();
231     }
232
233     public boolean getAndResetNew() {
234       throw new ImplementMe();
235     }
236
237     public String JavaDoc getFieldNameByOffset(long fieldOffset) {
238       throw new ImplementMe();
239     }
240
241     public TLinkable getNext() {
242       throw new ImplementMe();
243     }
244
245     public ObjectID getObjectID() {
246       throw new ImplementMe();
247     }
248
249     public Object JavaDoc getPeerObject() {
250       throw new ImplementMe();
251     }
252
253     public TLinkable getPrevious() {
254       throw new ImplementMe();
255     }
256
257     public Object JavaDoc getResolveLock() {
258       throw new ImplementMe();
259     }
260
261     public TCClass getTCClass() {
262       throw new ImplementMe();
263     }
264
265     public long getVersion() {
266       throw new ImplementMe();
267     }
268
269     public void hydrate(DNA from, boolean force) throws DNAException {
270       throw new ImplementMe();
271     }
272
273     public void intFieldChanged(String JavaDoc classname, String JavaDoc fieldname, int newValue, int index) {
274       throw new ImplementMe();
275     }
276
277     public boolean isNew() {
278       throw new ImplementMe();
279     }
280
281     public boolean isShared() {
282       throw new ImplementMe();
283     }
284
285     public void logicalInvoke(int method, String JavaDoc methodSignature, Object JavaDoc[] params) {
286       throw new ImplementMe();
287     }
288
289     public void longFieldChanged(String JavaDoc classname, String JavaDoc fieldname, long newValue, int index) {
290       throw new ImplementMe();
291     }
292
293     public void objectFieldChanged(String JavaDoc classname, String JavaDoc fieldname, Object JavaDoc newValue, int index) {
294       throw new ImplementMe();
295     }
296
297     public void objectFieldChangedByOffset(String JavaDoc classname, long fieldOffset, Object JavaDoc newValue, int index) {
298       throw new ImplementMe();
299     }
300
301     public void resolveAllReferences() {
302       throw new ImplementMe();
303     }
304
305     public void resolveArrayReference(int index) {
306       throw new ImplementMe();
307     }
308
309     public void resolveReference(String JavaDoc fieldName) {
310       throw new ImplementMe();
311     }
312
313     public void setIsNew() {
314       throw new ImplementMe();
315     }
316
317     public void setNext(TLinkable link) {
318       throw new ImplementMe();
319     }
320
321     public void setPrevious(TLinkable link) {
322       throw new ImplementMe();
323     }
324
325     public void setReference(String JavaDoc fieldName, ObjectID id) {
326       throw new ImplementMe();
327     }
328
329     public void setValue(String JavaDoc fieldName, Object JavaDoc obj) {
330       throw new ImplementMe();
331     }
332
333     public void setVersion(long version) {
334       throw new ImplementMe();
335     }
336
337     public void shortFieldChanged(String JavaDoc classname, String JavaDoc fieldname, short newValue, int index) {
338       throw new ImplementMe();
339     }
340
341     public boolean canEvict() {
342       throw new ImplementMe();
343     }
344
345     public void clearAccessed() {
346       throw new ImplementMe();
347     }
348
349     public void markAccessed() {
350       throw new ImplementMe();
351     }
352
353     public boolean recentlyAccessed() {
354       throw new ImplementMe();
355     }
356
357     public void objectArrayChanged(int startPos, Object JavaDoc[] array, int length) {
358       throw new ImplementMe();
359     }
360
361     public void primitiveArrayChanged(int startPos, Object JavaDoc array, int length) {
362       throw new ImplementMe();
363     }
364
365     public int accessCount(int factor) {
366       throw new ImplementMe();
367     }
368
369     public void literalValueChanged(Object JavaDoc newValue, Object JavaDoc oldValue) {
370       throw new ImplementMe();
371     }
372
373     public void setLiteralValue(Object JavaDoc newValue) {
374       throw new ImplementMe();
375     }
376
377     public ArrayIndexOutOfBoundsException JavaDoc checkArrayIndex(int index) {
378       throw new ImplementMe();
379     }
380
381   }
382
383 }
384
Popular Tags