KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > api > TestGarbageCollector


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.api;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.exception.ImplementMe;
9 import com.tc.object.ObjectID;
10 import com.tc.objectserver.core.api.Filter;
11 import com.tc.objectserver.core.api.GarbageCollector;
12 import com.tc.text.PrettyPrinter;
13 import com.tc.util.Assert;
14 import com.tc.util.concurrent.LifeCycleState;
15 import com.tc.util.concurrent.NullLifeCycleState;
16 import com.tc.util.concurrent.StoppableThread;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 public class TestGarbageCollector implements GarbageCollector {
26   public Set JavaDoc collectedObjects = new HashSet JavaDoc();
27   private boolean collected = false;
28   private boolean isPausingOrPaused = false;
29   private boolean isPaused = false;
30
31   private LinkedQueue collectCalls;
32   private LinkedQueue notifyReadyToGCCalls;
33   private LinkedQueue notifyGCCompleteCalls;
34   private LinkedQueue requestGCCalls;
35   private LinkedQueue blockUntilReadyToGCCalls;
36   private LinkedQueue blockUntilReadyToGCQueue;
37   private ObjectManager objectProvider;
38
39   public TestGarbageCollector(ObjectManager objectProvider) {
40     initQueues();
41     this.objectProvider = objectProvider;
42   }
43
44   private void initQueues() {
45     collectCalls = new LinkedQueue();
46     notifyReadyToGCCalls = new LinkedQueue();
47     notifyGCCompleteCalls = new LinkedQueue();
48     requestGCCalls = new LinkedQueue();
49     blockUntilReadyToGCCalls = new LinkedQueue();
50     blockUntilReadyToGCQueue = new LinkedQueue();
51   }
52
53   private List JavaDoc drainQueue(LinkedQueue queue) {
54     List JavaDoc rv = new ArrayList JavaDoc();
55     while (queue.peek() != null) {
56       try {
57         rv.add(queue.take());
58       } catch (InterruptedException JavaDoc e) {
59         throw new AssertionError JavaDoc(e);
60       }
61     }
62     return rv;
63   }
64
65   public synchronized void reset() {
66     collectedObjects.clear();
67     collected = false;
68     isPausingOrPaused = false;
69     initQueues();
70   }
71
72   public Set JavaDoc collect(Filter filter, Collection JavaDoc rootIds, Set JavaDoc managedObjectIds) {
73     try {
74       collectCalls.put(new CollectCallContext(filter, rootIds, managedObjectIds, objectProvider));
75     } catch (InterruptedException JavaDoc e) {
76       throw new AssertionError JavaDoc(e);
77     }
78     this.collected = true;
79     return collectedObjects;
80   }
81
82   public boolean collectWasCalled() {
83     return collectCalls.peek() != null;
84   }
85
86   public boolean waitForCollectToBeCalled(long timeout) {
87     try {
88       return collectCalls.poll(timeout) != null;
89     } catch (InterruptedException JavaDoc e) {
90       throw new AssertionError JavaDoc(e);
91     }
92   }
93
94   public CollectCallContext getNextCollectCall() {
95     try {
96       return (CollectCallContext) collectCalls.take();
97     } catch (InterruptedException JavaDoc e) {
98       throw new AssertionError JavaDoc(e);
99     }
100   }
101
102   public List JavaDoc getCollectCalls() {
103     return drainQueue(collectCalls);
104   }
105
106   public static class CollectCallContext {
107     public final Filter filter;
108     public final Collection JavaDoc roots;
109     public final Set JavaDoc managedObjectIds;
110     public final ManagedObjectProvider objectProvider;
111
112     private CollectCallContext(Filter filter, Collection JavaDoc roots, Set JavaDoc managedObjectIds,
113                                ManagedObjectProvider objectProvider) {
114       this.filter = filter;
115       this.roots = Collections.unmodifiableCollection(roots);
116       this.managedObjectIds = Collections.unmodifiableSet(managedObjectIds);
117       this.objectProvider = objectProvider;
118     }
119   }
120
121   public boolean isCollected() {
122     return this.collected;
123   }
124
125   public synchronized boolean isPausingOrPaused() {
126     return isPausingOrPaused;
127   }
128
129   public synchronized boolean isPaused() {
130     return isPaused;
131   }
132
133   public void notifyReadyToGC() {
134     try {
135       isPaused = true;
136       notifyReadyToGCCalls.put(new Object JavaDoc());
137     } catch (InterruptedException JavaDoc e) {
138       throw new AssertionError JavaDoc(e);
139     }
140   }
141
142   public boolean notifyReadyToGC_WasCalled() {
143     return notifyReadyToGCCalls.peek() != null;
144   }
145
146   public boolean waitFor_notifyReadyToGC_ToBeCalled(long timeout) {
147     try {
148       return notifyReadyToGCCalls.poll(timeout) != null;
149     } catch (InterruptedException JavaDoc e) {
150       throw new AssertionError JavaDoc(e);
151     }
152   }
153
154   public void blockUntilReadyToGC() {
155     try {
156       blockUntilReadyToGCCalls.put(new Object JavaDoc());
157       blockUntilReadyToGCQueue.take();
158     } catch (InterruptedException JavaDoc e) {
159       throw new AssertionError JavaDoc(e);
160     }
161   }
162
163   public void allow_blockUntilReadyToGC_ToProceed() {
164     try {
165       Assert.eval("queue was not empty!", blockUntilReadyToGCQueue.peek() == null);
166       blockUntilReadyToGCQueue.put(new Object JavaDoc());
167     } catch (InterruptedException JavaDoc e) {
168       throw new AssertionError JavaDoc(e);
169     }
170   }
171
172   public void waitUntil_blockUntilReadyToGC_IsCalled() {
173     try {
174       blockUntilReadyToGCCalls.take();
175     } catch (InterruptedException JavaDoc e) {
176       throw new AssertionError JavaDoc(e);
177     }
178   }
179
180   public boolean waitFor_blockUntilReadyToGC_ToBeCalled(int timeout) {
181     try {
182       return blockUntilReadyToGCCalls.poll(timeout) != null;
183     } catch (InterruptedException JavaDoc e) {
184       throw new AssertionError JavaDoc(e);
185     }
186   }
187
188   public boolean blockUntilReadyToGC_WasCalled() {
189     return blockUntilReadyToGCCalls.peek() != null;
190   }
191
192   public void notifyGCComplete() {
193     try {
194       isPausingOrPaused = false;
195       isPaused = false;
196       notifyGCCompleteCalls.put(new Object JavaDoc());
197     } catch (InterruptedException JavaDoc e) {
198       throw new AssertionError JavaDoc(e);
199     }
200     return;
201   }
202
203   public void waitUntil_notifyGCComplete_IsCalled() {
204     try {
205       notifyGCCompleteCalls.take();
206     } catch (InterruptedException JavaDoc e) {
207       throw new AssertionError JavaDoc(e);
208     }
209   }
210
211   public boolean waitFor_notifyGCComplete_ToBeCalled(long timeout) {
212     try {
213       return notifyGCCompleteCalls.poll(timeout) != null;
214     } catch (InterruptedException JavaDoc e) {
215       throw new AssertionError JavaDoc(e);
216     }
217   }
218
219   public void requestGCPause() {
220     try {
221       isPausingOrPaused = true;
222       isPaused = false;
223       requestGCCalls.put(new Object JavaDoc());
224     } catch (InterruptedException JavaDoc e) {
225       throw new AssertionError JavaDoc(e);
226     }
227     return;
228   }
229
230   public PrettyPrinter prettyPrint(PrettyPrinter out) {
231     return out.print(getClass().getName());
232   }
233
234   public Set JavaDoc collect(Filter traverser, Collection JavaDoc roots, Set JavaDoc managedObjectIds, LifeCycleState state) {
235     return collect(traverser, roots, managedObjectIds);
236   }
237
238   public void changed(ObjectID changedObject, ObjectID oldReference, ObjectID newReference) {
239     throw new ImplementMe();
240
241   }
242
243   // This test is kind of messed up now that I refactored. Needs evaluating.
244
public void gc() {
245     collect(null, objectProvider.getRootIDs(), objectProvider.getAllObjectIDs(), new NullLifeCycleState());
246     this.requestGCPause();
247     this.blockUntilReadyToGC();
248     this.notifyGCComplete();
249   }
250
251   public void addNewReferencesTo(Set JavaDoc rescueIds) {
252     throw new ImplementMe();
253
254   }
255
256   public void start() {
257     throw new ImplementMe();
258
259   }
260
261   public void stop() {
262     throw new ImplementMe();
263
264   }
265
266   public void setState(StoppableThread st) {
267     throw new ImplementMe();
268
269   }
270
271   public void addListener(ObjectManagerEventListener listener) {
272     throw new ImplementMe();
273
274   }
275
276   public GCStats[] getGarbageCollectorStats() {
277     return null;
278   }
279 }
Popular Tags