/** * Asynchronously gets all the ephemeral nodes created by this session. * ephemerals * * @since 3.6.0 * */ public void getEphemerals(AsyncCallback.EphemeralsCallback cb, Object ctx) { getEphemerals("/", cb, ctx); }
/** * Synchronously gets all the ephemeral nodes created by this session. * * @since 3.6.0 * */ public List<String> getEphemerals() throws KeeperException, InterruptedException { return getEphemerals("/"); }
@Override public boolean exec() throws CliException { String path; List<String> ephemerals; try { if (args.length < 2) { // gets all the ephemeral nodes for the session ephemerals = zk.getEphemerals(); } else { path = args[1]; ephemerals = zk.getEphemerals(path); } } catch (IllegalArgumentException ex) { throw new MalformedPathException(ex.getMessage()); } catch (KeeperException|InterruptedException ex) { throw new CliWrapperException(ex); } out.println(ephemerals); return false; } }
@Test public void testGetEphemeralsSync() throws KeeperException, InterruptedException { List<String> actual = zk.getEphemerals(); Assert.assertEquals( "Expected ephemeral count for allPaths", actual.size() , expected.length); for (int i = 0; i < expected.length; i++) { String path = expected[i]; Assert.assertTrue( String.format("Path=%s exists in get All Ephemerals list ", path), actual.contains(path)); } }
@Test public void testGetEphemeralsSyncByPath() throws KeeperException, InterruptedException { final String prefixPath = BASE + 0; List<String> actual = zk.getEphemerals(prefixPath); Assert.assertEquals( "Expected ephemeral count for allPaths", actual.size() , EPHEMERAL_CNT); for (int i = 0; i < EPHEMERAL_CNT; i++) { String path = expected[i]; Assert.assertTrue(String.format("Path=%s exists in getEphemerals(%s) list ", path, prefixPath), actual.contains(path)); } }
@Test public void testGetEphemeralsErrors() throws KeeperException { try { zk.getEphemerals(null, null, null); Assert.fail("Should have thrown a IllegalArgumentException for a null prefixPath"); } catch (IllegalArgumentException e) { //pass } try { zk.getEphemerals("no leading slash", null, null); Assert.fail("Should have thrown a IllegalArgumentException " + "for a prefix with no leading slash"); } catch (IllegalArgumentException e) { //pass } }
@Test public void testGetEphemeralsEmpty() throws IOException, KeeperException, InterruptedException { final CountDownLatch doneProcessing = new CountDownLatch(1); final String checkPath = "/unknownPath"; final int expectedSize = 0; final List<String> unexpectedBehavior = new ArrayList<String>(); zk.getEphemerals(checkPath, new AsyncCallback.EphemeralsCallback() { @Override public void processResult(int rc, Object ctx, List<String> paths) { if (paths == null ) { unexpectedBehavior.add( String.format("Expected ephemeral count for %s to be %d but was null", checkPath, expectedSize)); } else if (paths.size() != expectedSize) { unexpectedBehavior.add( String.format("Expected ephemeral count for %s to be %d but was %d", checkPath, expectedSize, paths.size())); } doneProcessing.countDown(); } }, null); long waitForCallbackSecs = 2l; if (!doneProcessing.await(waitForCallbackSecs, TimeUnit.SECONDS)) { Assert.fail(String.format("getEphemerals(%s) didn't callback within %d seconds", checkPath, waitForCallbackSecs)); } checkForUnexpectedBehavior(unexpectedBehavior); }
zk.getEphemerals(new AsyncCallback.EphemeralsCallback() { @Override public void processResult(int rc, Object ctx, List<String> paths) {
final String checkPath = BASE + "0"; final List<String> unexpectedBehavior = new ArrayList<String>(); zk.getEphemerals(checkPath, new AsyncCallback.EphemeralsCallback() { @Override public void processResult(int rc, Object ctx, List<String> paths) {