Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 357   Methods: 18
NCLOC: 276   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NextMemberBuddyLocatorTest.java 80% 99.5% 100% 98.7%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 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    /**
 22    * Tests the NextMemberBuddyLocator
 23    *
 24    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 25    */
 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  16 try
 35    {
 36  16 dataOwner = new IpAddress(InetAddress.getByName("localhost"), 1000);
 37  16 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 2000));
 38  16 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 3000));
 39  16 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 4000));
 40  16 buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 5000));
 41   
 42    // lets get a few more interfaces from the current host
 43  16 Enumeration en= NetworkInterface.getNetworkInterfaces();
 44  16 while(en.hasMoreElements())
 45    {
 46  32 NetworkInterface i=(NetworkInterface)en.nextElement();
 47  32 for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();)
 48    {
 49  32 InetAddress addr=(InetAddress)en2.nextElement();
 50  16 if(addr.isLoopbackAddress() || addr instanceof Inet6Address) continue;
 51  16 buddies_same_host_different_nic.add(new IpAddress(addr, 1000));
 52    }
 53    }
 54   
 55    // now lets get some which are definitely on different hosts
 56  16 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.amazon.com"), 1000));
 57  16 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.cnn.com"), 1000));
 58  16 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.google.com"), 1000));
 59  16 buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.microsoft.com"), 1000));
 60    }
 61    catch (Exception e)
 62    {
 63  0 e.printStackTrace();
 64    }
 65    }
 66   
 67   
 68  13 private List getBuddies(int numBuddies, boolean ignoreColoc, List candidates)
 69    {
 70  13 return getBuddies(numBuddies, ignoreColoc, candidates, null);
 71    }
 72   
 73  16 private List getBuddies(int numBuddies, boolean ignoreColoc, List candidates, Map buddyPool)
 74    {
 75  16 NextMemberBuddyLocatorConfig cfg = new NextMemberBuddyLocatorConfig();
 76  16 cfg.setIgnoreColocatedBuddies(ignoreColoc);
 77  16 cfg.setNumBuddies(numBuddies);
 78  16 NextMemberBuddyLocator nmbl = new NextMemberBuddyLocator();
 79  16 nmbl.init(cfg);
 80  16 return nmbl.locateBuddies(buddyPool, candidates, dataOwner);
 81    }
 82   
 83   
 84    // without colocation
 85   
 86  1 public void testSingleBuddyNoColoc()
 87    {
 88  1 List list = new ArrayList();
 89  1 list.add(dataOwner);
 90  1 list.add(buddies_localhost.get(0));
 91  1 list.add(buddies_localhost.get(1));
 92  1 List results = getBuddies(1, false, list);
 93   
 94  1 assertEquals(1, results.size());
 95  1 assertEquals(buddies_localhost.get(0), results.get(0));
 96    }
 97   
 98  1 public void testThreeBuddiesNoColoc()
 99    {
 100  1 List list = new ArrayList();
 101  1 list.add(dataOwner);
 102  1 list.add(buddies_localhost.get(0));
 103  1 list.add(buddies_localhost.get(1));
 104  1 list.add(buddies_localhost.get(2));
 105  1 list.add(buddies_localhost.get(3));
 106   
 107  1 List results = getBuddies(3, false, list);
 108   
 109  1 assertEquals(3, results.size());
 110  1 assertEquals(buddies_localhost.get(0), results.get(0));
 111  1 assertEquals(buddies_localhost.get(1), results.get(1));
 112  1 assertEquals(buddies_localhost.get(2), results.get(2));
 113    }
 114   
 115  1 public void testMoreBuddiesThanAvblNoColoc()
 116    {
 117  1 List list = new ArrayList();
 118  1 list.add(dataOwner);
 119  1 list.add(buddies_localhost.get(0));
 120  1 list.add(buddies_localhost.get(1));
 121   
 122  1 List results = getBuddies(3, false, list);
 123   
 124  1 assertEquals(2, results.size());
 125  1 assertEquals(buddies_localhost.get(0), results.get(0));
 126  1 assertEquals(buddies_localhost.get(1), results.get(1));
 127    }
 128   
 129    // with colocation, but all candidates are on the same host
 130  1 public void testSingleBuddyWithColocAllCandidatesColoc()
 131    {
 132  1 List list = new ArrayList();
 133  1 list.add(dataOwner);
 134  1 list.add(buddies_localhost.get(0));
 135  1 list.add(buddies_localhost.get(1));
 136  1 List results = getBuddies(1, true, list);
 137   
 138  1 assertEquals(1, results.size());
 139  1 assertEquals(buddies_localhost.get(0), results.get(0));
 140    }
 141   
 142  1 public void testThreeBuddiesWithColocAllCandidatesColoc()
 143    {
 144  1 List list = new ArrayList();
 145  1 list.add(dataOwner);
 146  1 list.add(buddies_localhost.get(0));
 147  1 list.add(buddies_localhost.get(1));
 148  1 list.add(buddies_localhost.get(2));
 149  1 list.add(buddies_localhost.get(3));
 150   
 151  1 List results = getBuddies(3, true, list);
 152   
 153  1 assertEquals(3, results.size());
 154  1 assertEquals(buddies_localhost.get(0), results.get(0));
 155  1 assertEquals(buddies_localhost.get(1), results.get(1));
 156  1 assertEquals(buddies_localhost.get(2), results.get(2));
 157    }
 158   
 159  1 public void testMoreBuddiesThanAvblWithColocAllCandidatesColoc()
 160    {
 161  1 List list = new ArrayList();
 162  1 list.add(dataOwner);
 163  1 list.add(buddies_localhost.get(0));
 164  1 list.add(buddies_localhost.get(1));
 165   
 166  1 List results = getBuddies(3, true, list);
 167   
 168  1 assertEquals(2, results.size());
 169  1 assertEquals(buddies_localhost.get(0), results.get(0));
 170  1 assertEquals(buddies_localhost.get(1), results.get(1));
 171    }
 172   
 173    // with colocation, all candidates are on the same host but with different NICs
 174  1 public void testSingleBuddyWithColocAllCandidatesColocDiffNics()
 175    {
 176  1 List list = new ArrayList();
 177  1 list.add(dataOwner);
 178  1 list.add(buddies_localhost.get(0));
 179  1 list.add(buddies_localhost.get(1));
 180  1 list.addAll(buddies_same_host_different_nic);
 181  1 List results = getBuddies(1, true, list);
 182   
 183  1 assertEquals(1, results.size());
 184  1 assertEquals(buddies_localhost.get(0), results.get(0));
 185    }
 186   
 187  1 public void testThreeBuddiesWithColocAllCandidatesColocDiffNics()
 188    {
 189  1 List list = new ArrayList();
 190  1 list.add(dataOwner);
 191  1 list.add(buddies_localhost.get(0));
 192  1 list.add(buddies_localhost.get(1));
 193  1 list.add(buddies_localhost.get(2));
 194  1 list.add(buddies_localhost.get(3));
 195  1 list.addAll(buddies_same_host_different_nic);
 196   
 197  1 List results = getBuddies(3, true, list);
 198   
 199  1 assertEquals(3, results.size());
 200  1 assertEquals(buddies_localhost.get(0), results.get(0));
 201  1 assertEquals(buddies_localhost.get(1), results.get(1));
 202  1 assertEquals(buddies_localhost.get(2), results.get(2));
 203    }
 204   
 205  1 public void testMoreBuddiesThanAvblWithColocAllCandidatesColocDiffNics()
 206    {
 207  1 List list = new ArrayList();
 208  1 list.add(dataOwner);
 209  1 list.add(buddies_localhost.get(0));
 210  1 list.add(buddies_localhost.get(1));
 211  1 list.addAll(buddies_same_host_different_nic);
 212   
 213  1 List results = getBuddies(3, true, list);
 214   
 215  1 assertEquals(buddies_same_host_different_nic.isEmpty() ? 2 : 3, results.size());
 216  1 assertEquals(buddies_localhost.get(0), results.get(0));
 217  1 assertEquals(buddies_localhost.get(1), results.get(1));
 218  1 if (!buddies_same_host_different_nic.isEmpty()) assertEquals(buddies_same_host_different_nic.get(0), results.get(2));
 219    }
 220   
 221    // now for some non-colocated buddies to pick from
 222  1 public void testSingleBuddyWithColocDiffHosts()
 223    {
 224  1 List list = new ArrayList();
 225  1 list.add(dataOwner);
 226  1 list.add(buddies_localhost.get(0));
 227  1 list.add(buddies_localhost.get(1));
 228  1 list.addAll(buddies_same_host_different_nic);
 229  1 list.addAll(buddies_different_hosts);
 230  1 List results = getBuddies(1, true, list);
 231   
 232  1 assertEquals(1, results.size());
 233  1 assertEquals(buddies_different_hosts.get(0), results.get(0));
 234    }
 235   
 236  1 public void testThreeBuddiesWithColocDiffHosts()
 237    {
 238  1 List list = new ArrayList();
 239  1 list.add(dataOwner);
 240  1 list.add(buddies_localhost.get(0));
 241  1 list.add(buddies_localhost.get(1));
 242  1 list.add(buddies_localhost.get(2));
 243  1 list.add(buddies_localhost.get(3));
 244  1 list.addAll(buddies_same_host_different_nic);
 245  1 list.addAll(buddies_different_hosts);
 246   
 247  1 List results = getBuddies(3, true, list);
 248   
 249  1 assertEquals(3, results.size());
 250  1 assertEquals(buddies_different_hosts.get(0), results.get(0));
 251  1 assertEquals(buddies_different_hosts.get(1), results.get(1));
 252  1 assertEquals(buddies_different_hosts.get(2), results.get(2));
 253    }
 254   
 255  1 public void testMoreBuddiesThanAvblWithColocDiffHosts()
 256    {
 257  1 List list = new ArrayList();
 258  1 list.add(dataOwner);
 259  1 list.add(buddies_localhost.get(0));
 260  1 list.add(buddies_localhost.get(1));
 261  1 list.add(buddies_different_hosts.get(0));
 262  1 list.add(buddies_different_hosts.get(1));
 263   
 264   
 265  1 List results = getBuddies(3, true, list);
 266   
 267  1 assertEquals(3, results.size());
 268  1 assertEquals(buddies_different_hosts.get(0), results.get(0));
 269  1 assertEquals(buddies_different_hosts.get(1), results.get(1));
 270  1 assertEquals(buddies_localhost.get(0), results.get(2));
 271    }
 272   
 273    // now lets try this with a buddy pool
 274  1 public void testSingleLocalBuddyWithPool()
 275    {
 276  1 List list = new ArrayList();
 277  1 list.add( dataOwner );
 278  1 list.add( buddies_localhost.get(0) );
 279  1 list.add( buddies_localhost.get(1) );
 280  1 list.add( buddies_localhost.get(2) );
 281   
 282  1 Map pool = new HashMap();
 283  1 pool.put(dataOwner, "A");
 284  1 pool.put(buddies_localhost.get(2), "A");
 285  1 pool.put(buddies_localhost.get(0), "B");
 286  1 pool.put(buddies_localhost.get(1), "B");
 287   
 288  1 List results = getBuddies(1, true, list, pool);
 289   
 290  1 assertEquals(1, results.size());
 291  1 assertEquals(buddies_localhost.get(2), results.get(0));
 292    }
 293   
 294   
 295    // now lets try this with a buddy pool
 296  1 public void testSingleLocalBuddyWithPoolMixed1()
 297    {
 298  1 List list = new ArrayList();
 299  1 list.add( dataOwner );
 300  1 list.add( buddies_localhost.get(0) );
 301  1 list.add( buddies_localhost.get(1) );
 302  1 list.add( buddies_localhost.get(2) );
 303  1 list.add( buddies_different_hosts.get(0) );
 304  1 list.add( buddies_different_hosts.get(1) );
 305   
 306  1 Map pool = new HashMap();
 307  1 pool.put(dataOwner, "A");
 308  1 pool.put(buddies_localhost.get(2), "A");
 309  1 pool.put(buddies_localhost.get(0), "B");
 310  1 pool.put(buddies_localhost.get(1), "B");
 311  1 pool.put(buddies_different_hosts.get(0), "C");
 312  1 pool.put(buddies_different_hosts.get(1), "C");
 313   
 314  1 List results = getBuddies(1, true, list, pool);
 315   
 316  1 assertEquals(1, results.size());
 317  1 assertEquals(buddies_localhost.get(2), results.get(0));
 318    }
 319   
 320  1 public void testSingleLocalBuddyWithPoolMixed2()
 321    {
 322  1 List list = new ArrayList();
 323  1 list.add( dataOwner );
 324  1 list.add( buddies_localhost.get(0) );
 325  1 list.add( buddies_localhost.get(1) );
 326  1 list.add( buddies_localhost.get(2) );
 327  1 list.add( buddies_different_hosts.get(0) );
 328  1 list.add( buddies_different_hosts.get(1) );
 329   
 330  1 Map pool = new HashMap();
 331  1 pool.put(dataOwner, "A");
 332  1 pool.put(buddies_localhost.get(2), "B");
 333  1 pool.put(buddies_localhost.get(0), "B");
 334  1 pool.put(buddies_localhost.get(1), "B");
 335  1 pool.put(buddies_different_hosts.get(0), "C");
 336  1 pool.put(buddies_different_hosts.get(1), "C");
 337   
 338  1 List results = getBuddies(1, true, list, pool);
 339   
 340  1 assertEquals(1, results.size());
 341    // preference should be for the non-coloc host
 342  1 assertEquals(buddies_different_hosts.get(0), results.get(0));
 343    }
 344   
 345  1 public void testWithDataOwnerAtEnd()
 346    {
 347  1 List list = new ArrayList();
 348  1 list.addAll(buddies_localhost);
 349  1 list.add(dataOwner);
 350   
 351  1 List results = getBuddies(1, true, list);
 352   
 353  1 assertEquals(1, results.size());
 354  1 assertEquals(buddies_localhost.get(0), results.get(0));
 355    }
 356   
 357    }