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.TreeCache; 9 import org.jboss.cache.lock.IsolationLevel; 10 11 14 public class LRUPolicyTest extends TestCase 15 { 16 TreeCache cache_; 17 int wakeupIntervalMillis_ = 0; 18 final String ROOT_STR = "/test"; 19 Throwable t1_ex, t2_ex; 20 final long DURATION = 10000; 21 boolean isTrue; 22 23 public LRUPolicyTest(String s) 24 { 25 super(s); 26 } 27 28 public void setUp() throws Exception 29 { 30 super.setUp(); 31 initCaches(); 32 wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000; 33 log("wakeupInterval is " + wakeupIntervalMillis_); 34 if (wakeupIntervalMillis_ < 0) 35 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_); 36 37 t1_ex = t2_ex = null; 38 isTrue = true; 39 } 40 41 void initCaches() throws Exception 42 { 43 cache_ = new TreeCache(); 44 PropertyConfigurator config = new PropertyConfigurator(); 45 config.configure(cache_, "META-INF/local-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 47 cache_.setIsolationLevel(IsolationLevel.SERIALIZABLE); 48 cache_.startService(); 49 } 50 51 public void tearDown() throws Exception 52 { 53 super.tearDown(); 54 cache_.stopService(); 55 } 56 57 public void testEviction() 58 { 59 String rootStr = "/org/jboss/test/data/"; 60 for (int i = 0; i < 10; i++) { 61 String str = rootStr + i; 62 Fqn fqn = Fqn.fromString(str); 63 try { 64 cache_.put(fqn, str, str); 65 } catch (Exception e) { 66 fail("Failed to insert data" + e); 67 e.printStackTrace(); 68 } 69 } 70 71 _sleep(2 * wakeupIntervalMillis_); 72 try { 73 String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 74 assertNull("Node should be empty ", val); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 } 79 80 public void testNodeVisited() 81 { 82 String rootStr = "/org/jboss/test/data/"; 83 for (int i = 0; i < 10; i++) { 84 String str = rootStr + i; 85 Fqn fqn = Fqn.fromString(str); 86 try { 87 cache_.put(fqn, str, str); 88 } catch (Exception e) { 89 fail("Failed to insert data" + e); 90 e.printStackTrace(); 91 } 92 } 93 System.out.println("cache is:\n" + cache_.toString(true)); 94 95 int period = (wakeupIntervalMillis_ / 2 + 1000); 96 log("sleeping for " + period + "ms"); 97 _sleep(period); String str = rootStr + "7"; 99 Fqn fqn = Fqn.fromString(str); 100 try { 101 cache_.get(fqn, str); System.out.println("-- sleeping for " + period + "ms"); 103 _sleep(period); cache_.get(fqn, str); System.out.println("-- sleeping for " + period + "ms"); 106 _sleep(period); String val = (String ) cache_.get(rootStr + "3", rootStr + "3"); 108 System.out.println("-- val=" + val); 109 assertNull("Node should be empty ", val); 110 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 111 System.out.println("-- val=" + val); 112 assertNotNull("Node should not be empty ", val); 113 System.out.println("-- sleeping for " + (wakeupIntervalMillis_ * 2 + 2000) + "ms"); 114 _sleep(wakeupIntervalMillis_ * 2 + 2000); 115 val = (String ) cache_.get(rootStr + "7", rootStr + "7"); 116 System.out.println("-- val=" + val); 117 assertNull("Node should be empty ", val); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 123 public void testNodeRemoved() 124 { 125 String rootStr = "/org/jboss/test/data/"; 126 for (int i = 0; i < 10; i++) { 127 String str = rootStr + i + "/" + i; 128 Fqn fqn = Fqn.fromString(str); 129 try { 130 cache_.put(fqn, str, str); 131 } catch (Exception e) { 132 fail("Failed to insert data" + e); 133 e.printStackTrace(); 134 } 135 } 136 137 int period = (wakeupIntervalMillis_ / 2 + 1000); 138 log("period is " + period); 139 _sleep(period); String str1 = rootStr + "7"; 141 Fqn fqn1 = Fqn.fromString(str1); 142 String str2 = rootStr + "7/7"; 143 Fqn fqn2 = Fqn.fromString(str2); 144 try { 145 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"); 152 assertNotNull("Node should not be empty ", val); 153 cache_.remove(fqn1); 154 _sleep(wakeupIntervalMillis_ * 2 + 2000); 155 val = (String ) cache_.get(rootStr + "7/7", rootStr + "7/7"); 156 assertNull("Node should be empty ", val); 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160 } 161 162 163 class MyPutter extends Thread { 164 165 public MyPutter(String name) { 166 super(name); 167 } 168 169 public void run() 170 { 171 int i=0; 172 final String myName=ROOT_STR + "/test1/node" + getName(); 173 while (isTrue) { 174 try { 175 cache_.put(myName + i++, "value", new Integer (i)); 176 sleep(1); 177 } 178 catch (Exception e) { 179 e.printStackTrace(); 180 if(t1_ex != null) 181 t1_ex = e; 182 } 183 } 184 } 185 } 186 187 188 public void testConcurrentPutAndEvict() throws Exception 189 { 190 cache_.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 191 cache_.put(ROOT_STR+ "/concurrentPutAndEvict", "value", new Integer (1)); 192 193 for (int i = 0; i < 10; i++) { 194 new MyPutter("Putter" + i).start(); 195 } 196 197 int counter = 0; 198 while (true) { 199 counter++; 200 if(t1_ex != null) { 201 fail("Exception generated in put() " +t1_ex); 202 } 203 log("nodes/locks: " + cache_.getNumberOfNodes() + "/" + cache_.getNumberOfLocksHeld()); 204 _sleep(1000); 205 if(counter > 10) { isTrue = false; 207 break; 208 } 209 } 210 } 211 212 void _sleep(long msecs) 213 { 214 try { 215 Thread.sleep(msecs); 216 } catch (InterruptedException e) { 217 e.printStackTrace(); } 219 } 220 221 void log(String msg) 222 { 223 System.out.println("-- " + msg); 224 } 225 226 public static Test suite() 227 { 228 return new TestSuite(LRUPolicyTest.class); 229 } 230 231 public static void main(String [] args) 232 { 233 junit.textui.TestRunner.run(suite()); 234 } 235 236 } 237 | Popular Tags |