package com.cloudera.flume.master;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.junit.Assert;
import org.junit.Test;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.util.FileUtil;
@Test(expected=IOException.class)
ZooKeeperService svc = new ZooKeeperService();
svc.createClient();
}
@Test
public void testLifecycle()
throws IOException, InterruptedException, KeeperException {
FlumeConfiguration cfg = FlumeConfiguration.get();
File tmp = FileUtil.mktempdir();
cfg.set(FlumeConfiguration.MASTER_ZK_SERVERS, "localhost:2181:3181:4181");
cfg.set(FlumeConfiguration.MASTER_ZK_LOGDIR, tmp.getAbsolutePath());
ZooKeeperService svc = new ZooKeeperService();
svc.init(cfg);
Assert.assertTrue("ZooKeeperService did not initialise", svc.isInitialised());
ZKClient client = svc.createClient();
client.init();
Assert.assertTrue("Expected at least one child of root for ZK service",
client.getChildren("/", false).size() > 0);
client.close();
svc.shutdown();
Assert.assertFalse("ZooKeeperService did not shut down", svc.isInitialised());
FileUtil.rmr(tmp);
}
@Test
FlumeConfiguration cfg = FlumeConfiguration.get();
File tmp = FileUtil.mktempdir();
cfg.set(FlumeConfiguration.MASTER_ZK_SERVERS, "localhost:2181:3181:4181");
cfg.set(FlumeConfiguration.MASTER_ZK_LOGDIR, tmp.getAbsolutePath());
ZooKeeperService svc = new ZooKeeperService();
svc.init(cfg);
Assert.assertTrue("ZooKeeperService did not initialise", svc.isInitialised());
List<ZKClient> clients = new ArrayList<ZKClient>();
for (int i=0;i<15;++i) {
ZKClient client = svc.createClient();
client.init();
clients.add(client);
}
for (ZKClient client : clients) {
client.close();
}
svc.shutdown();
Assert.assertFalse("ZooKeeperService did not shut down", svc.isInitialised());
FileUtil.rmr(tmp);
}
}