KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_CacheService


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_CacheService
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.iapi.services.cache.*;
28
29 import org.apache.derby.iapi.services.daemon.*;
30
31 import org.apache.derby.iapi.services.monitor.Monitor;
32
33 import org.apache.derby.iapi.error.StandardException;
34
35 public class T_CacheService extends T_Generic implements CacheableFactory {
36
37     protected CacheFactory cf;
38
39     public Cacheable newCacheable(CacheManager cm) {
40         return new T_CachedInteger();
41     }
42
43     /**
44         @exception T_Fail - the test has failed.
45     */

46     protected void runTests() throws T_Fail {
47
48         DaemonFactory df;
49         try {
50             cf = (CacheFactory) Monitor.startSystemModule(getModuleToTestProtocolName());
51             df = (DaemonFactory) Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory);
52         } catch (StandardException mse) {
53             throw T_Fail.exceptionFail(mse);
54         }
55         if (cf == null) {
56             throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " module not started.");
57         }
58         if (df == null)
59             throw T_Fail.testFailMsg(org.apache.derby.iapi.reference.Module.DaemonFactory + " module not started.");
60     
61
62         try {
63
64             DaemonService ds = df.createNewDaemon("CacheTester");
65             if (ds == null)
66                 throw T_Fail.testFailMsg("Can't create deamon service");
67
68             CacheManager cm1 = cf.newCacheManager(this, "testCache1", 20, 40);
69             if (cm1 == null)
70                 throw T_Fail.testFailMsg("unable to create cache manager");
71             T001(cm1, 40);
72             cm1.useDaemonService(ds);
73             thrashCache(cm1, 10, 1000);
74             cm1.shutdown();
75             cm1 = null;
76
77             CacheManager cm2 = cf.newCacheManager(this, "testCache2", 0, 1);
78             if (cm2 == null)
79                 throw T_Fail.testFailMsg("unable to create cache manager");
80             T001(cm2, 1);
81             cm2.useDaemonService(ds);
82             thrashCache(cm2, 10, 1000);
83             cm2.shutdown();
84             cm2 = null;
85
86             CacheManager cm3= cf.newCacheManager(this, "testCache3", 2000, 40);
87             if (cm3 == null)
88                 throw T_Fail.testFailMsg("unable to create cache manager");
89             T001(cm3, 40);
90             cm3.useDaemonService(ds);
91             thrashCache(cm3, 10, 1000);
92             cm3.shutdown();
93             cm3 = null;
94
95             // now two that don't use the daemon service
96
CacheManager cm4 = cf.newCacheManager(this, "testCache4", 2000, 40);
97             if (cm4 == null)
98                 throw T_Fail.testFailMsg("unable to create cache manager");
99             T001(cm4, 40);
100             thrashCache(cm4, 10, 1000);
101             cm4.shutdown();
102             cm4 = null;
103
104             CacheManager cm5 = cf.newCacheManager(this, "testCache5", 0, 40);
105             if (cm5 == null)
106                 throw T_Fail.testFailMsg("unable to create cache manager");
107             T001(cm5, 40);
108             thrashCache(cm5, 10, 1000);
109             cm5.shutdown();
110             cm5 = null;
111
112         } catch (StandardException se) {
113             throw T_Fail.exceptionFail(se);
114         } catch (Throwable JavaDoc t) {
115             t.printStackTrace();
116             throw T_Fail.exceptionFail(t);
117         }
118     }
119
120     /**
121       Get the name of the protocol for the module to test.
122       This is the 'factory.MODULE' variable.
123       
124       'moduleName' to the name of the module to test.
125
126       @param testConfiguration the configuration for this test.
127       */

128     protected String JavaDoc getModuleToTestProtocolName() {
129         return org.apache.derby.iapi.reference.Module.CacheFactory;
130     }
131
132
133     /*
134     ** The tests
135     */

136
137     /**
138         Test the find and findCached calls.
139         @exception StandardException Standard Derby Error policy
140         @exception T_Fail Some error
141     */

