001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_CacheService
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Generic;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import org.apache.derby.iapi.services.cache.*;
028:
029: import org.apache.derby.iapi.services.daemon.*;
030:
031: import org.apache.derby.iapi.services.monitor.Monitor;
032:
033: import org.apache.derby.iapi.error.StandardException;
034:
035: public class T_CacheService extends T_Generic implements
036: CacheableFactory {
037:
038: protected CacheFactory cf;
039:
040: public Cacheable newCacheable(CacheManager cm) {
041: return new T_CachedInteger();
042: }
043:
044: /**
045: @exception T_Fail - the test has failed.
046: */
047: protected void runTests() throws T_Fail {
048:
049: DaemonFactory df;
050: try {
051: cf = (CacheFactory) Monitor
052: .startSystemModule(getModuleToTestProtocolName());
053: df = (DaemonFactory) Monitor
054: .startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory);
055: } catch (StandardException mse) {
056: throw T_Fail.exceptionFail(mse);
057: }
058: if (cf == null) {
059: throw T_Fail.testFailMsg(getModuleToTestProtocolName()
060: + " module not started.");
061: }
062: if (df == null)
063: throw T_Fail
064: .testFailMsg(org.apache.derby.iapi.reference.Module.DaemonFactory
065: + " module not started.");
066:
067: try {
068:
069: DaemonService ds = df.createNewDaemon("CacheTester");
070: if (ds == null)
071: throw T_Fail.testFailMsg("Can't create deamon service");
072:
073: CacheManager cm1 = cf.newCacheManager(this , "testCache1",
074: 20, 40);
075: if (cm1 == null)
076: throw T_Fail
077: .testFailMsg("unable to create cache manager");
078: T001(cm1, 40);
079: cm1.useDaemonService(ds);
080: thrashCache(cm1, 10, 1000);
081: cm1.shutdown();
082: cm1 = null;
083:
084: CacheManager cm2 = cf.newCacheManager(this , "testCache2",
085: 0, 1);
086: if (cm2 == null)
087: throw T_Fail
088: .testFailMsg("unable to create cache manager");
089: T001(cm2, 1);
090: cm2.useDaemonService(ds);
091: thrashCache(cm2, 10, 1000);
092: cm2.shutdown();
093: cm2 = null;
094:
095: CacheManager cm3 = cf.newCacheManager(this , "testCache3",
096: 2000, 40);
097: if (cm3 == null)
098: throw T_Fail
099: .testFailMsg("unable to create cache manager");
100: T001(cm3, 40);
101: cm3.useDaemonService(ds);
102: thrashCache(cm3, 10, 1000);
103: cm3.shutdown();
104: cm3 = null;
105:
106: // now two that don't use the daemon service
107: CacheManager cm4 = cf.newCacheManager(this , "testCache4",
108: 2000, 40);
109: if (cm4 == null)
110: throw T_Fail
111: .testFailMsg("unable to create cache manager");
112: T001(cm4, 40);
113: thrashCache(cm4, 10, 1000);
114: cm4.shutdown();
115: cm4 = null;
116:
117: CacheManager cm5 = cf.newCacheManager(this , "testCache5",
118: 0, 40);
119: if (cm5 == null)
120: throw T_Fail
121: .testFailMsg("unable to create cache manager");
122: T001(cm5, 40);
123: thrashCache(cm5, 10, 1000);
124: cm5.shutdown();
125: cm5 = null;
126:
127: } catch (StandardException se) {
128: throw T_Fail.exceptionFail(se);
129: } catch (Throwable t) {
130: t.printStackTrace();
131: throw T_Fail.exceptionFail(t);
132: }
133: }
134:
135: /**
136: Get the name of the protocol for the module to test.
137: This is the 'factory.MODULE' variable.
138:
139: 'moduleName' to the name of the module to test.
140:
141: @param testConfiguration the configuration for this test.
142: */
143: protected String getModuleToTestProtocolName() {
144: return org.apache.derby.iapi.reference.Module.CacheFactory;
145: }
146:
147: /*
148: ** The tests
149: */
150:
151: /**
152: Test the find and findCached calls.
153: @exception StandardException Standard Derby Error policy
154: @exception T_Fail Some error
155: */
156: protected void T001(CacheManager cm, int cacheSize) throws T_Fail,
157: StandardException {
158:
159: T_Key tkey1 = T_Key.simpleInt(1);
160:
161: // cahce is empty, nothing should be there
162: t_findCachedFail(cm, tkey1);
163:
164: // find a valid entry
165: cm.release(t_findSucceed(cm, tkey1));
166:
167: // check it is still in the cache
168: cm.release(t_findCachedSucceed(cm, tkey1));
169:
170: // look for an item that can't be found
171: T_Key tkey2 = T_Key.dontFindInt(2);
172: t_findCachedFail(cm, tkey2);
173: t_findFail(cm, tkey2);
174:
175: // see if the first item still can be found
176: // can't assume it can be cached as it may have aged out ...
177: cm.release(t_findSucceed(cm, tkey1));
178:
179: // now ensure we can find an item with the key that just couldn't
180: // be found
181: tkey2 = T_Key.simpleInt(2);
182: cm.release(t_findSucceed(cm, tkey2));
183: cm.release(t_findSucceed(cm, tkey1));
184:
185: // now create a key that will cause an exception ...
186: T_Key tkey3 = T_Key.exceptionInt(3);
187: t_findCachedFail(cm, tkey3);
188: try {
189:
190: t_findFail(cm, tkey3);
191: throw T_Fail.testFailMsg("find call lost user exception");
192: } catch (StandardException se) {
193: if (!(se instanceof T_CacheException))
194: throw se;
195: if (((T_CacheException) se).getType() != T_CacheException.IDENTITY_FAIL)
196: throw se;
197: }
198:
199: tkey3 = T_Key.simpleInt(3);
200: cm.release(t_findSucceed(cm, tkey3));
201: cm.release(t_findSucceed(cm, tkey2));
202: cm.release(t_findSucceed(cm, tkey1));
203:
204: // since this cache is in use by only this method we should
205: // be able to call clean with deadlocking and then ageOut
206: // leaving the cache empty.
207: cm.cleanAll();
208: cm.ageOut();
209:
210: t_findCachedFail(cm, tkey1);
211: t_findCachedFail(cm, tkey2);
212: t_findCachedFail(cm, tkey3);
213:
214: // now put many valid objects into the cache
215: for (int i = 0; i < 4 * cacheSize; i++) {
216: T_Key tkeyi = T_Key.simpleInt(i);
217: cm.release(t_findSucceed(cm, tkeyi));
218: cm.release(t_findCachedSucceed(cm, tkeyi));
219: }
220: cm.cleanAll();
221: cm.ageOut();
222: for (int i = 0; i < 4 * cacheSize; i++) {
223: T_Key tkeyi = T_Key.simpleInt(i);
224: t_findCachedFail(cm, tkeyi);
225: }
226:
227: // Ensure that we can find an object multiple times
228: Cacheable e1 = t_findSucceed(cm, tkey1);
229: Cacheable e2 = t_findSucceed(cm, tkey2);
230:
231: if (e1 == e2)
232: throw T_Fail
233: .testFailMsg("same object returned for two different keys");
234:
235: if (t_findSucceed(cm, tkey1) != e1)
236: throw T_Fail
237: .testFailMsg("different object returned for same key");
238: if (t_findSucceed(cm, tkey2) != e2)
239: throw T_Fail
240: .testFailMsg("different object returned for same key");
241:
242: cm.release(e1);
243: cm.release(e1);
244: e1 = null;
245: cm.release(e2);
246: cm.release(e2);
247: e2 = null;
248:
249: PASS("T001");
250: }
251:
252: /*
253: ** Multi-user tests
254: */
255:
256: protected void thrashCache(CacheManager cm, int threads,
257: int iterations) throws T_Fail {
258:
259: Thread[] children = new Thread[threads];
260:
261: for (int i = 0; i < threads; i++) {
262:
263: children[i] = new Thread(new T_CacheUser(cm, iterations,
264: this , out));
265:
266: }
267:
268: for (int i = 0; i < threads; i++) {
269: children[i].start();
270: }
271:
272: try {
273: for (int i = 0; i < threads; i++) {
274: if (threadFail != null)
275: throw threadFail;
276:
277: children[i].join();
278:
279: if (threadFail != null)
280: throw threadFail;
281: }
282: } catch (InterruptedException ie) {
283: throw T_Fail.exceptionFail(ie);
284: }
285:
286: PASS("thrashCache");
287:
288: }
289:
290: protected T_Fail threadFail;
291:
292: public synchronized void setChildException(T_Fail tf) {
293: if (threadFail == null)
294: threadFail = tf;
295: }
296:
297: /**
298: A call to findCached() that is expected to return nothing.
299: @exception StandardException Standard Derby Error policy
300: @exception T_Fail Something was found.
301: */
302: protected void t_findCachedFail(CacheManager cm, Object key)
303: throws StandardException, T_Fail {
304: Cacheable entry = cm.findCached(key);
305: if (entry != null) {
306: throw T_Fail.testFailMsg("found cached item unexpectedly");
307: }
308: }
309:
310: /**
311: A call to findCached() that is expected to find something.
312: @exception StandardException Standard Derby Error policy
313: @exception T_Fail Nothing was found.
314: */
315: protected Cacheable t_findCachedSucceed(CacheManager cm, Object key)
316: throws StandardException, T_Fail {
317: Cacheable entry = cm.findCached(key);
318: if (entry == null) {
319: throw T_Fail.testFailMsg("expected item to be in cache");
320: }
321:
322: if (!entry.getIdentity().equals(key))
323: throw T_Fail
324: .testFailMsg("item returned does not match key");
325: return entry;
326: }
327:
328: /**
329: A call to find() that is expected to return nothing.
330:
331: @exception T_Fail Something was found.
332: @exception StandardException Standard Derby Error policy
333: */
334: protected void t_findFail(CacheManager cm, Object key)
335: throws T_Fail, StandardException {
336: Cacheable entry = cm.find(key);
337: if (entry != null) {
338: throw T_Fail.testFailMsg("found item unexpectedly");
339: }
340: }
341:
342: /**
343: A call to findCached() that is expected to find something.
344:
345: @exception T_Fail Nothing was found.
346: @exception StandardException Standard Derby Error policy
347: */
348: protected Cacheable t_findSucceed(CacheManager cm, Object key)
349: throws T_Fail, StandardException {
350: Cacheable entry = cm.find(key);
351: if (entry == null) {
352: throw T_Fail.testFailMsg("expected item to be found");
353: }
354: if (!entry.getIdentity().equals(key))
355: throw T_Fail
356: .testFailMsg("item returned does not match key");
357:
358: return entry;
359: }
360: }
|