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