/**
   * Tests the behavior of Zookeeper upon a restart. ZK should clean up old coordinates.
   *
   * @throws Exception
   */
  @Test
  public void testZookeeperRestarts() throws Exception {
    final CountDownLatch connectedLatch1 = new CountDownLatch(1);
    final CountDownLatch connectedLatch2 = new CountDownLatch(3);
    TestCoordinateListener listener = setUpListenerEnvironment(connectedLatch1, connectedLatch2);
    assertTrue(connectedLatch1.await(20, TimeUnit.SECONDS));
    log.info("Killing zookeeper");
    forwarder.terminate();

    ezk.shutdown();
    ezk.del();
    ezk.init();
    Thread.sleep(2000);
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);

    int timeoutSecs = 30;
    while (--timeoutSecs > 0) {
      Thread.sleep(1000);
    }
    Coordinate c = Coordinate.parse("1.service.user.cell");
    cn.createCoordinate(c);

    Thread.sleep(9000);
    assertEquals(
        listener.events.get(listener.events.size() - 1), CoordinateListener.Event.COORDINATE_OK);
  }
  private TestCoordinateListener setUpListenerEnvironment(
      CountDownLatch connectedLatch1, CountDownLatch connectedLatch2) throws Exception {
    forwarderPort = Net.getFreePort();
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);
    final Coordinate c = Coordinate.parse("1.service.user.cell");

    cn = makeLocalZkCloudname(forwarderPort);
    try {
      cn.createCoordinate(c);
    } catch (CoordinateException e) {
      fail(e.toString());
    }
    final TestCoordinateListener listener =
        new TestCoordinateListener(connectedLatch1, connectedLatch2);
    ServiceHandle serviceHandle = cn.claim(c);
    serviceHandle.registerCoordinateListener(listener);

    return listener;
  }