1 22 23 package org.jboss.cache.eviction; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import org.jboss.cache.CacheImpl; 29 import org.jboss.cache.Fqn; 30 import org.jboss.cache.RegionManager; 31 import org.jboss.cache.config.EvictionConfig; 32 import org.jboss.cache.config.EvictionRegionConfig; 33 import org.jboss.cache.factories.XmlConfigurationParser; 34 import org.jboss.cache.lock.IsolationLevel; 35 import org.jboss.cache.misc.TestingUtil; 36 import org.jboss.cache.xml.XmlHelper; 37 import org.w3c.dom.Element ; 38 39 45 public class ProgrammaticLRUPolicyTest extends TestCase 46 { 47 CacheImpl cache_; 48 int wakeupIntervalMillis_ = 0; 49 50 public ProgrammaticLRUPolicyTest(String s) 51 { 52 super(s); 53 } 54 55 public void setUp() throws Exception 56 { 57 super.setUp(); 58 initCaches(); 59 wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000; 60 log("wakeupInterval is " + wakeupIntervalMillis_); 61 if (wakeupIntervalMillis_ < 0) 62 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_); 63 64 } 65 66 public void initCaches() throws Exception 67 { 68 cache_ = new CacheImpl(); 69 cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-lru-eviction-service.xml")); cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 71 cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 72 73 cache_.create(); 74 cache_.start(); 75 } 76 77 public void tearDown() throws Exception 78 { 79 super.tearDown(); 80 cache_.stop(); 81 } 82 83 private void addStringBasedRegion() throws Exception 84 { 85 String xml = "<region name=\"/dummy\">" + 87 "<attribute name=\"maxNodes\">10000</attribute>" + 88 "<attribute name=\"timeToLiveSeconds\">4</attribute>" + 89 "</region>"; 90 Element element = XmlHelper.stringToElement(xml); 91 RegionManager regionManager = cache_.getRegionManager(); 92 EvictionConfig topConfig = cache_.getConfiguration().getEvictionConfig(); 93 EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element, topConfig.getDefaultEvictionPolicyClass(), topConfig.getDefaultEventQueueSize()); 94 regionManager.setEvictionConfig(topConfig); 95 regionManager.getRegion("/programmatic", true).setEvictionPolicy(erc.getEvictionPolicyConfig()); 97 } 98 99 public void testStringBasedFqnEviction() throws Exception 100 { 101 addStringBasedRegion(); 102 103 String rootStr = "/programmatic/"; 104 for (int i = 0; i < 10; i++) 105 { 106 String str = rootStr + i; 107 Fqn fqn = Fqn.fromString(str); 108 cache_.put(fqn, str, str); 109 } 110 111 String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 112 assertNotNull("DataNode should be empty ", val); 113 114 System.out.println(cache_.toString()); 115 TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500); 116 System.out.println(cache_.toString()); 117 val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 118 assertNull("DataNode should be empty ", val); 119 } 120 121 private void addObjectBasedRegion() throws Exception 122 { 123 String xml = "<region name=\"/dummy\">" + 125 "<attribute name=\"maxNodes\">10000</attribute>" + 126 "<attribute name=\"timeToLiveSeconds\">4</attribute>" + 127 "</region>"; 128 Element element = XmlHelper.stringToElement(xml); 129 RegionManager regionManager = cache_.getRegionManager(); 130 EvictionConfig topEC = cache_.getConfiguration().getEvictionConfig(); 131 EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element, 132 topEC.getDefaultEvictionPolicyClass(), 133 topEC.getDefaultEventQueueSize()); 134 Integer ii = 1; 136 Fqn fqn = new Fqn(ii); 137 regionManager.getRegion(fqn, true).setEvictionPolicy(erc.getEvictionPolicyConfig()); 138 } 139 140 public void testObjectBasedFqnEviction1() throws Exception 141 { 142 addStringBasedRegion(); 143 144 String rootStr = "/programmatic/"; 145 for (int i = 0; i < 10; i++) 146 { 147 String str = rootStr; 148 Integer in = i; 149 Fqn fqn = new Fqn(Fqn.fromString(str), in); 150 try 151 { 152 cache_.put(fqn, str, str); 153 } 154 catch (Exception e) 155 { 156 fail("Failed to insert data" + e); 157 e.printStackTrace(); 158 } 159 } 160 161 Integer in = 3; 162 Fqn fqn = new Fqn(Fqn.fromString(rootStr), in); 163 try 164 { 165 String val = (String ) cache_.get(fqn, in); 166 assertNull("DataNode should be empty ", val); 167 } 168 catch (Exception e) 169 { 170 e.printStackTrace(); 171 fail("Failed to get" + e); 172 } 173 174 System.out.println(cache_.toString()); 175 TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500); 176 System.out.println(cache_.toString()); 177 178 try 179 { 180 String val = (String ) cache_.get(fqn, in); 181 assertNull("DataNode should be empty ", val); 182 } 183 catch (Exception e) 184 { 185 e.printStackTrace(); 186 fail("Failed to get" + e); 187 } 188 } 189 190 public void testObjectBasedFqnEviction2() throws Exception 191 { 192 addObjectBasedRegion(); 193 194 Integer ii = 1; 195 Fqn rootfqn = new Fqn(ii); 196 for (int i = 0; i < 10; i++) 197 { 198 Integer in = i; 199 Fqn fqn = new Fqn(rootfqn, in); 200 try 201 { 202 cache_.put(fqn, in, in); 203 } 204 catch (Exception e) 205 { 206 fail("Failed to insert data" + e); 207 e.printStackTrace(); 208 } 209 } 210 211 try 212 { 213 Integer in = 3; 214 Fqn fqn = new Fqn(rootfqn, in); 215 Object val = cache_.get(fqn, in); 216 assertNotNull("DataNode should be empty ", val); 217 } 218 catch (Exception e) 219 { 220 e.printStackTrace(); 221 fail("Failed to get" + e); 222 } 223 224 System.out.println(cache_.toString()); 225 TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500); 226 System.out.println(cache_.toString()); 227 try 228 { 229 Integer in = 3; 230 Fqn fqn = new Fqn(rootfqn, in); 231 Object val = cache_.get(fqn, in); 232 assertNull("DataNode should be empty ", val); 233 } 234 catch (Exception e) 235 { 236 e.printStackTrace(); 237 fail("Failed to get" + e); 238 } 239 } 240 241 void log(String msg) 242 { 243 System.out.println("-- " + msg); 244 } 245 246 public static Test suite() 247 { 248 return new TestSuite(ProgrammaticLRUPolicyTest.class); 249 } 250 251 public static void main(String [] args) 252 { 253 junit.textui.TestRunner.run(suite()); 254 } 255 256 } 257 | Popular Tags |