ZooKeeperClient { public synchronized ZooKeeper get() throws ZooKeeperConnectionException, InterruptedException { try { return get(WAIT_FOREVER); } catch (TimeoutException e) { InterruptedException interruptedException = new InterruptedException("Got an unexpected TimeoutException for 0 wait"); interruptedException.initCause(e); throw interruptedException; } } ZooKeeperClient(Amount<Integer, Time> sessionTimeout, InetSocketAddress zooKeeperServer,
      InetSocketAddress... zooKeeperServers);  ZooKeeperClient(Amount<Integer, Time> sessionTimeout,
      Iterable<InetSocketAddress> zooKeeperServers);  ZooKeeperClient(Amount<Integer, Time> sessionTimeout, Credentials credentials,
      InetSocketAddress zooKeeperServer, InetSocketAddress... zooKeeperServers);  ZooKeeperClient(Amount<Integer, Time> sessionTimeout, Credentials credentials,
      Iterable<InetSocketAddress> zooKeeperServers);  ZooKeeperClient(Amount<Integer, Time> sessionTimeout, Optional<Credentials> credentials,
      Optional<String> chrootPath, Iterable<InetSocketAddress> zooKeeperServers); synchronized ZooKeeper get(); synchronized ZooKeeper get(Amount<Long, Time> connectionTimeout); Watcher registerExpirationHandler(final Command onExpired); void register(Watcher watcher); boolean unregister(Watcher watcher); boolean shouldRetry(KeeperException e); synchronized void close();  }
@Test public void testGet() throws Exception { final ZooKeeperClient zkClient = createZkClient(); shutdownNetwork(); try { zkClient.get(Amount.of(50L, Time.MILLISECONDS)); fail("Expected client connection to timeout while network down"); } catch (TimeoutException e) { assertTrue(zkClient.isClosed()); } assertNull(zkClient.getZooKeeperClientForTests()); final CountDownLatch blockingGetComplete = new CountDownLatch(1); final AtomicReference<ZooKeeper> client = new AtomicReference<ZooKeeper>(); new Thread(() -> { try { client.set(zkClient.get()); } catch (ZooKeeperConnectionException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { blockingGetComplete.countDown(); } }).start(); restartNetwork(); blockingGetComplete.await(); assertNotNull(client.get()); long sessionId = zkClient.get().getSessionId(); assertSame(client.get(), zkClient.get()); shutdownNetwork(); ZooKeeper zooKeeper = zkClient.get(); try { zooKeeper.exists("/", false); fail("Expected client operation to fail while network down"); } catch (ConnectionLossException e) { } restartNetwork(); assertEquals("Expected connection to be re-established with existing session", sessionId, zkClient.get().getSessionId()); }
@Test public void testCredentials() throws Exception { String path = "/test"; ZooKeeperClient authenticatedClient = createZkClient("creator", "creator"); assertEquals(path, authenticatedClient.get().create(path, "42".getBytes(), ZooKeeperUtils.EVERYONE_READ_CREATOR_ALL, CreateMode.PERSISTENT)); ZooKeeperClient unauthenticatedClient = createZkClient(); assertEquals("42", getData(unauthenticatedClient, path)); try { setData(unauthenticatedClient, path, "37"); fail("Expected unauthenticated write attempt to fail"); } catch (NoAuthException e) { assertEquals("42", getData(unauthenticatedClient, path)); } ZooKeeperClient nonOwnerClient = createZkClient("nonowner", "nonowner"); assertEquals("42", getData(nonOwnerClient, path)); try { setData(nonOwnerClient, path, "37"); fail("Expected non owner write attempt to fail"); } catch (NoAuthException e) { assertEquals("42", getData(nonOwnerClient, path)); } ZooKeeperClient authenticatedClient2 = createZkClient("creator", "creator"); setData(authenticatedClient2, path, "37"); assertEquals("37", getData(authenticatedClient2, path)); }
@Test public void testChrootPath() throws Exception { ZooKeeperClient rootClient = createZkClient(); String rootPath = "/test"; String subPath = "/test/subtest"; assertEquals(rootPath, rootClient.get().create(rootPath, "42".getBytes(), ZooKeeperUtils.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); assertEquals(subPath, rootClient.get().create(subPath, "37".getBytes(), ZooKeeperUtils.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); ZooKeeperClient chrootedClient = createZkClient(rootPath); assertArrayEquals("37".getBytes(), chrootedClient.get().getData("/subtest", false, null)); }