use of org.apache.zookeeper.server.quorum.QuorumCnxManager.Message in project zookeeper by apache.
the class CnxManagerTest method testCnxManager.
@Test
public void testCnxManager() throws Exception {
CnxManagerThread thread = new CnxManagerThread();
thread.start();
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
QuorumCnxManager cnxManager = peer.createCnxnManager();
QuorumCnxManager.Listener listener = cnxManager.listener;
if (listener != null) {
listener.start();
} else {
LOG.error("Null listener when initializing cnx manager");
}
cnxManager.toSend(0L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
Message m = null;
int numRetries = 1;
while ((m == null) && (numRetries++ <= THRESHOLD)) {
m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS);
if (m == null)
cnxManager.connectAll();
}
Assert.assertTrue("Exceeded number of retries", numRetries <= THRESHOLD);
thread.join(5000);
if (thread.isAlive()) {
Assert.fail("Thread didn't join");
} else {
if (thread.failed)
Assert.fail("Did not receive expected message");
}
cnxManager.halt();
Assert.assertFalse(cnxManager.listener.isAlive());
}
use of org.apache.zookeeper.server.quorum.QuorumCnxManager.Message in project zookeeper by apache.
the class CnxManagerTest method testCnxManagerNPE.
/**
* Tests a bug in QuorumCnxManager that causes a NPE when a 3.4.6
* observer connects to a 3.5.0 server.
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1789}
*
* @throws Exception
*/
@Test
public void testCnxManagerNPE() throws Exception {
// the connecting peer (id = 2) is a 3.4.6 observer
peers.get(2L).type = LearnerType.OBSERVER;
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
QuorumCnxManager cnxManager = peer.createCnxnManager();
QuorumCnxManager.Listener listener = cnxManager.listener;
if (listener != null) {
listener.start();
} else {
LOG.error("Null listener when initializing cnx manager");
}
int port = peers.get(peer.getId()).electionAddr.getPort();
LOG.info("Election port: " + port);
Thread.sleep(1000);
SocketChannel sc = SocketChannel.open();
sc.socket().connect(peers.get(1L).electionAddr, 5000);
/*
* Write id (3.4.6 protocol). This previously caused a NPE in
* QuorumCnxManager.
*/
byte[] msgBytes = new byte[8];
ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes);
msgBuffer.putLong(2L);
msgBuffer.position(0);
sc.write(msgBuffer);
msgBuffer = ByteBuffer.wrap(new byte[8]);
// write length of message
msgBuffer.putInt(4);
// write message
msgBuffer.putInt(5);
msgBuffer.position(0);
sc.write(msgBuffer);
Message m = cnxManager.pollRecvQueue(1000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(m);
peer.shutdown();
cnxManager.halt();
Assert.assertFalse(cnxManager.listener.isAlive());
}
use of org.apache.zookeeper.server.quorum.QuorumCnxManager.Message in project zookeeper by apache.
the class CnxManagerTest method testCnxManagerNPE.
/**
* Tests a bug in QuorumCnxManager that causes a NPE when a 3.4.6
* observer connects to a 3.5.0 server.
* see https://issues.apache.org/jira/browse/ZOOKEEPER-1789
*
* @throws Exception
*/
@Test
public void testCnxManagerNPE() throws Exception {
// the connecting peer (id = 2) is a 3.4.6 observer
peers.get(2L).type = LearnerType.OBSERVER;
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2);
QuorumCnxManager cnxManager = peer.createCnxnManager();
QuorumCnxManager.Listener listener = cnxManager.listener;
if (listener != null) {
listener.start();
} else {
LOG.error("Null listener when initializing cnx manager");
}
InetSocketAddress address = peers.get(peer.getId()).electionAddr.getReachableOrOne();
LOG.info("Election port: {}", address.getPort());
Thread.sleep(1000);
SocketChannel sc = SocketChannel.open();
sc.socket().connect(address, 5000);
/*
* Write id (3.4.6 protocol). This previously caused a NPE in
* QuorumCnxManager.
*/
byte[] msgBytes = new byte[8];
ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes);
msgBuffer.putLong(2L);
msgBuffer.position(0);
sc.write(msgBuffer);
msgBuffer = ByteBuffer.wrap(new byte[8]);
// write length of message
msgBuffer.putInt(4);
// write message
msgBuffer.putInt(5);
msgBuffer.position(0);
sc.write(msgBuffer);
Message m = cnxManager.pollRecvQueue(1000, TimeUnit.MILLISECONDS);
assertNotNull(m);
peer.shutdown();
cnxManager.halt();
assertFalse(cnxManager.listener.isAlive());
}
use of org.apache.zookeeper.server.quorum.QuorumCnxManager.Message in project zookeeper by apache.
the class CnxManagerTest method testCnxManager.
@Test
public void testCnxManager() throws Exception {
CnxManagerThread thread = new CnxManagerThread();
thread.start();
QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2);
QuorumCnxManager cnxManager = peer.createCnxnManager();
QuorumCnxManager.Listener listener = cnxManager.listener;
if (listener != null) {
listener.start();
} else {
LOG.error("Null listener when initializing cnx manager");
}
cnxManager.toSend(0L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
Message m = null;
int numRetries = 1;
while ((m == null) && (numRetries++ <= THRESHOLD)) {
m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS);
if (m == null) {
cnxManager.connectAll();
}
}
assertTrue(numRetries <= THRESHOLD, "Exceeded number of retries");
thread.join(5000);
if (thread.isAlive()) {
fail("Thread didn't join");
} else {
if (thread.failed) {
fail("Did not receive expected message");
}
}
cnxManager.halt();
assertFalse(cnxManager.listener.isAlive());
}
Aggregations