1 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 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 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 t) { 115 t.printStackTrace(); 116 throw T_Fail.exceptionFail(t); 117 } 118 } 119 120 128 protected String getModuleToTestProtocolName() { 129 return org.apache.derby.iapi.reference.Module.CacheFactory; 130 } 131 132 133 136 137 142 protected void T001(CacheManager cm, int cacheSize) throws T_Fail, StandardException { 143 144 T_Key tkey1 = T_Key.simpleInt(1); 145 146 t_findCachedFail(cm, tkey1); 148 149 cm.release(t_findSucceed(cm, tkey1)); 151 152 cm.release(t_findCachedSucceed(cm, tkey1)); 154 155 T_Key tkey2 = T_Key.dontFindInt(2); 157 t_findCachedFail(cm, tkey2); 158 t_findFail(cm, tkey2); 159 160 cm.release(t_findSucceed(cm, tkey1)); 163 164 tkey2 = T_Key.simpleInt(2); 167 cm.release(t_findSucceed(cm, tkey2)); 168 cm.release(t_findSucceed(cm, tkey1)); 169 170 171 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 cm.cleanAll(); 194 cm.ageOut(); 195 196 t_findCachedFail(cm, tkey1); 197 t_findCachedFail(cm, tkey2); 198 t_findCachedFail(cm, tkey3); 199 200 201 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 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 246 247 248 protected void thrashCache(CacheManager cm, int threads, int iterations) throws T_Fail { 249 250 Thread [] children = new Thread [threads]; 251 252 for (int i = 0; i < threads; i++) { 253 254 children[i] = new Thread (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 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 291 protected void t_findCachedFail(CacheManager cm, Object 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 303 protected Cacheable t_findCachedSucceed(CacheManager cm, Object 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 319 protected void t_findFail(CacheManager cm, Object 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 332 protected Cacheable t_findSucceed(CacheManager cm, Object 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 |