1 7 package org.jboss.cache.buddyreplication; 8 9 import junit.framework.TestCase; 10 import org.jgroups.stack.IpAddress; 11 12 import java.net.InetAddress ; 13 import java.net.NetworkInterface ; 14 import java.net.Inet6Address ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.Enumeration ; 18 import java.util.Map ; 19 import java.util.HashMap ; 20 21 26 public class NextMemberBuddyLocatorTest extends TestCase 27 { 28 private IpAddress dataOwner; 29 private List buddies_localhost = new ArrayList (); 30 private List buddies_same_host_different_nic = new ArrayList (); 31 private List buddies_different_hosts = new ArrayList (); 32 33 { 34 try 35 { 36 dataOwner = new IpAddress(InetAddress.getByName("localhost"), 1000); 37 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 2000)); 38 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 3000)); 39 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 4000)); 40 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 5000)); 41 42 Enumeration en= NetworkInterface.getNetworkInterfaces(); 44 while(en.hasMoreElements()) 45 { 46 NetworkInterface i=(NetworkInterface )en.nextElement(); 47 for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) 48 { 49 InetAddress addr=(InetAddress )en2.nextElement(); 50 if(addr.isLoopbackAddress() || addr instanceof Inet6Address ) continue; 51 buddies_same_host_different_nic.add(new IpAddress(addr, 1000)); 52 } 53 } 54 55 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.amazon.com"), 1000)); 57 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.cnn.com"), 1000)); 58 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.google.com"), 1000)); 59 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.microsoft.com"), 1000)); 60 } 61 catch (Exception e) 62 { 63 e.printStackTrace(); 64 } 65 } 66 67 68 private List getBuddies(int numBuddies, boolean ignoreColoc, List candidates) 69 { 70 return getBuddies(numBuddies, ignoreColoc, candidates, null); 71 } 72 73 private List getBuddies(int numBuddies, boolean ignoreColoc, List candidates, Map buddyPool) 74 { 75 NextMemberBuddyLocatorConfig cfg = new NextMemberBuddyLocatorConfig(); 76 cfg.setIgnoreColocatedBuddies(ignoreColoc); 77 cfg.setNumBuddies(numBuddies); 78 NextMemberBuddyLocator nmbl = new NextMemberBuddyLocator(); 79 nmbl.init(cfg); 80 return nmbl.locateBuddies(buddyPool, candidates, dataOwner); 81 } 82 83 84 86 public void testSingleBuddyNoColoc() 87 { 88 List list = new ArrayList (); 89 list.add(dataOwner); 90 list.add(buddies_localhost.get(0)); 91 list.add(buddies_localhost.get(1)); 92 List results = getBuddies(1, false, list); 93 94 assertEquals(1, results.size()); 95 assertEquals(buddies_localhost.get(0), results.get(0)); 96 } 97 98 public void testThreeBuddiesNoColoc() 99 { 100 List list = new ArrayList (); 101 list.add(dataOwner); 102 list.add(buddies_localhost.get(0)); 103 list.add(buddies_localhost.get(1)); 104 list.add(buddies_localhost.get(2)); 105 list.add(buddies_localhost.get(3)); 106 107 List results = getBuddies(3, false, list); 108 109 assertEquals(3, results.size()); 110 assertEquals(buddies_localhost.get(0), results.get(0)); 111 assertEquals(buddies_localhost.get(1), results.get(1)); 112 assertEquals(buddies_localhost.get(2), results.get(2)); 113 } 114 115 public void testMoreBuddiesThanAvblNoColoc() 116 { 117 List list = new ArrayList (); 118 list.add(dataOwner); 119 list.add(buddies_localhost.get(0)); 120 list.add(buddies_localhost.get(1)); 121 122 List results = getBuddies(3, false, list); 123 124 assertEquals(2, results.size()); 125 assertEquals(buddies_localhost.get(0), results.get(0)); 126 assertEquals(buddies_localhost.get(1), results.get(1)); 127 } 128 129 public void testSingleBuddyWithColocAllCandidatesColoc() 131 { 132 List list = new ArrayList (); 133 list.add(dataOwner); 134 list.add(buddies_localhost.get(0)); 135 list.add(buddies_localhost.get(1)); 136 List results = getBuddies(1, true, list); 137 138 assertEquals(1, results.size()); 139 assertEquals(buddies_localhost.get(0), results.get(0)); 140 } 141 142 public void testThreeBuddiesWithColocAllCandidatesColoc() 143 { 144 List list = new ArrayList (); 145 list.add(dataOwner); 146 list.add(buddies_localhost.get(0)); 147 list.add(buddies_localhost.get(1)); 148 list.add(buddies_localhost.get(2)); 149 list.add(buddies_localhost.get(3)); 150 151 List results = getBuddies(3, true, list); 152 153 assertEquals(3, results.size()); 154 assertEquals(buddies_localhost.get(0), results.get(0)); 155 assertEquals(buddies_localhost.get(1), results.get(1)); 156 assertEquals(buddies_localhost.get(2), results.get(2)); 157 } 158 159 public void testMoreBuddiesThanAvblWithColocAllCandidatesColoc() 160 { 161 List list = new ArrayList (); 162 list.add(dataOwner); 163 list.add(buddies_localhost.get(0)); 164 list.add(buddies_localhost.get(1)); 165 166 List results = getBuddies(3, true, list); 167 168 assertEquals(2, results.size()); 169 assertEquals(buddies_localhost.get(0), results.get(0)); 170 assertEquals(buddies_localhost.get(1), results.get(1)); 171 } 172 173 public void testSingleBuddyWithColocAllCandidatesColocDiffNics() 175 { 176 List list = new ArrayList (); 177 list.add(dataOwner); 178 list.add(buddies_localhost.get(0)); 179 list.add(buddies_localhost.get(1)); 180 list.addAll(buddies_same_host_different_nic); 181 List results = getBuddies(1, true, list); 182 183 assertEquals(1, results.size()); 184 assertEquals(buddies_localhost.get(0), results.get(0)); 185 } 186 187 public void testThreeBuddiesWithColocAllCandidatesColocDiffNics() 188 { 189 List list = new ArrayList (); 190 list.add(dataOwner); 191 list.add(buddies_localhost.get(0)); 192 list.add(buddies_localhost.get(1)); 193 list.add(buddies_localhost.get(2)); 194 list.add(buddies_localhost.get(3)); 195 list.addAll(buddies_same_host_different_nic); 196 197 List results = getBuddies(3, true, list); 198 199 assertEquals(3, results.size()); 200 assertEquals(buddies_localhost.get(0), results.get(0)); 201 assertEquals(buddies_localhost.get(1), results.get(1)); 202 assertEquals(buddies_localhost.get(2), results.get(2)); 203 } 204 205 public void testMoreBuddiesThanAvblWithColocAllCandidatesColocDiffNics() 206 { 207 List list = new ArrayList (); 208 list.add(dataOwner); 209 list.add(buddies_localhost.get(0)); 210 list.add(buddies_localhost.get(1)); 211 list.addAll(buddies_same_host_different_nic); 212 213 List results = getBuddies(3, true, list); 214 215 assertEquals(buddies_same_host_different_nic.isEmpty() ? 2 : 3, results.size()); 216 assertEquals(buddies_localhost.get(0), results.get(0)); 217 assertEquals(buddies_localhost.get(1), results.get(1)); 218 if (!buddies_same_host_different_nic.isEmpty()) assertEquals(buddies_same_host_different_nic.get(0), results.get(2)); 219 } 220 221 public void testSingleBuddyWithColocDiffHosts() 223 { 224 List list = new ArrayList (); 225 list.add(dataOwner); 226 list.add(buddies_localhost.get(0)); 227 list.add(buddies_localhost.get(1)); 228 list.addAll(buddies_same_host_different_nic); 229 list.addAll(buddies_different_hosts); 230 List results = getBuddies(1, true, list); 231 232 assertEquals(1, results.size()); 233 assertEquals(buddies_different_hosts.get(0), results.get(0)); 234 } 235 236 public void testThreeBuddiesWithColocDiffHosts() 237 { 238 List list = new ArrayList (); 239 list.add(dataOwner); 240 list.add(buddies_localhost.get(0)); 241 list.add(buddies_localhost.get(1)); 242 list.add(buddies_localhost.get(2)); 243 list.add(buddies_localhost.get(3)); 244 list.addAll(buddies_same_host_different_nic); 245 list.addAll(buddies_different_hosts); 246 247 List results = getBuddies(3, true, list); 248 249 assertEquals(3, results.size()); 250 assertEquals(buddies_different_hosts.get(0), results.get(0)); 251 assertEquals(buddies_different_hosts.get(1), results.get(1)); 252 assertEquals(buddies_different_hosts.get(2), results.get(2)); 253 } 254 255 public void testMoreBuddiesThanAvblWithColocDiffHosts() 256 { 257 List list = new ArrayList (); 258 list.add(dataOwner); 259 list.add(buddies_localhost.get(0)); 260 list.add(buddies_localhost.get(1)); 261 list.add(buddies_different_hosts.get(0)); 262 list.add(buddies_different_hosts.get(1)); 263 264 265 List results = getBuddies(3, true, list); 266 267 assertEquals(3, results.size()); 268 assertEquals(buddies_different_hosts.get(0), results.get(0)); 269 assertEquals(buddies_different_hosts.get(1), results.get(1)); 270 assertEquals(buddies_localhost.get(0), results.get(2)); 271 } 272 273 public void testSingleLocalBuddyWithPool() 275 { 276 List list = new ArrayList (); 277 list.add( dataOwner ); 278 list.add( buddies_localhost.get(0) ); 279 list.add( buddies_localhost.get(1) ); 280 list.add( buddies_localhost.get(2) ); 281 282 Map pool = new HashMap (); 283 pool.put(dataOwner, "A"); 284 pool.put(buddies_localhost.get(2), "A"); 285 pool.put(buddies_localhost.get(0), "B"); 286 pool.put(buddies_localhost.get(1), "B"); 287 288 List results = getBuddies(1, true, list, pool); 289 290 assertEquals(1, results.size()); 291 assertEquals(buddies_localhost.get(2), results.get(0)); 292 } 293 294 295 public void testSingleLocalBuddyWithPoolMixed1() 297 { 298 List list = new ArrayList (); 299 list.add( dataOwner ); 300 list.add( buddies_localhost.get(0) ); 301 list.add( buddies_localhost.get(1) ); 302 list.add( buddies_localhost.get(2) ); 303 list.add( buddies_different_hosts.get(0) ); 304 list.add( buddies_different_hosts.get(1) ); 305 306 Map pool = new HashMap (); 307 pool.put(dataOwner, "A"); 308 pool.put(buddies_localhost.get(2), "A"); 309 pool.put(buddies_localhost.get(0), "B"); 310 pool.put(buddies_localhost.get(1), "B"); 311 pool.put(buddies_different_hosts.get(0), "C"); 312 pool.put(buddies_different_hosts.get(1), "C"); 313 314 List results = getBuddies(1, true, list, pool); 315 316 assertEquals(1, results.size()); 317 assertEquals(buddies_localhost.get(2), results.get(0)); 318 } 319 320 public void testSingleLocalBuddyWithPoolMixed2() 321 { 322 List list = new ArrayList (); 323 list.add( dataOwner ); 324 list.add( buddies_localhost.get(0) ); 325 list.add( buddies_localhost.get(1) ); 326 list.add( buddies_localhost.get(2) ); 327 list.add( buddies_different_hosts.get(0) ); 328 list.add( buddies_different_hosts.get(1) ); 329 330 Map pool = new HashMap (); 331 pool.put(dataOwner, "A"); 332 pool.put(buddies_localhost.get(2), "B"); 333 pool.put(buddies_localhost.get(0), "B"); 334 pool.put(buddies_localhost.get(1), "B"); 335 pool.put(buddies_different_hosts.get(0), "C"); 336 pool.put(buddies_different_hosts.get(1), "C"); 337 338 List results = getBuddies(1, true, list, pool); 339 340 assertEquals(1, results.size()); 341 assertEquals(buddies_different_hosts.get(0), results.get(0)); 343 } 344 345 public void testWithDataOwnerAtEnd() 346 { 347 List list = new ArrayList (); 348 list.addAll(buddies_localhost); 349 list.add(dataOwner); 350 351 List results = getBuddies(1, true, list); 352 353 assertEquals(1, results.size()); 354 assertEquals(buddies_localhost.get(0), results.get(0)); 355 } 356 357 } 358 | Popular Tags |