Search in sources :

Example 1 with StaticHostProvider

use of org.apache.zookeeper.client.StaticHostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testNoCurrentHostDuringNormalMode.

@Test
public void testNoCurrentHostDuringNormalMode() throws UnknownHostException {
    // Start with 9 servers and 10000 clients
    boolean disconnectRequired;
    StaticHostProvider[] hostProviderArray = new StaticHostProvider[numClients];
    InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients];
    int[] numClientsPerHost = new int[9];
    // initialization
    for (int i = 0; i < numClients; i++) {
        hostProviderArray[i] = getHostProvider((byte) 9);
        if (i >= (numClients / 2)) {
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        } else {
            // its supposed to be the first server on serverList.
            // we'll set it later, see below (*)
            curHostForEachClient[i] = null;
        }
    }
    // remove hosts 7 and 8 (the last two in a list of 9 hosts)
    Collection<InetSocketAddress> newList = getServerAddresses((byte) 7);
    for (int i = 0; i < numClients; i++) {
        // tests the case currentHost == null && lastIndex == -1
        // calls next for clients with index < numClients/2
        disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        else if (curHostForEachClient[i] == null) {
            // (*) setting it to what it should be
            curHostForEachClient[i] = hostProviderArray[i].getServerAtIndex(0);
        }
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        // sets lastIndex, resets reconfigMode
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 7; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 7));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 7));
        // prepare for next test
        numClientsPerHost[i] = 0;
    }
    assertTrue(numClientsPerHost[7] == 0);
    assertTrue(numClientsPerHost[8] == 0);
    // add back server 7
    newList = getServerAddresses((byte) 8);
    for (int i = 0; i < numClients; i++) {
        InetSocketAddress myServer = (i < (numClients / 2)) ? null : curHostForEachClient[i];
        // tests the case currentHost == null && lastIndex >= 0
        disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 8; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8));
    }
}
Also used : StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 2 with StaticHostProvider

use of org.apache.zookeeper.client.StaticHostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testReconfigDuringReconfigMode.

@Test
public void testReconfigDuringReconfigMode() throws UnknownHostException {
    // Start with 9 servers and 10000 clients
    boolean disconnectRequired;
    StaticHostProvider[] hostProviderArray = new StaticHostProvider[numClients];
    InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients];
    int[] numClientsPerHost = new int[9];
    // initialization
    for (int i = 0; i < numClients; i++) {
        hostProviderArray[i] = getHostProvider((byte) 9);
        curHostForEachClient[i] = hostProviderArray[i].next(0);
    }
    // remove hosts 7 and 8 (the last two in a list of 9 hosts)
    Collection<InetSocketAddress> newList = getServerAddresses((byte) 7);
    for (int i = 0; i < numClients; i++) {
        // sets reconfigMode
        hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]);
    }
    // add back servers 7 and 8 while still in reconfigMode (we didn't call
    // next)
    newList = getServerAddresses((byte) 9);
    for (int i = 0; i < numClients; i++) {
        InetSocketAddress myServer = (i < (numClients / 2)) ? null : curHostForEachClient[i];
        // for i < (numClients/2) this tests the case currentHost == null &&
        // reconfigMode = true
        // for i >= (numClients/2) this tests the case currentHost!=null &&
        // reconfigMode = true
        disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer);
        if (disconnectRequired)
            curHostForEachClient[i] = hostProviderArray[i].next(0);
        else {
            // currentIndex was set by the call to updateServerList, which
            // called next
            curHostForEachClient[i] = hostProviderArray[i].getServerAtCurrentIndex();
        }
        numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++;
        hostProviderArray[i].onConnected();
    }
    for (int i = 0; i < 9; i++) {
        assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9));
        assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9));
    }
}
Also used : StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 3 with StaticHostProvider

use of org.apache.zookeeper.client.StaticHostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testTwoInvalidHostAddresses.

@Test(expected = IllegalArgumentException.class)
public void testTwoInvalidHostAddresses() {
    ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
    list.add(new InetSocketAddress("a...", 2181));
    list.add(new InetSocketAddress("b...", 2181));
    new StaticHostProvider(list);
}
Also used : StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with StaticHostProvider

use of org.apache.zookeeper.client.StaticHostProvider in project zookeeper by apache.

the class StaticHostProviderTest method testOneInvalidHostAddresses.

@Test
public void testOneInvalidHostAddresses() {
    Collection<InetSocketAddress> addr = getServerAddresses((byte) 1);
    addr.add(new InetSocketAddress("a...", 2181));
    StaticHostProvider sp = new StaticHostProvider(addr);
    InetSocketAddress n1 = sp.next(0);
    InetSocketAddress n2 = sp.next(0);
    assertEquals(n2, n1);
}
Also used : StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)4 StaticHostProvider (org.apache.zookeeper.client.StaticHostProvider)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)1