Search in sources :

Example 6 with ZooKeeperGroup

use of org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup in project camel by apache.

the class GroupTest method testOrder.

@Test
public void testOrder() throws Exception {
    int port = findFreePort();
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    final String path = "/singletons/test/Order" + System.currentTimeMillis();
    ArrayList<ZooKeeperGroup> members = new ArrayList<ZooKeeperGroup>();
    for (int i = 0; i < 4; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, path, NodeState.class);
        group.add(listener);
        members.add(group);
    }
    for (ZooKeeperGroup group : members) {
        assertFalse(group.isConnected());
        assertFalse(group.isMaster());
    }
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // first to start should be master if members are ordered...
    int i = 0;
    for (ZooKeeperGroup group : members) {
        group.start();
        group.update(new NodeState("foo" + i));
        i++;
        // wait for registration
        while (group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = members.get(0).isMaster();
    for (ZooKeeperGroup group : members) {
        group.close();
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
    assertTrue("first started is master", firsStartedIsMaster);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ArrayList(java.util.ArrayList) ZooKeeperGroup(org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) Test(org.junit.Test)

Aggregations

ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 RetryNTimes (org.apache.curator.retry.RetryNTimes)6 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)1 ChildData (org.apache.camel.component.zookeepermaster.group.internal.ChildData)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1