use of org.apache.zookeeper.server.quorum.LeaderElection in project zookeeper by apache.
the class LETest method testLE.
@Test
public void testLE() throws Exception {
int count = 30;
HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>(count);
ArrayList<LEThread> threads = new ArrayList<LEThread>(count);
File[] tmpdir = new File[count];
int[] port = new int[count];
votes = new Vote[count];
for (int i = 0; i < count; i++) {
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
tmpdir[i] = ClientBase.createTmpDir();
port[i] = PortAssignment.unique();
}
LeaderElection[] le = new LeaderElection[count];
leaderDies = true;
boolean allowOneBadLeader = leaderDies;
for (int i = 0; i < le.length; i++) {
QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 0, i, 1000, 2, 2);
peer.startLeaderElection();
le[i] = new LeaderElection(peer);
LEThread thread = new LEThread(le[i], peer, i);
thread.start();
threads.add(thread);
}
for (int i = 0; i < threads.size(); i++) {
threads.get(i).join(15000);
if (threads.get(i).isAlive()) {
Assert.fail("Threads didn't join");
}
}
long id = votes[0].getId();
for (int i = 1; i < votes.length; i++) {
if (votes[i] == null) {
Assert.fail("Thread " + i + " had a null vote");
}
if (votes[i].getId() != id) {
if (allowOneBadLeader && votes[i].getId() == i) {
allowOneBadLeader = false;
} else {
Assert.fail("Thread " + i + " got " + votes[i].getId() + " expected " + id);
}
}
}
}
Aggregations