1 7 package org.jboss.cache.eviction; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.factories.XmlConfigurationParser; 12 import org.jboss.cache.lock.IsolationLevel; 13 import org.jboss.cache.misc.TestingUtil; 14 15 21 public class MRUPolicyTest 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 void setUp() throws Exception 31 { 32 super.setUp(); 33 System.out.println("Test " + getName() + "\n"); 34 initCaches(); 35 wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000; 36 log("wakeupInterval is " + wakeupIntervalMillis); 37 if (wakeupIntervalMillis < 0) 38 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis); 39 40 t1_ex = t2_ex = null; 41 isTrue = true; 42 } 43 44 public void tearDown() throws Exception 45 { 46 super.tearDown(); 47 cache.stop(); 48 } 49 50 private void initCaches() throws Exception 51 { 52 cache = new CacheImpl(); 53 XmlConfigurationParser parser = new XmlConfigurationParser(); 54 cache.setConfiguration(parser.parseFile("META-INF/local-mru-eviction-service.xml")); cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 56 cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 57 cache.start(); 58 } 59 60 public void testEviction() throws Exception 61 { 62 cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a"); 63 cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b"); 64 cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c"); 65 cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d"); 66 cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e"); 67 68 TestingUtil.sleepThread(wakeupIntervalMillis + 500); 69 70 System.out.println(cache.toString(true)); 71 72 cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f"); 73 cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g"); 74 cache.put("/org/jboss/test/data/h", "/org/jboss/test/data/h", "/org/jboss/test/data/h"); 75 assertNotNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a")); 76 assertNotNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b")); 77 78 TestingUtil.sleepThread(wakeupIntervalMillis + 500); 79 80 System.out.println(cache.toString(true)); 81 82 assertNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a")); 83 assertNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b")); 84 } 85 86 public void testNodeRemoved() throws Exception 87 { 88 cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a"); 89 cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b"); 90 cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c"); 91 cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d"); 92 cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e"); 93 94 TestingUtil.sleepThread(wakeupIntervalMillis + 500); 95 96 cache.remove("/org/jboss/test/data/d"); 97 cache.remove("/org/jboss/test/data/e"); 98 cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f"); 99 cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g"); 100 101 TestingUtil.sleepThread(wakeupIntervalMillis + 500); 102 103 assertNull(cache.get("/org/jboss/test/data/d", "/org/jboss/test/data/d")); 104 assertNull(cache.get("/org/jboss/test/data/e", "/org/jboss/test/data/e")); 105 106 assertNotNull(cache.get("/org/jboss/test/data/f", "/org/jboss/test/data/f")); 107 assertNotNull(cache.get("/org/jboss/test/data/g", "/org/jboss/test/data/g")); 108 } 109 110 class MyPutter extends Thread 111 { 112 113 public MyPutter(String name) 114 { 115 super(name); 116 } 117 118 public void run() 119 { 120 int i = 0; 121 final String myName = ROOT_STR + "/test1/node" + getName(); 122 while (isTrue) 123 { 124 try 125 { 126 cache.put(myName + i++, "value", i); 127 TestingUtil.sleepThread(1); 128 } 129 catch (Throwable e) 130 { 131 e.printStackTrace(); 132 if (t1_ex == null) 133 t1_ex = e; 134 } 135 } 136 } 137 } 138 139 140 public void testConcurrentPutAndEvict() throws Exception 141 { 142 cache.stop(); 143 cache.destroy(); 144 cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ); 145 cache.start(); 146 cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1); 147 148 for (int i = 0; i < 10; i++) 149 { 150 new MyPutter("Putter" + i).start(); 151 } 152 153 int counter = 0; 154 while (true) 155 { 156 counter++; 157 if (t1_ex != null) 158 { 159 fail("Exception generated in put() " + t1_ex); 160 } 161 log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld()); 162 TestingUtil.sleepThread(1000); 163 if (counter > 10) 164 { isTrue = false; 166 break; 167 } 168 } 169 } 170 171 private void log(String s) 172 { 173 System.out.println(s); 174 } 175 } 176 | Popular Tags |