@Test
  public void testCnxManagerTimeout() throws Exception {
    Random rand = new Random();
    byte b = (byte) rand.nextInt();
    int deadPort = PortAssignment.unique();
    String deadAddress = new String("10.1.1." + b);

    LOG.info("This is the dead address I'm trying: " + deadAddress);

    peers.put(
        Long.valueOf(2),
        new QuorumServer(
            2,
            new InetSocketAddress(deadAddress, deadPort),
            new InetSocketAddress(deadAddress, PortAssignment.unique())));
    peerTmpdir[2] = ClientBase.createTmpDir();

    QuorumPeer peer =
        new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
      listener.start();
    } else {
      LOG.error("Null listener when initializing cnx manager");
    }

    long begin = System.currentTimeMillis();
    cnxManager.toSend(new Long(2), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    long end = System.currentTimeMillis();

    if ((end - begin) > 6000) fail("Waited more than necessary");
  }
  @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 = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if (listener != null) {
      listener.start();
    } else {
      LOG.error("Null listener when initializing cnx manager");
    }

    cnxManager.toSend(new Long(0), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));

    Message m = null;
    int numRetries = 1;
    while ((m == null) && (numRetries++ <= THRESHOLD)) {
      m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
      if (m == null) cnxManager.connectAll();
    }

    assertTrue("Exceeded number of retries", numRetries <= THRESHOLD);

    thread.join(5000);
    if (thread.isAlive()) {
      fail("Thread didn't join");
    } else {
      if (thread.failed) fail("Did not receive expected message");
    }
  }
    public void run() {
      try {
        QuorumPeer peer =
            new QuorumPeer(
                peers, peerTmpdir[0], peerTmpdir[0], peerClientPort[0], 3, 0, 1000, 2, 2);
        QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
        QuorumCnxManager.Listener listener = cnxManager.listener;
        if (listener != null) {
          listener.start();
        } else {
          LOG.error("Null listener when initializing cnx manager");
        }

        long sid = 1;
        cnxManager.toSend(sid, createMsg(ServerState.LOOKING.ordinal(), 0, -1, 1));

        Message m = null;
        int numRetries = 1;
        while ((m == null) && (numRetries++ <= THRESHOLD)) {
          m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
          if (m == null) cnxManager.connectAll();
        }

        if (numRetries > THRESHOLD) {
          failed = true;
          return;
        }

        cnxManager.testInitiateConnection(sid);

        m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
        if (m == null) {
          failed = true;
          return;
        }
      } catch (Exception e) {
        LOG.error("Exception while running mock thread", e);
        fail("Unexpected exception");
      }
    }
 /**
  * Called by run() once there is a new message to send.
  *
  * @param m message to send
  */
 void process(ToSend m) {
   ByteBuffer requestBuffer =
       buildMsg(m.state.ordinal(), m.leader, m.zxid, m.electionEpoch, m.peerEpoch);
   manager.toSend(m.sid, requestBuffer);
 }