1 7 package org.jboss.test.cache.test.eviction; 8 9 import junit.framework.TestCase; 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.eviction.Region; 14 import org.jboss.cache.eviction.RegionManager; 15 import org.jboss.cache.eviction.LRUAlgorithm; 16 import org.jboss.cache.eviction.EvictionException; 17 18 import java.util.HashMap ; 19 20 23 public class LRUAlgorithmUnitTestCase extends TestCase 24 { 25 RegionManager regionManager_; 26 LRUAlgorithm algo_; 27 28 public LRUAlgorithmUnitTestCase(String s) 29 { 30 super(s); 31 } 32 33 public void setUp() throws Exception 34 { 35 super.setUp(); 36 algo_ = new LRUAlgorithm(); 37 DummyEvictionPolicy policy = new DummyEvictionPolicy(); 38 regionManager_ = new RegionManager(policy); 39 regionManager_.createRegion("/a/b", algo_); 40 47 } 48 49 public void tearDown() throws Exception 50 { 51 super.tearDown(); 52 } 53 54 57 public void XtestEvictException() { 58 Fqn fqn1 = Fqn.fromString("/a/b/c"); 59 Fqn fqn2 = Fqn.fromString("/a/b/d"); 60 Fqn fqn3 = Fqn.fromString("/a/b/e"); 61 Region region = regionManager_.getRegion("/a/b"); 62 region.setMaxNodes(1); 63 region.setAddedNode(fqn1); 64 region.setAddedNode(fqn2); 65 66 try { 67 algo_.process(region); 68 } catch (EvictionException e) { 69 fail("testMaxNode: process failed " +e); 70 e.printStackTrace(); 71 } 72 assertEquals("Queue size should be ", 1, algo_.evictionQueueSize()); 73 74 region.setAddedNode(fqn2); 75 region.setAddedNode(fqn3); 76 77 try { 78 algo_.process(region); 79 } catch (EvictionException e) { 80 fail("testMaxNode: process failed " +e); 81 e.printStackTrace(); 82 } 83 assertEquals("Queue size should be ", 1, algo_.evictionQueueSize()); 84 } 85 86 87 90 public void testMaxNode1() { 91 Fqn fqn1 = Fqn.fromString("/a/b/c"); 92 Fqn fqn2 = Fqn.fromString("/a/b/d"); 93 Fqn fqn3 = Fqn.fromString("/a/b/e"); 94 Region region = regionManager_.getRegion("/a/b"); 95 region.setMaxNodes(0); 96 region.setAddedNode(fqn1); 97 region.setAddedNode(fqn2); 98 99 try { 100 algo_.process(region); 101 } catch (EvictionException e) { 102 fail("testMaxNode: process failed " +e); 103 e.printStackTrace(); 104 } 105 assertEquals("Queue size should be ", 2, algo_.evictionQueueSize()); 106 107 } 108 109 112 public void testMaxNode2() { 113 Fqn fqn1 = Fqn.fromString("/a/b/c"); 114 Fqn fqn2 = Fqn.fromString("/a/b/d"); 115 Fqn fqn3 = Fqn.fromString("/a/b/e"); 116 Region region = regionManager_.getRegion("/a/b"); 117 region.setMaxNodes(1); 118 region.setAddedNode(fqn1); 119 region.setAddedNode(fqn2); 120 121 try { 122 algo_.process(region); 123 } catch (EvictionException e) { 124 fail("testMaxNode: process failed " +e); 125 e.printStackTrace(); 126 } 127 assertEquals("Queue size should be ", 1, algo_.evictionQueueSize()); 128 129 region.setAddedNode(fqn2); 130 region.setAddedNode(fqn3); 131 132 try { 133 algo_.process(region); 134 } catch (EvictionException e) { 135 fail("testMaxNode: process failed " +e); 136 e.printStackTrace(); 137 } 138 assertEquals("Queue size should be ", 1, algo_.evictionQueueSize()); 139 } 140 141 144 public void testIdleTimeSeconds1() { 145 Fqn fqn1 = Fqn.fromString("/a/b/c"); 146 Fqn fqn2 = Fqn.fromString("/a/b/d"); 147 Fqn fqn3 = Fqn.fromString("/a/b/e"); 148 Region region = regionManager_.getRegion("/a/b"); 149 region.setMaxNodes(0); 150 region.setTimeToLiveSeconds(0); 151 region.setAddedNode(fqn1); 152 region.setAddedNode(fqn2); 153 _sleep(2000); 154 try { 155 algo_.process(region); 156 } catch (EvictionException e) { 157 fail("testMaxNode: process failed " +e); 158 e.printStackTrace(); 159 } 160 assertEquals("Queue size should be ", 2, algo_.evictionQueueSize()); 161 162 } 163 164 167 public void testIdleTimeSeconds2() { 168 Fqn fqn1 = Fqn.fromString("/a/b/c"); 169 Fqn fqn2 = Fqn.fromString("/a/b/d"); 170 Fqn fqn3 = Fqn.fromString("/a/b/e"); 171 Region region = regionManager_.getRegion("/a/b"); 172 region.setMaxNodes(0); 173 region.setTimeToLiveSeconds(1); 174 region.setAddedNode(fqn1); 175 region.setAddedNode(fqn2); 176 region.setAddedNode(fqn3); 177 try { 178 algo_.process(region); 179 } catch (EvictionException e) { 180 fail("testMaxNode: process failed " +e); 181 e.printStackTrace(); 182 } 183 assertEquals("Queue size #1: ", 3, algo_.evictionQueueSize()); 184 _sleep(2000); 185 try { 186 algo_.process(region); 187 } catch (EvictionException e) { 188 fail("testMaxNode: process failed " +e); 189 e.printStackTrace(); 190 } 191 assertEquals("Queue size #2: ", 0, algo_.evictionQueueSize()); 192 } 193 194 197 public void testIdleTimeSeconds3() { 198 Fqn fqn1 = Fqn.fromString("/a/b/c"); 199 Fqn fqn2 = Fqn.fromString("/a/b/d"); 200 Fqn fqn3 = Fqn.fromString("/a/b/e"); 201 Region region = regionManager_.getRegion("/a/b"); 202 region.setMaxNodes(0); 203 region.setTimeToLiveSeconds(1); 204 region.setAddedNode(fqn1); 205 region.setAddedNode(fqn2); 206 region.setAddedNode(fqn3); 207 try { 208 algo_.process(region); 209 } catch (EvictionException e) { 210 fail("testMaxNode: process failed " +e); 211 e.printStackTrace(); 212 } 213 assertEquals("Queue size #1: ", 3, algo_.evictionQueueSize()); 214 _sleep(2000); 215 region.setVisitedNode(fqn2); 216 try { 217 algo_.process(region); 218 } catch (EvictionException e) { 219 fail("testMaxNode: process failed " +e); 220 e.printStackTrace(); 221 } 222 assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize()); 223 } 224 225 226 229 public void testCombo1() { 230 Fqn fqn1 = Fqn.fromString("/a/b/c"); 231 Fqn fqn2 = Fqn.fromString("/a/b/d"); 232 Fqn fqn3 = Fqn.fromString("/a/b/e"); 233 Region region = regionManager_.getRegion("/a/b"); 234 region.setMaxNodes(2); 235 region.setTimeToLiveSeconds(1); 236 region.setAddedNode(fqn1); 237 region.setAddedNode(fqn2); 238 try { 239 algo_.process(region); 240 } catch (EvictionException e) { 241 fail("testMaxNode: process failed " +e); 242 e.printStackTrace(); 243 } 244 assertEquals("Queue size #1: ", 2, algo_.evictionQueueSize()); 245 region.setAddedNode(fqn3); 246 _sleep(2000); 247 try { 248 algo_.process(region); 249 } catch (EvictionException e) { 250 fail("testMaxNode: process failed " +e); 251 e.printStackTrace(); 252 } 253 assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize()); 254 } 255 256 259 public void testCombo2() { 260 Fqn fqn1 = Fqn.fromString("/a/b/c"); 261 Fqn fqn2 = Fqn.fromString("/a/b/d"); 262 Fqn fqn3 = Fqn.fromString("/a/b/e"); 263 Region region = regionManager_.getRegion("/a/b"); 264 region.setMaxNodes(2); 265 region.setTimeToLiveSeconds(1); 266 region.setAddedNode(fqn1); 267 region.setAddedNode(fqn2); 268 region.setRemovedNode(fqn2); 269 try { 270 algo_.process(region); 271 } catch (EvictionException e) { 272 fail("testMaxNode: process failed " +e); 273 e.printStackTrace(); 274 } 275 assertEquals("Queue size #1: ", 1, algo_.evictionQueueSize()); 276 region.setAddedNode(fqn3); 277 _sleep(2000); 278 try { 279 algo_.process(region); 280 } catch (EvictionException e) { 281 fail("testMaxNode: process failed " +e); 282 e.printStackTrace(); 283 } 284 assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize()); 285 } 286 287 void _sleep(long msecs) { 288 try { 289 Thread.sleep(msecs); 290 } catch (InterruptedException e) { 291 e.printStackTrace(); } 293 } 294 295 void log(String msg) 296 { 297 System.out.println("-- " + msg); 298 } 299 300 public static Test suite() 301 { 302 return new TestSuite(LRUAlgorithmUnitTestCase.class); 303 } 304 305 public static void main(String [] args) 306 { 307 junit.textui.TestRunner.run(suite()); 308 } 309 310 } 311 | Popular Tags |