KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > core > impl > MarkAndSweepGarbageCollectorTest


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.objectserver.core.impl;
5
6 import com.tc.exception.ImplementMe;
7 import com.tc.net.protocol.tcm.ChannelID;
8 import com.tc.object.ObjectID;
9 import com.tc.objectserver.api.ObjectManager;
10 import com.tc.objectserver.api.ObjectManagerStatsListener;
11 import com.tc.objectserver.context.ObjectManagerResultsContext;
12 import com.tc.objectserver.core.api.Filter;
13 import com.tc.objectserver.core.api.GarbageCollector;
14 import com.tc.objectserver.core.api.ManagedObject;
15 import com.tc.objectserver.impl.ManagedObjectReference;
16 import com.tc.objectserver.l1.api.TestClientStateManager;
17 import com.tc.objectserver.persistence.api.PersistenceTransaction;
18 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
19 import com.tc.objectserver.persistence.impl.NullPersistenceTransactionProvider;
20 import com.tc.text.PrettyPrinter;
21 import com.tc.util.SyncObjectIdSet;
22 import com.tc.util.SyncObjectIdSetImpl;
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import junit.framework.TestCase;
34
35 /**
36  * @author steve
37  */

38 public class MarkAndSweepGarbageCollectorTest extends TestCase implements ObjectManager {
39   private long objectIDCounter = 0;
40   private TestManagedObject root1;
41   private TestManagedObject root2;
42   private Set roots = new HashSet JavaDoc();
43   private Map JavaDoc managed; // = new
44
// HashMap();
45
private GarbageCollector collector; // = new
46
// MarkAndSweepGarbageCollector();
47
private Set lookedUp;
48   private Set released;
49   private PersistenceTransactionProvider transactionProvider = new NullPersistenceTransactionProvider();
50
51   private Filter filter = new Filter() {
52                                                                public boolean shouldVisit(ObjectID referencedObject) {
53                                                                  return true;
54                                                                }
55                                                              };
56
57   /**
58    * Constructor for MarkAndSweepGarbageCollectorTest.
59    *
60    * @param arg0
61    */

62   public MarkAndSweepGarbageCollectorTest(String JavaDoc arg0) {
63     super(arg0);
64   }
65
66   /*
67    * @see TestCase#setUp()
68    */

69   protected void setUp() throws Exception JavaDoc {
70     super.setUp();
71     this.managed = new HashMap JavaDoc();
72
73     this.collector = new MarkAndSweepGarbageCollector(this, new TestClientStateManager(), false);
74     this.lookedUp = new HashSet JavaDoc();
75     this.released = new HashSet JavaDoc();
76     this.root1 = createObject(8);
77     this.root2 = createObject(8);
78     this.roots = new HashSet JavaDoc();
79     roots.add(root1);
80     roots.add(root2);
81   }
82
83   public Object JavaDoc getLock() {
84     return this;
85   }
86
87   public Set getRootIds() {
88     HashSet JavaDoc rv = new HashSet JavaDoc();
89     for (Iterator JavaDoc i = roots.iterator(); i.hasNext();) {
90       rv.add(((TestManagedObject) i.next()).getID());
91     }
92     return rv;
93   }
94
95   public void testEmptyRoots() {
96     // System.out.println("running: testEmptyRoots");
97

98     Set toDelete = collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
99     // System.out.println(toDelete);
100
assertTrue(toDelete.size() == 0);
101   }
102
103   public void testOneLevelNoGarbage() {
104     // System.out.println("running: testOneLevelNoGarbage");
105
TestManagedObject tmo = createObject(3);
106     root1.setReference(0, tmo.getID());
107     Set toDelete = collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
108     // System.out.println(toDelete);
109
assertTrue(toDelete.size() == 0);
110   }
111
112   public void testSharedBetweenRootsNoGarbage() {
113     // System.out.println("running: testSharedBetweenRootsNoGarbage");
114
TestManagedObject tmo = createObject(3);
115     root1.setReference(0, tmo.getID());
116     root2.setReference(0, tmo.getID());
117     Set toDelete = collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
118     // System.out.println(toDelete);
119
// System.out.println(managed);
120
assertTrue(toDelete.size() == 0);
121   }
122
123   public void testObjectCycleNoGarbage() {
124     TestManagedObject tmo1 = createObject(3);
125     TestManagedObject tmo2 = createObject(3);
126     tmo1.setReference(0, tmo2.getID());
127     tmo2.setReference(0, tmo1.getID());
128
129     root1.setReference(0, tmo1.getID());
130
131     Set toDelete = collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
132     // System.out.println(toDelete);
133
assertTrue(toDelete.size() == 0);
134   }
135
136   public void testObjectCycleWithGarbage() {
137     TestManagedObject tmo1 = createObject(3);
138     TestManagedObject tmo2 = createObject(3);
139     createObject(3);
140
141     tmo1.setReference(0, tmo2.getID());
142     tmo2.setReference(0, tmo1.getID());
143
144     root1.setReference(0, tmo1.getID());
145
146     Set toDelete = collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
147     // System.out.println(toDelete);
148
assertTrue(toDelete.size() == 1);
149   }
150
151   public void testFilter() {
152     final TestManagedObject tmo1 = createObject(3);
153     final TestManagedObject tmo2 = createObject(3);
154     final TestManagedObject tmo3 = createObject(3);
155
156     tmo1.setReference(0, tmo2.getID());
157     tmo2.setReference(0, tmo1.getID());
158     tmo2.setReference(1, tmo3.getID());
159
160     root1.setReference(0, tmo1.getID());
161
162     Filter testFilter = new Filter() {
163       public boolean shouldVisit(ObjectID referencedObject) {
164         boolean rv = (!tmo2.getID().equals(referencedObject));
165         return rv;
166       }
167     };
168
169     // make sure that the filter filters out the sub-graph starting at the reference to tmo2.
170
collector.collect(testFilter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
171     assertTrue(this.lookedUp.contains(tmo1.getID()));
172     assertFalse(this.lookedUp.contains(tmo2.getID()));
173     assertFalse(this.lookedUp.contains(tmo3.getID()));
174
175     // try it with the regular filter to make sure the behavior is actually different.
176
collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
177     assertTrue(this.lookedUp.contains(tmo1.getID()));
178     assertTrue(this.lookedUp.contains(tmo2.getID()));
179     assertTrue(this.lookedUp.contains(tmo3.getID()));
180   }
181
182   public void testLookupAndReleaseBalanced() {
183     final TestManagedObject tmo1 = createObject(3);
184     final TestManagedObject tmo2 = createObject(3);
185     final TestManagedObject tmo3 = createObject(3);
186
187     tmo1.setReference(0, tmo2.getID());
188     tmo2.setReference(0, tmo1.getID());
189     tmo2.setReference(1, tmo3.getID());
190
191     root1.setReference(0, tmo1.getID());
192
193     collector.collect(filter, getRootIds(), new HashSet JavaDoc(managed.keySet()));
194     assertTrue(lookedUp.equals(released));
195   }
196
197   public void testIsInGCPause() throws Exception JavaDoc {
198     assertFalse(collector.isPausingOrPaused());
199     collector.requestGCPause();
200     collector.notifyReadyToGC();
201     assertTrue(collector.isPausingOrPaused());
202     collector.notifyGCComplete();
203     assertFalse(collector.isPausingOrPaused());
204   }
205
206   private ObjectID nextID() {
207     return new ObjectID(objectIDCounter++);
208   }
209
210   private TestManagedObject createObject(int refCount) {
211     ObjectID[] ids = new ObjectID[refCount];
212
213     Arrays.fill(ids, ObjectID.NULL_ID);
214
215     TestManagedObject tmo = new TestManagedObject(nextID(), ids);
216     // System.out.println("Creating Object:" + tmo.getID());
217
managed.put(tmo.getID(), tmo.getReference());
218     return tmo;
219   }
220
221   public ManagedObject getObjectByID(ObjectID id) {
222     this.lookedUp.add(id);
223     ManagedObjectReference ref = (ManagedObjectReference) managed.get(id);
224     return (ref == null) ? null : ref.getObject();
225   }
226
227   public void release(PersistenceTransaction tx, ManagedObject object) {
228     this.released.add(object.getID());
229     return;
230   }
231
232   public void releaseAll(PersistenceTransaction tx, Collection JavaDoc c) {
233     return;
234   }
235
236   public void stop() {
237     throw new ImplementMe();
238   }
239
240   public boolean lookupObjectsAndSubObjectsFor(ChannelID channelID, ObjectManagerResultsContext responseContext,
241                                                int maxCount) {
242     throw new ImplementMe();
243   }
244
245   public boolean lookupObjectsForCreateIfNecessary(ChannelID channelID, ObjectManagerResultsContext context) {
246     throw new ImplementMe();
247   }
248
249   public Iterator JavaDoc getRoots() {
250     throw new ImplementMe();
251   }
252
253   public void createObject(ManagedObject object) {
254     throw new ImplementMe();
255   }
256
257   public void createRoot(String JavaDoc name, ObjectID id) {
258     throw new ImplementMe();
259   }
260
261   public ObjectID lookupRootID(String JavaDoc name) {
262     throw new ImplementMe();
263   }
264
265   public void setGarbageCollector(GarbageCollector gc) {
266     throw new ImplementMe();
267   }
268
269   public void setStatsListener(ObjectManagerStatsListener listener) {
270     throw new ImplementMe();
271   }
272
273   public void start() {
274     throw new ImplementMe();
275   }
276
277   public PrettyPrinter prettyPrint(PrettyPrinter out) {
278     throw new ImplementMe();
279   }
280
281   public void releaseReadOnly(ManagedObject object) {
282     this.released.add(object.getID());
283     return;
284   }
285
286   public void dump() {
287     throw new ImplementMe();
288   }
289
290   public void releaseAll(Collection JavaDoc objects) {
291     releaseAll(transactionProvider.nullTransaction(), objects);
292   }
293
294   public int getCheckedOutCount() {
295     return 0;
296   }
297
298   public Set getRootIDs() {
299     return roots;
300   }
301
302   public SyncObjectIdSet getAllObjectIDs() {
303     SyncObjectIdSet rv = new SyncObjectIdSetImpl();
304     rv.addAll(managed.keySet());
305     return rv;
306   }
307
308   public void addFaultedObject(ObjectID oid, ManagedObject mo, boolean removeOnRelease) {
309     throw new ImplementMe();
310   }
311
312   public void waitUntilReadyToGC() {
313     throw new ImplementMe();
314   }
315
316   public void notifyGCComplete(Set toDelete) {
317     throw new ImplementMe();
318   }
319
320   public void flushAndEvict(List JavaDoc objects2Flush) {
321     throw new ImplementMe();
322   }
323
324   public Map JavaDoc getRootNamesToIDsMap() {
325     throw new ImplementMe();
326   }
327
328 }
329
Popular Tags