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.TreeCache; 15 import org.jboss.cache.Node; 16 17 20 public class LRUPolicyUnitTestCase extends TestCase 21 { 22 TreeCache cache_; 23 int wakeupIntervalMillis_ = 0; 24 final String ROOT_STR = "/test"; 25 Throwable t1_ex, t2_ex; 26 final long DURATION = 10000; 27 boolean isTrue; 28 29 public LRUPolicyUnitTestCase(String s) 30 { 31 super(s); 32 } 33 34 public void setUp() throws Exception 35 { 36 super.setUp(); 37 initCaches(); 38 wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000; 39 log("wakeupInterval is " + wakeupIntervalMillis_); 40 if (wakeupIntervalMillis_ <= 0) 41 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_); 42 43 t1_ex = t2_ex = null; 44 isTrue = true; 45 } 46 47 void initCaches() throws Exception 48 { 49 cache_ = new TreeCache(); 51 PropertyConfigurator config = new PropertyConfigurator(); 52 config.configure(cache_, "META-INF/local-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 54 cache_.startService(); 55 } 56 57 public void tearDown() throws Exception 58 { 59 super.tearDown(); 60 cache_.stopService(); 61 } 62 63 public void testEviction() 64 { 65 String rootStr = "/org/jboss/test/data/"; 66 for (int i = 0; i < 10; i++) { 67 String str = rootStr + i; 68 Fqn fqn = Fqn.fromString(str); 69 try { 70 cache_.put(fqn, str, str); 71 } catch (Exception e) { 72 fail("Failed to insert data" + e); 73 e.printStackTrace(); 74 } 75 } 76 77 _sleep(2 * wakeupIntervalMillis_); 78 try { 79 String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 80 assertNull("Node should be empty ", val); 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } 84 } 85 86 public void testNodeVisited() 87 { 88 String rootStr = "/org/jboss/test/data/"; 89 for (int i = 0; i < 10; i++) { 90 String str = rootStr + i; 91 Fqn fqn = Fqn.fromString(str); 92 try { 93 cache_.put(fqn, str, str); 94 } catch (Exception e) { 95 fail("Failed to insert data" + e); 96 e.printStackTrace(); 97 } 98 } 99 100 int period = (wakeupIntervalMillis_ / 2 + 1000); 101 log("period is " + period); 102 _sleep(period); String str = rootStr + "7"; 104 Fqn fqn = Fqn.fromString(str); 105 try { 106 cache_.get(fqn, str); _sleep(period); cache_.get(fqn, str); _sleep(period); String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 111 assertNull("Node should be empty ", val); 112 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 113 assertNotNull("Node should not be empty ", val); 114 _sleep(wakeupIntervalMillis_ * 2 + 2000); 115 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 116 assertNull("Node should be empty ", val); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 } 121 122 public void testNodeRemoved() 123 { 124 String rootStr = "/org/jboss/test/data/"; 125 for (int i = 0; i < 10; i++) { 126 String str = rootStr + i + "/" + i; 127 Fqn fqn = Fqn.fromString(str); 128 try { 129 cache_.put(fqn, str, str); 130 } catch (Exception e) { 131 fail("Failed to insert data" + e); 132 e.printStackTrace(); 133 } 134 } 135 136 int period = (wakeupIntervalMillis_ / 2 + 1000); 137 log("period is " + period); 138 _sleep(period); String str1 = rootStr + "7"; 140 Fqn fqn1 = Fqn.fromString(str1); 141 String str2 = rootStr + "7/7"; 142 Fqn fqn2 = Fqn.fromString(str2); 143 try { 144 cache_.get(fqn1, str1); cache_.get(fqn2, str2); _sleep(period); cache_.get(fqn1, str1); cache_.get(fqn2, str2); _sleep(period); String val = (String ) cache_.get(rootStr + "7/7", rootStr + "7/7"); 151 assertNotNull("Node should not be empty ", val); 152 cache_.remove(fqn1); 153 _sleep(wakeupIntervalMillis_ * 2 + 2000); 154 val = (String ) cache_.get(rootStr + "7/7", rootStr + "7/7"); 155 assertNull("Node should be empty ", val); 156 } catch (Exception e) { 157 e.printStackTrace(); 158 } 159 } 160 161 202 203 void _sleep(long msecs) 204 { 205 try { 206 Thread.sleep(msecs); 207 } catch (InterruptedException e) { 208 e.printStackTrace(); } 210 } 211 212 void log(String msg) 213 { 214 System.out.println("-- " + msg); 215 } 216 217 public static Test suite() 218 { 219 return new TestSuite(LRUPolicyUnitTestCase.class); 220 } 221 222 public static void main(String [] args) 223 { 224 junit.textui.TestRunner.run(suite()); 225 } 226 227 } 228 | Popular Tags |