1 package org.jboss.cache.eviction; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.factories.XmlConfigurationParser; 9 import org.jboss.cache.lock.IsolationLevel; 10 import org.jboss.cache.misc.TestingUtil; 11 12 19 public class LRUPolicyTest extends TestCase 20 { 21 CacheImpl cache_; 22 int wakeupIntervalMillis_ = 0; 23 final String ROOT_STR = "/test"; 24 Throwable t1_ex, t2_ex; 25 final long DURATION = 10000; 26 boolean isTrue; 27 28 public LRUPolicyTest(String s) 29 { 30 super(s); 31 } 32 33 public void setUp() throws Exception 34 { 35 super.setUp(); 36 initCaches(); 37 wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000; 38 log("wakeupInterval is " + wakeupIntervalMillis_); 39 if (wakeupIntervalMillis_ < 0) 40 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_); 41 42 t1_ex = t2_ex = null; 43 isTrue = true; 44 } 45 46 public void initCaches() throws Exception 47 { 48 cache_ = new CacheImpl(); 49 cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-lru-eviction-service.xml")); cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 51 cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 52 cache_.start(); 53 } 54 55 public void tearDown() throws Exception 56 { 57 super.tearDown(); 58 cache_.stop(); 59 } 60 61 public void testInUseEviction() throws Exception 62 { 63 String rootStr = "/org/jboss/test/data/inuse/"; 64 Fqn fqn; 65 68 for (int i = 0; i < 10; i++) 69 { 70 String str = rootStr + i; 71 fqn = Fqn.fromString(str); 72 cache_.put(fqn, str, str); 73 } 74 75 TestingUtil.sleepThread(wakeupIntervalMillis_ + 500); 76 System.out.println("***************************** marking as in-use"); 77 cache_.getRegionManager().getRegion(Fqn.fromString(rootStr + 5), false).markNodeCurrentlyInUse(Fqn.fromString(rootStr + 5), 0); 78 79 for (int i = 10; i < 15; i++) 80 { 81 String str = rootStr + i; 82 fqn = Fqn.fromString(str); 83 cache_.put(fqn, str, str); 84 } 85 86 TestingUtil.sleepThread(wakeupIntervalMillis_ + 500); 87 88 for (int i = 0; i < 5; i++) 89 { 90 assertNull(cache_.get(Fqn.fromString(rootStr + i))); 91 } 92 93 assertNotNull(cache_.get(Fqn.fromString(rootStr + 5))); 94 95 for (int i = 6; i < 11; i++) 96 { 97 assertNull(cache_.get(Fqn.fromString(rootStr + i))); 98 } 99 for (int i = 11; i < 15; i++) 100 { 101 assertNotNull(cache_.get(Fqn.fromString(rootStr + i))); 102 } 103 } 104 105 public void testEviction() 106 { 107 String rootStr = "/org/jboss/test/data/"; 108 for (int i = 0; i < 10; i++) 109 { 110 String str = rootStr + i; 111 Fqn fqn = Fqn.fromString(str); 112 try 113 { 114 cache_.put(fqn, str, str); 115 } 116 catch (Exception e) 117 { 118 fail("Failed to insert data" + e); 119 e.printStackTrace(); 120 } 121 } 122 System.out.println(cache_.toString()); 123 TestingUtil.sleepThread(wakeupIntervalMillis_ + 500); 124 System.out.println(cache_.toString()); 125 try 126 { 127 String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 128 assertNull("DataNode should be empty ", val); 129 } 130 catch (Exception e) 131 { 132 e.printStackTrace(); 133 fail("Failed to get" + e); 134 } 135 } 136 137 public void testNodeVisited() 138 { 139 String rootStr = "/org/jboss/test/data/"; 140 141 System.out.println("REGIONS: " + cache_.getRegionManager().dumpRegions()); 142 143 for (int i = 0; i < 10; i++) 144 { 145 String str = rootStr + i; 146 Fqn fqn = Fqn.fromString(str); 147 try 148 { 149 cache_.put(fqn, str, str); 150 } 151 catch (Exception e) 152 { 153 fail("Failed to insert data" + e); 154 e.printStackTrace(); 155 } 156 } 157 System.out.println("cache is:\n" + cache_.toString(true)); 158 159 int period = (wakeupIntervalMillis_ / 2 + 500); 160 log("sleeping for " + period + "ms"); 161 TestingUtil.sleepThread(period); String str = rootStr + "7"; 163 Fqn fqn = Fqn.fromString(str); 164 try 165 { 166 cache_.get(fqn, str); System.out.println("-- sleeping for " + period + "ms"); 168 TestingUtil.sleepThread(period); cache_.get(fqn, str); System.out.println("-- sleeping for " + period + "ms"); 171 TestingUtil.sleepThread(period); String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 173 System.out.println("-- val=" + val); 174 assertNull("DataNode should be empty ", val); 175 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 176 System.out.println("-- val=" + val); 177 assertNotNull("DataNode should not be empty ", val); 178 System.out.println("-- sleeping for " + (wakeupIntervalMillis_ + 1000) + "ms"); 179 TestingUtil.sleepThread(wakeupIntervalMillis_ + 1000); 180 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 181 System.out.println("-- val=" + val); 182 assertNull("DataNode should be empty ", val); 183 } 184 catch (Exception e) 185 { 186 e.printStackTrace(); 187 fail("Failed to evict" + e); 188 } 189 } 190 191 public void testNodeRemoved() 192 { 193 String rootStr = "/org/jboss/test/data/"; 194 for (int i = 0; i < 10; i++) 195 { 196 String str = rootStr + i + "/" + i; 197 Fqn fqn = Fqn.fromString(str); 198 try 199 { 200 cache_.put(fqn, str, str); 201 } 202 catch (Exception e) 203 { 204 fail("Failed to insert data" + e); 205 e.printStackTrace(); 206 } 207 } 208 209 int period = (wakeupIntervalMillis_ / 2 + 500); 210 log("period is " + period); 211 String str1 = rootStr + "7"; 213 Fqn fqn1 = Fqn.fromString(str1); 214 String str2 = rootStr + "7/7"; 215 Fqn fqn2 = Fqn.fromString(str2); 216 try 217 { 218 cache_.get(fqn1, str1); cache_.get(fqn2, str2); TestingUtil.sleepThread(period); cache_.get(fqn1, str1); cache_.get(fqn2, str2); TestingUtil.sleepThread(period); String val = (String ) cache_.get(rootStr + "7/7", rootStr + "7/7"); 225 assertNotNull("DataNode should not be empty ", val); 226 cache_.remove(fqn1); 227 TestingUtil.sleepThread(wakeupIntervalMillis_ + 500); 228 val = (String ) cache_.get(rootStr + "7/7", rootStr + "7/7"); 229 assertNull("DataNode should be empty ", val); 230 } 231 catch (Exception e) 232 { 233 e.printStackTrace(); 234 fail("Failed to evict" + e); 235 } 236 } 237 238 239 class MyPutter extends Thread 240 { 241 242 public MyPutter(String name) 243 { 244 super(name); 245 } 246 247 public void run() 248 { 249 int i = 0; 250 final String myName = ROOT_STR + "/test1/node" + getName(); 251 while (isTrue) 252 { 253 try 254 { 255 cache_.put(myName + i++, "value", i); 256 sleep(1); 257 } 258 catch (Throwable e) 259 { 260 e.printStackTrace(); 261 if (t1_ex == null) 262 t1_ex = e; 263 } 264 } 265 } 266 } 267 268 269 public void testConcurrentPutAndEvict() throws Exception 270 { 271 cache_.stop(); 272 cache_.destroy(); 273 cache_.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ); 274 cache_.create(); 275 cache_.start(); 276 cache_.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1); 277 278 for (int i = 0; i < 10; i++) 279 { 280 new MyPutter("Putter" + i).start(); 281 } 282 283 int counter = 0; 284 while (true) 285 { 286 counter++; 287 if (t1_ex != null) 288 { 289 fail("Exception generated in put() " + t1_ex); 290 } 291 log("nodes/locks: " + cache_.getNumberOfNodes() + "/" + cache_.getNumberOfLocksHeld()); 292 TestingUtil.sleepThread(1000); 293 if (counter > 10) 294 { isTrue = false; 296 break; 297 } 298 } 299 } 300 301 public void testForEvictionInternalError() 302 { 303 try 304 { 305 String rootStr = "/test/testdata"; 307 308 for (int i = 0; i < 10; i++) 309 { 310 String str = rootStr + i; 311 Fqn fqn = Fqn.fromString(str); 312 try 313 { 314 cache_.put(fqn, str, str); 315 } 316 catch (Exception e) 317 { 318 fail("Failed to insert data" + e); 319 e.printStackTrace(); 320 } 321 } 322 323 TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 2000); 325 326 String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 327 assertNull("DataNode should be empty ", val); 328 329 for (int i = 0; i < 10; i++) 331 { 332 String str = rootStr + i; 333 Fqn fqn = Fqn.fromString(str); 334 try 335 { 336 cache_.put(fqn, str, str); 337 } 338 catch (Exception e) 339 { 340 fail("Failed to insert data" + e); 341 e.printStackTrace(); 342 } 343 } 344 345 Fqn root = cache_.get("/").getFqn(); 347 cache_.remove(root); 348 349 TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 1000); 351 352 val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 353 assertNull("DataNode should be empty ", val); 354 355 } 356 catch (Exception e) 357 { 358 e.printStackTrace(); 359 fail("Failed to get" + e); 360 } 361 } 362 363 void log(String msg) 364 { 365 System.out.println("-- " + msg); 366 } 367 368 public static Test suite() 369 { 370 return new TestSuite(LRUPolicyTest.class); 371 } 372 373 public static void main(String [] args) 374 { 375 junit.textui.TestRunner.run(suite()); 376 } 377 378 } 379 | Popular Tags |