org.apache.zookeeper.AsyncCallback.Children2Callback Java Examples

The following examples show how to use org.apache.zookeeper.AsyncCallback.Children2Callback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testGetLogSegmentsZKExceptions() throws Exception {
    String logName = testName.getMethodName();
    String logIdentifier = "<default>";

    ZooKeeper mockZk = mock(ZooKeeper.class);
    ZooKeeperClient mockZkc = mock(ZooKeeperClient.class);
    when(mockZkc.get()).thenReturn(mockZk);
    doAnswer(invocationOnMock -> {
        String path = (String) invocationOnMock.getArguments()[0];
        Children2Callback callback = (Children2Callback) invocationOnMock.getArguments()[2];
        callback.processResult(Code.BADVERSION.intValue(), path, null, null, null);
        return null;
    }).when(mockZk).getChildren(anyString(), anyBoolean(), any(Children2Callback.class), anyObject());

    String logSegmentsPath = LogMetadata.getLogSegmentsPath(uri, logName, logIdentifier);
    try {
        FutureUtils.result(getLogSegments(mockZkc, logSegmentsPath));
        fail("Should fail to get log segments when encountering zk exceptions");
    } catch (ZKException zke) {
        assertEquals(Code.BADVERSION, zke.getKeeperExceptionCode());
    }
}
 
Example #2
Source File: MockZooKeeper.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public void getChildren(final String path, boolean watcher, final Children2Callback cb, final Object ctx) {
    executor.execute(() -> {
        mutex.lock();

        Optional<KeeperException.Code> failure = programmedFailure(Op.GET_CHILDREN, path);
        if (failure.isPresent()) {
            mutex.unlock();
            cb.processResult(failure.get().intValue(), path, ctx, null, null);
            return;
        } else if (stopped) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null, null);
            return;
        } else if (!tree.containsKey(path)) {
            mutex.unlock();
            cb.processResult(KeeperException.Code.NoNode, path, ctx, null, null);
            return;
        }

        log.debug("getChildren path={}", path);
        List<String> children = Lists.newArrayList();
        for (String item : tree.tailMap(path).keySet()) {
            log.debug("Checking path {}", item);
            if (!item.startsWith(path)) {
                break;
            } else if (item.equals(path)) {
                continue;
            } else {
                String child = item.substring(path.length());
                if (child.indexOf("/") == 0) {
                    child = child.substring(1);
                    log.debug("child: '{}'", child);
                    if (!child.contains("/")) {
                        children.add(child);
                    }
                }
            }
        }

        log.debug("getChildren done path={} result={}", path, children);
        mutex.unlock();
        cb.processResult(0, path, ctx, children, new Stat());
    });

}