1 7 package org.jboss.test.cache.test.eviction; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.PropertyConfigurator; 14 import org.jboss.cache.aop.AOPInstance; 15 import org.jboss.cache.aop.TreeCacheAop; 16 17 20 public class AopLRUPolicyUnitTestCase extends TestCase 21 { 22 TreeCacheAop cache_; 23 int wakeupIntervalMillis_ = 0; 24 25 public AopLRUPolicyUnitTestCase(String s) 26 { 27 super(s); 28 } 29 30 public void setUp() throws Exception 31 { 32 super.setUp(); 33 initCaches(); 34 wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() *1000; 35 log("wakeupInterval is " +wakeupIntervalMillis_); 36 if(wakeupIntervalMillis_ <=0) 37 fail("testEviction(): eviction thread wake up interval is illegal " +wakeupIntervalMillis_); 38 39 } 40 41 void initCaches() throws Exception 42 { 43 cache_ = new TreeCacheAop(); 45 PropertyConfigurator config = new PropertyConfigurator(); 46 config.configure(cache_, "META-INF/local-aop-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 48 cache_.startService(); 49 } 50 51 public void tearDown() throws Exception 52 { 53 super.tearDown(); 54 cache_.stopService(); 55 } 56 57 public void testSimpleEviction() { 58 String rootStr = "/aop/"; 59 AOPInstance aop = new AOPInstance(); 60 try { 61 for(int i=0; i < 4; i++) { 62 String stri = rootStr +i; 63 Fqn fqni = Fqn.fromString(stri); 64 cache_.put(fqni, stri, stri); 65 cache_.put(fqni, AOPInstance.KEY, aop); cache_.put(fqni, TreeCacheAop.CLASS_INTERNAL, String .class); for(int j=0; j < 3; j++) { 68 String strj = stri +"/" +j; 69 Fqn fqnj = Fqn.fromString(strj); 70 cache_.put(fqnj, strj, strj); 71 } 72 } 73 } catch (Exception e) { 74 e.printStackTrace(); 75 fail("Failed to insert data" +e); 76 } 77 78 int period = (wakeupIntervalMillis_ +1000); 79 log("period is " +period); 80 _sleep(period); 82 try { 83 String str = rootStr + "0"; 84 String val = (String )cache_.get(Fqn.fromString(str), str); 85 assertNull("Node should be empty ", val); 86 str = rootStr + "3"; 87 val = (String )cache_.get(Fqn.fromString(str), str); 88 assertNotNull("Node should not be empty if maxElements is 4 ", val); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } 92 } 93 94 public void testUpdateEviction() { 95 String rootStr = "/aop/"; 96 AOPInstance aop = new AOPInstance(); 97 try { 98 for(int i=0; i < 4; i++) { 99 String stri = rootStr +i; 100 Fqn fqni = Fqn.fromString(stri); 101 cache_.put(fqni, stri, stri); 102 cache_.put(fqni, AOPInstance.KEY, aop); cache_.put(fqni, TreeCacheAop.CLASS_INTERNAL, String .class); for(int j=0; j < 2; j++) { 105 String strj = stri +"/" +j; 106 Fqn fqnj = Fqn.fromString(strj); 107 cache_.put(fqnj, strj, strj); 108 } 109 } 110 } catch (Exception e) { 111 e.printStackTrace(); 112 fail("Failed to insert data" +e); 113 } 114 115 int period = (wakeupIntervalMillis_ +1000); 116 log("period is " +period); 117 _sleep(period); String str = rootStr + "3"; 119 Fqn fqn = Fqn.fromString(str); 120 try { 121 cache_.get(fqn, str); _sleep(period); cache_.get(fqn, str); _sleep(period); String val = (String )cache_.get(rootStr +"3/1", rootStr+"3/1"); 126 assertNotNull("Node should not be empty ", val); 127 _sleep(wakeupIntervalMillis_*2 +2000); 128 val = (String )cache_.get(rootStr +"3", rootStr+"3"); 129 assertNull("Node should be empty. But this is broken because of TreeCache._removeData() has disabled " + 130 "sendNodeEvent. See the FIXME. ", val); 131 } catch (Exception e) { 132 e.printStackTrace(); 133 } 134 } 135 136 public void testRemoveEviction() { 137 String rootStr = "/aop/"; 138 AOPInstance aop = new AOPInstance(); 139 try { 140 for(int i=0; i < 4; i++) { 141 String stri = rootStr +i; 142 Fqn fqni = Fqn.fromString(stri); 143 cache_.put(fqni, stri, stri); 144 cache_.put(fqni, AOPInstance.KEY, aop); for(int j=0; j < 2; j++) { 146 String strj = stri +"/" +j; 147 Fqn fqnj = Fqn.fromString(strj); 148 cache_.put(fqnj, strj, strj); 149 } 150 } 151 } catch (Exception e) { 152 e.printStackTrace(); 153 fail("Failed to insert data" +e); 154 } 155 156 int period = (wakeupIntervalMillis_ +1000); 157 log("period is " +period); 158 _sleep(period); String str = rootStr + "3"; 160 Fqn fqn = Fqn.fromString(str); 161 try { 162 cache_.get(fqn, str); _sleep(period); cache_.get(fqn, str); _sleep(period); String val = (String )cache_.get(rootStr +"3/1", rootStr+"3/1"); 167 assertNotNull("Node should not be empty ", val); 168 cache_.remove(rootStr +"3"); 169 val = (String )cache_.get(rootStr +"3", rootStr+"3"); 170 assertNull("Node should be empty ", val); 171 _sleep(wakeupIntervalMillis_ +1000); 172 } catch (Exception e) { 173 e.printStackTrace(); 174 } 175 } 176 177 void _sleep(long msecs) { 178 try { 179 Thread.sleep(msecs); 180 } catch (InterruptedException e) { 181 e.printStackTrace(); } 183 } 184 void log(String msg) 185 { 186 System.out.println("-- " + msg); 187 } 188 189 public static Test suite() 190 { 191 return new TestSuite(AopLRUPolicyUnitTestCase.class); 192 } 193 194 public static void main(String [] args) 195 { 196 junit.textui.TestRunner.run(suite()); 197 } 198 199 } 200 | Popular Tags |