Example #1
0
  @Test
  public void testZkClientWhenZKIsDownAndRestarts() throws Exception {

    // Iterate updating the timestamp and check the final value
    long previousMaxTimestamp = INITIAL_MAX_TS_VALUE;
    for (int i = 0; i < ITERATION_COUNT; i++) {
      long newMaxTimestamp = previousMaxTimestamp + 1_000_000;
      storage.updateMaxTimestamp(previousMaxTimestamp, newMaxTimestamp);
      previousMaxTimestamp = newMaxTimestamp;
    }
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);

    // Stop ZK Server, expect the IO exception, reconnect and get the right value
    LOG.info("Stopping ZK Server");
    zkServer.stop();
    LOG.info("ZK Server Stopped");

    try {
      storage.getMaxTimestamp();
      fail();
    } catch (IOException ioe) {
      LOG.info("Expected exception", ioe);
    }

    LOG.info("Restarting ZK again");
    zkServer.restart();
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);
  }