KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > InterceptorSynchronizationTest


1 package org.jboss.cache.loader;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheException;
5 import org.jboss.cache.CacheImpl;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.Modification;
8 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
9
10 import java.io.ObjectInputStream JavaDoc;
11 import java.io.ObjectOutputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * Test case that proves that the CacheImpl is serialized inside the CacheLoaderInterceptor
22  * when the node is empty. ANY call to retrieve a node that is not loaded will lock
23  * up inside CacheLoaderInterceptor while the node is loaded and any other thread
24  * that wants a node out of the cache will wait for the first one to finish before it can even _start_ loading.
25  *
26  * @author paulsmith
27  */

28 public class InterceptorSynchronizationTest extends TestCase
29 {
30
31    final int CACHELOADER_WAITTIME = 2000; // lets say loading a node takes 2 seconds
32
final int numThreadsPerTopLevelNode = 5; // we'll have 5 requests for nodes within a branch
33

34
35    public void testBlockingProblem() throws Exception JavaDoc
36    {
37
38       CacheImpl cache = new CacheImpl();
39       cache.setCacheLoader(new TestSlowCacheLoader());
40       cache.start();
41
42       long begin = System.currentTimeMillis();
43       Collection JavaDoc threads = new ArrayList JavaDoc();
44
45       /*
46       * Create lots of threads all trying to load DIFFERENT fqn's, as well as a set with different top level nodes.
47       */

48
49       for (int i = 0; i < numThreadsPerTopLevelNode; i++)
50       {
51          Thread JavaDoc thread = new Thread JavaDoc(new Retriever(cache, "/Moo/" + i));
52          threads.add(thread);
53          Thread JavaDoc thread2 = new Thread JavaDoc(new Retriever(cache, "/Meow/" + i));
54          threads.add(thread2);
55       }
56       for (Iterator JavaDoc iter = threads.iterator(); iter.hasNext();)
57       {
58          Thread JavaDoc thread = (Thread JavaDoc) iter.next();
59          thread.start();
60
61       }
62       for (Iterator JavaDoc iter = threads.iterator(); iter.hasNext();)
63       {
64          Thread JavaDoc thread = (Thread JavaDoc) iter.next();
65          thread.join();
66       }
67
68       long end = System.currentTimeMillis();
69       long timeTaken = (end - begin);
70
71       /*
72       * My expectation is that if there are 2 top level nodes they should be loaded in parallel at the very least,
73       * but even bottom level nodes that are different should be able to be loaded concurrently.
74       *
75       * In this test, NONE of the threads operate in parallel once entered into CacheLoaderInterceptor.
76       */

77       int totalTimeExpectedToWaitIfNotSerialized = 3 * CACHELOADER_WAITTIME; // i'm being very generous here. 3 times the wait time for each node is more than enough if it was in parallel.
78
assertTrue("If it was parallel, it should have finished quicker than this:" + timeTaken, timeTaken < totalTimeExpectedToWaitIfNotSerialized);
79    }
80
81    /**
82     * Dummy cache loader that emulates a slow loading of any node from a virtual backing store.
83     *
84     * @author paulsmith
85     */

86    public class TestSlowCacheLoader extends AbstractCacheLoader
87    {
88       public void setConfig(IndividualCacheLoaderConfig config)
89       {
90       }
91
92       public IndividualCacheLoaderConfig getConfig()
93       {
94          return null;
95       }
96
97       public Set JavaDoc getChildrenNames(Fqn arg0) throws Exception JavaDoc
98       {
99          return null;
100       }
101
102       public Object JavaDoc get(Fqn arg0, Object JavaDoc arg1) throws Exception JavaDoc
103       {
104          return null;
105       }
106
107       public Map JavaDoc get(Fqn arg0) throws Exception JavaDoc
108       {
109          Thread.sleep(CACHELOADER_WAITTIME);
110          return Collections.singletonMap("foo", "bar");
111       }
112
113       public boolean exists(Fqn arg0) throws Exception JavaDoc
114       {
115          return true;
116       }
117
118       public Object JavaDoc put(Fqn arg0, Object JavaDoc arg1, Object JavaDoc arg2) throws Exception JavaDoc
119       {
120          return null;
121       }
122
123       public void put(Fqn arg0, Map JavaDoc arg1) throws Exception JavaDoc
124       {
125
126       }
127
128       public void put(List JavaDoc<Modification> modifications) throws Exception JavaDoc
129       {
130       }
131
132       public Object JavaDoc remove(Fqn arg0, Object JavaDoc arg1) throws Exception JavaDoc
133       {
134          return null;
135       }
136
137       public void remove(Fqn arg0) throws Exception JavaDoc
138       {
139
140       }
141
142       public void removeData(Fqn arg0) throws Exception JavaDoc
143       {
144
145       }
146
147       public void prepare(Object JavaDoc tx, List JavaDoc<Modification> modifications, boolean one_phase) throws Exception JavaDoc
148       {
149       }
150
151       public void commit(Object JavaDoc arg0) throws Exception JavaDoc
152       {
153
154       }
155
156       public void rollback(Object JavaDoc arg0)
157       {
158       }
159
160       /*public byte[] loadEntireState() throws Exception {
161           return null;
162       }
163
164       public void storeEntireState(byte[] arg0) throws Exception {
165
166       }
167
168       public byte[] loadState(Fqn subtree) throws Exception
169       {
170           return new byte[0];
171       }
172
173       public void storeState(byte[] state, Fqn subtree) throws Exception
174       {
175       }*/

176
177       public void loadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
178       {
179          // nothing to do here
180
}
181
182       public void loadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
183       {
184          // nothing to do here
185
}
186
187       public void storeEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
188       {
189          // nothing to do here
190
}
191
192       public void storeState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
193       {
194          // nothing to do here
195
}
196
197       public void create() throws Exception JavaDoc
198       {
199
200       }
201
202       public void start() throws Exception JavaDoc
203       {
204
205       }
206
207       public void stop()
208       {
209
210       }
211
212       public void destroy()
213       {
214
215       }
216
217    }
218
219
220    private static class Retriever implements Runnable JavaDoc
221    {
222       private final String JavaDoc fqn;
223       private CacheImpl cache;
224
225       private Retriever(CacheImpl cache, String JavaDoc fqn)
226       {
227          this.fqn = fqn;
228          this.cache = cache;
229       }
230
231       public void run()
232       {
233          try
234          {
235             cache.get(fqn, "foo");
236          }
237          catch (CacheException e)
238          {
239             throw new RuntimeException JavaDoc("Unexpected", e);
240          }
241
242       }
243    }
244 }
245
Popular Tags