/** Test that the replication worker will shutdown if it lose its zookeeper session */
  @Test(timeout = 30000)
  public void testRWZKSessionLost() throws Exception {
    ZooKeeper zk =
        ZooKeeperClient.createConnectedZooKeeper(zkUtil.getZooKeeperConnectString(), 10000);

    try {
      ReplicationWorker rw = new ReplicationWorker(zk, baseConf, getBookie(0));
      rw.start();
      for (int i = 0; i < 10; i++) {
        if (rw.isRunning()) {
          break;
        }
        Thread.sleep(1000);
      }
      assertTrue("Replication worker should be running", rw.isRunning());
      stopZKCluster();

      for (int i = 0; i < 10; i++) {
        if (!rw.isRunning()) {
          break;
        }
        Thread.sleep(1000);
      }
      assertFalse("Replication worker should have shut down", rw.isRunning());
    } finally {
      zk.close();
    }
  }
  /**
   * Test that if the local bookie turns out to be readonly, then no point in running RW. So RW
   * should shutdown.
   */
  @Test(timeout = 20000)
  public void testRWShutdownOnLocalBookieReadonlyTransition() throws Exception {
    LedgerHandle lh = bkc.createLedger(3, 3, BookKeeper.DigestType.CRC32, TESTPASSWD);

    for (int i = 0; i < 10; i++) {
      lh.addEntry(data);
    }
    BookieSocketAddress replicaToKill =
        LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);

    LOG.info("Killing Bookie", replicaToKill);
    killBookie(replicaToKill);

    int newBkPort = startNewBookie();
    for (int i = 0; i < 10; i++) {
      lh.addEntry(data);
    }

    BookieSocketAddress newBkAddr =
        new BookieSocketAddress(InetAddress.getLocalHost().getHostAddress(), newBkPort);
    LOG.info("New Bookie addr :" + newBkAddr);

    ReplicationWorker rw = new ReplicationWorker(zkc, baseConf, newBkAddr);

    rw.start();
    try {
      BookieServer newBk = bs.get(bs.size() - 1);
      bsConfs.get(bsConfs.size() - 1).setReadOnlyModeEnabled(true);
      newBk.getBookie().doTransitionToReadOnlyMode();
      underReplicationManager.markLedgerUnderreplicated(lh.getId(), replicaToKill.toString());
      while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)
          && rw.isRunning()) {
        Thread.sleep(100);
      }
      assertFalse("RW should shutdown if the bookie is readonly", rw.isRunning());
    } finally {
      rw.shutdown();
    }
  }