private QuorumPeer waitForQuorumPeer(MainThread mainThread, int timeout) throws TimeoutException { long start = Time.currentElapsedTime(); while (true) { QuorumPeer quorumPeer = mainThread.isAlive() ? mainThread.getQuorumPeer() : null; if (quorumPeer != null) { return quorumPeer; } if (Time.currentElapsedTime() > start + timeout) { LOG.error("Timed out while waiting for QuorumPeer"); throw new TimeoutException(); } try { Thread.sleep(250); } catch (InterruptedException e) { // ignore } } }
/** * Ensure that observer only comes up when a proper ensemble is configured. * (and will not come up with standalone server). */ @Test public void testObserverWithStandlone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer\n" + "server.2=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + "\npeerType=observer\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); q1.join(ClientBase.CONNECTION_TIMEOUT); Assert.assertFalse(q1.isAlive()); }
/** * This test ensures that an Observer does not elect itself as a leader, or * indeed come up properly, if it is the lone member of an ensemble. * @throws Exception */ @Test public void testObserverOnly() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer;" + CLIENT_PORT_QP1 + "\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); q1.join(ClientBase.CONNECTION_TIMEOUT); Assert.assertFalse(q1.isAlive()); }
public void restartAllServersAndClients(Watcher watcher) throws IOException, InterruptedException { for (MainThread t : mt) { if (!t.isAlive()) { t.start(); } } for (int i = 0; i < zk.length; i++) { restartClient(i, watcher); } }