142     protected void T001(CacheManager cm, int cacheSize) throws T_Fail, StandardException {
143
144         T_Key tkey1 = T_Key.simpleInt(1);
145
146         // cahce is empty, nothing should be there
147
t_findCachedFail(cm, tkey1);
148
149         // find a valid entry
150
cm.release(t_findSucceed(cm, tkey1));
151
152         // check it is still in the cache
153
cm.release(t_findCachedSucceed(cm, tkey1));
154
155         // look for an item that can't be found
156
T_Key tkey2 = T_Key.dontFindInt(2);
157         t_findCachedFail(cm, tkey2);
158         t_findFail(cm, tkey2);
159
160         // see if the first item still can be found
161
// can't assume it can be cached as it may have aged out ...
162
cm.release(t_findSucceed(cm, tkey1));
163
164         // now ensure we can find an item with the key that just couldn't
165
// be found
166
tkey2 = T_Key.simpleInt(2);
167         cm.release(t_findSucceed(cm, tkey2));
168         cm.release(t_findSucceed(cm, tkey1));
169
170
171         // now create a key that will cause an exception ...
172
T_Key tkey3 = T_Key.exceptionInt(3);
173         t_findCachedFail(cm, tkey3);
174         try {
175             
176             t_findFail(cm, tkey3);
177             throw T_Fail.testFailMsg("find call lost user exception");
178         } catch (StandardException se) {
179             if (!(se instanceof T_CacheException))
180                 throw se;
181             if (((T_CacheException) se).getType() != T_CacheException.IDENTITY_FAIL)
182                 throw se;
183         }
184
185         tkey3 = T_Key.simpleInt(3);
186         cm.release(t_findSucceed(cm, tkey3));
187         cm.release(t_findSucceed(cm, tkey2));
188         cm.release(t_findSucceed(cm, tkey1));
189
190         // since this cache is in use by only this method we should
191
// be able to call clean with deadlocking and then ageOut
192
// leaving the cache empty.
193
cm.cleanAll();
194         cm.ageOut();
195
196         t_findCachedFail(cm, tkey1);
197         t_findCachedFail(cm, tkey2);
198         t_findCachedFail(cm, tkey3);
199
200
201         // now put many valid objects into the cache
202
for (int i = 0; i < 4 * cacheSize ; i++) {
203             T_Key tkeyi = T_Key.simpleInt(i);
204             cm.release(t_findSucceed(cm, tkeyi));
205             cm.release(t_findCachedSucceed(cm, tkeyi));
206         }
207         cm.cleanAll();
208         cm.ageOut();
209         for (int i = 0; i < 4 * cacheSize ; i++) {
210             T_Key tkeyi = T_Key.simpleInt(i);
211             t_findCachedFail(cm, tkeyi);
212         }
213
214
215         // Ensure that we can find an object multiple times
216
Cacheable e1 = t_findSucceed(cm, tkey1);
217         Cacheable e2 = t_findSucceed(cm, tkey2);
218
219         if (e1 == e2)
220             throw T_Fail.testFailMsg("same object returned for two different keys");
221
222         if (t_findSucceed(cm, tkey1) != e1)
223             throw T_Fail.testFailMsg("different object returned for same key");
224         if (t_findSucceed(cm, tkey2) != e2)
225             throw T_Fail.testFailMsg("different object returned for same key");
226
227         cm.release(e1);
228         cm.release(e1);
229         e1 = null;
230         cm.release(e2);
231         cm.release(e2);
232         e2 = null;
233
234
235
236         
237         PASS("T001");
238     }
239
240
241
242
243     /*
244     ** Multi-user tests
245     */

246
247
248     protected void thrashCache(CacheManager cm, int threads, int iterations) throws T_Fail {
249
250         Thread JavaDoc[] children = new Thread JavaDoc[threads];
251
252         for (int i = 0; i < threads; i++) {
253
254             children[i] = new Thread JavaDoc(new T_CacheUser(cm, iterations, this, out));
255             
256         }
257
258         for (int i = 0; i < threads; i++) {
259             children[i].start();
260         }
261
262         try {
263             for (int i = 0; i < threads; i++) {
264                 if (threadFail != null)
265                     throw threadFail;
266
267                 children[i].join();
268
269                 if (threadFail != null)
270                     throw threadFail;
271             }
272         } catch (InterruptedException JavaDoc ie) {
273             throw T_Fail.exceptionFail(ie);
274         }
275
276         PASS("thrashCache");
277
278     }
279     protected T_Fail threadFail;
280     public synchronized void setChildException(T_Fail tf) {
281         if (threadFail == null)
282             threadFail = tf;
283     }
284
285
286     /**
287         A call to findCached() that is expected to return nothing.
288         @exception StandardException Standard Derby Error policy
289         @exception T_Fail Something was found.
290     */

291     protected void t_findCachedFail(CacheManager cm, Object JavaDoc key) throws StandardException, T_Fail {
292         Cacheable entry = cm.findCached(key);
293         if (entry != null) {
294             throw T_Fail.testFailMsg("found cached item unexpectedly");
295         }
296     }
297
298     /**
299         A call to findCached() that is expected to find something.
300         @exception StandardException Standard Derby Error policy
301         @exception T_Fail Nothing was found.
302     */

303     protected Cacheable t_findCachedSucceed(CacheManager cm, Object JavaDoc key) throws StandardException, T_Fail {
304         Cacheable entry = cm.findCached(key);
305         if (entry == null) {
306             throw T_Fail.testFailMsg("expected item to be in cache");
307         }
308
309         if (!entry.getIdentity().equals(key))
310             throw T_Fail.testFailMsg("item returned does not match key");
311         return entry;
312     }
313     /**
314         A call to find() that is expected to return nothing.
315
316         @exception T_Fail Something was found.
317         @exception StandardException Standard Derby Error policy
318     */

319     protected void t_findFail(CacheManager cm, Object JavaDoc key) throws T_Fail, StandardException {
320         Cacheable entry = cm.find(key);
321         if (entry != null) {
322             throw T_Fail.testFailMsg("found item unexpectedly");
323         }
324     }
325
326     /**
327         A call to findCached() that is expected to find something.
328
329         @exception T_Fail Nothing was found.
330         @exception StandardException Standard Derby Error policy
331     */

332     protected Cacheable t_findSucceed(CacheManager cm, Object JavaDoc key) throws T_Fail, StandardException {
333         Cacheable entry = cm.find(key);
334         if (entry == null) {
335             throw T_Fail.testFailMsg("expected item to be found");
336         }
337         if (!entry.getIdentity().equals(key))
338             throw T_Fail.testFailMsg("item returned does not match key");
339
340         return entry;
341     }
342 }
343
Popular Tags