1 7 package org.jboss.cache.eviction; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.Node; 13 import org.jboss.cache.NodeSPI; 14 import org.jboss.cache.factories.XmlConfigurationParser; 15 import org.jboss.cache.lock.IsolationLevel; 16 17 21 public class ElementSizePolicyTest extends TestCase 22 { 23 CacheImpl cache; 24 int wakeupIntervalMillis = 0; 25 final String ROOT_STR = "/test"; 26 Throwable t1_ex, t2_ex; 27 final long DURATION = 10000; 28 boolean isTrue; 29 30 public ElementSizePolicyTest(String s) 31 { 32 super(s); 33 } 34 35 public void setUp() throws Exception 36 { 37 super.setUp(); 38 initCaches(); 39 wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000; 40 log("wakeupInterval is " + wakeupIntervalMillis); 41 if (wakeupIntervalMillis < 0) 42 { 43 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis); 44 } 45 46 t1_ex = t2_ex = null; 47 isTrue = true; 48 } 49 50 void initCaches() throws Exception 51 { 52 cache = new CacheImpl(); 53 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-elementsize-eviction-service.xml")); cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 55 cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 56 cache.start(); 57 } 58 59 public void tearDown() throws Exception 60 { 61 super.tearDown(); 62 cache.stop(); 63 } 64 65 public void testEviction() throws Exception 66 { 67 String rootStr = "/org/jboss/test/data/"; 68 for (int i = 0; i < 10; i++) 69 { 70 String str = rootStr + i; 71 Fqn fqn = Fqn.fromString(str); 72 try 73 { 74 cache.put(fqn, str, str); 75 if (i % 2 == 0) 76 { 77 for (int k = 0; k < i; k++) 78 { 79 cache.put(fqn, new Integer (k), Integer.toString(k)); 80 } 81 } 82 } 83 catch (Exception e) 84 { 85 fail("Failed to insert data" + e); 86 e.printStackTrace(); 87 } 88 } 89 90 System.out.println(cache.toString(true)); 91 _sleep(wakeupIntervalMillis + 500); 92 System.out.println(cache.toString(true)); 93 94 for (int i = 0; i < 10; i++) 95 { 96 Node node = cache.get("/org/jboss/test/data/" + Integer.toString(i)); 97 System.out.println(node); 98 if (i % 2 == 0) 99 { 100 if (i < 6) 101 { 102 int numElements = ((NodeSPI) node).getDataDirect().size(); 103 assertEquals(i + 1, numElements); 104 } 105 else 106 { 107 assertNull(node); 108 } 109 } 110 else 111 { 112 assertEquals(1, ((NodeSPI) node).getDataDirect().size()); 113 } 114 } 115 } 116 117 public void testEviction2() throws Exception 118 { 119 String rootStr = "/org/jboss/data/"; 120 for (int i = 0; i < 20; i++) 121 { 122 String str = rootStr + Integer.toString(i); 123 Fqn fqn = Fqn.fromString(str); 124 cache.put(fqn, new Integer (i), str); 125 for (int k = 0; k < i; k++) 126 { 127 cache.put(fqn, new Integer (k), str); 128 } 129 } 130 131 System.out.println(cache.toString(true)); 132 _sleep(wakeupIntervalMillis + 500); 133 System.out.println(cache.toString(true)); 134 135 System.out.println("*******"); 136 System.out.println(cache.printLockInfo()); 137 138 for (int i = 0; i < 20; i++) 139 { 140 String str = rootStr + Integer.toString(i); 141 Fqn fqn = Fqn.fromString(str); 142 Node node = cache.get(fqn); 143 System.out.println(i + " " + node); 144 if (i > 9) 145 { 146 assertNull("Testing at " + i, node); 147 } 148 else 149 { 150 assertEquals(1 + i, node.getData().size()); 151 } 152 } 153 154 for (int i = 0; i < 17; i++) 155 { 156 cache.put("/org/jboss/data/" + Integer.toString(3), 100 + i, "value"); 157 } 158 159 Node node = cache.get("/org/jboss/data/" + Integer.toString(3)); 160 assertEquals(21, node.getData().size()); 161 _sleep(wakeupIntervalMillis + 500); 162 163 assertNull(cache.get("/org/jboss/data/" + Integer.toString(3))); 164 } 165 166 class MyPutter extends Thread 167 { 168 public MyPutter(String name) 169 { 170 super(name); 171 } 172 173 public void run() 174 { 175 int i = 0; 176 final String myName = ROOT_STR + "/test1/node" + getName(); 177 while (isTrue) 178 { 179 try 180 { 181 cache.put(myName + i++, "value", i); 182 sleep(1); 183 } 184 catch (Throwable e) 185 { 186 e.printStackTrace(); 187 if (t1_ex == null) 188 { 189 t1_ex = e; 190 } 191 } 192 } 193 } 194 } 195 196 197 public void testConcurrentPutAndEvict() throws Exception 198 { 199 cache.stop(); 200 cache.destroy(); 201 cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ); 202 203 cache.start(); 204 cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1); 205 206 for (int i = 0; i < 10; i++) 207 { 208 new MyPutter("Putter" + i).start(); 209 } 210 211 int counter = 0; 212 while (true) 213 { 214 counter++; 215 if (t1_ex != null) 216 { 217 fail("Exception generated in put() " + t1_ex); 218 } 219 log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld()); 220 _sleep(1000); 221 if (counter > 10) 222 { isTrue = false; 224 break; 225 } 226 } 227 } 228 229 void _sleep(long msecs) 230 { 231 try 232 { 233 Thread.sleep(msecs); 234 } 235 catch (InterruptedException e) 236 { 237 e.printStackTrace(); } 239 } 240 241 void log(String msg) 242 { 243 System.out.println("-- " + msg); 244 } 245 246 } 247 | Popular Tags |