@Test
public void testGetRandomIpFromCidrUsignAvoid() throws Exception {

    final String cidr = "192.168.124.1";
    final int size = 30;
    final SortedSet<Long> avoid = new TreeSet<Long>();
    long ip = NetUtils.getRandomIpFromCidr(cidr, size, avoid);
    assertThat("We should be able to retrieve an ip on the first call.", ip, not(equalTo(-1L)));
    avoid.add(ip);
    ip = NetUtils.getRandomIpFromCidr(cidr, size, avoid);
    assertThat("We should be able to retrieve an ip on the second call.", ip, not(equalTo(-1L)));
    assertThat("ip returned is not in the avoid list", avoid, not(contains(ip)));
    avoid.add(ip);
    ip = NetUtils.getRandomIpFromCidr(cidr, size, avoid);
    assertThat("We should be able to retrieve an ip on the third call.", ip, not(equalTo(-1L)));
    assertThat("ip returned is not in the avoid list", avoid, not(contains(ip)));
    avoid.add(ip);
    ip = NetUtils.getRandomIpFromCidr(cidr, size, avoid);
    assertEquals("This should be -1 because we ran out of ip addresses: " + ip, ip, -1);
}