Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4325: Fix bug when list "/" with ZkUtil::listSubTreeBFS #1729

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public static List<String> listSubTreeBFS(
String node = queue.poll();
List<String> children = zk.getChildren(node, false);
for (final String child : children) {
final String childPath = node + "/" + child;
// Fix IllegalArgumentException when list "/".
final String childPath = (node.equals("/") ? "" : node) + "/" + child;
queue.add(childPath);
tree.add(childPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@

import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ZKUtilTest {
public class ZKUtilTest extends ClientBase {

private static final File testData = new File(System.getProperty("test.data.dir", "build/test/data"));

Expand Down Expand Up @@ -85,4 +90,33 @@ public void testUnreadableFileInput() throws Exception {
assertEquals(expectedMessage, error);
}

@Test
public void testListRootPathSuccess() throws IOException, InterruptedException, KeeperException {
TestableZooKeeper zk = createClient();
zk.setData("/", "some".getBytes(), -1);
zk.create("/a", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/a/b", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

List<String> list = ZKUtil.listSubTreeBFS(zk, "/");
list.remove(Quotas.procZookeeper);
list.remove(Quotas.quotaZookeeper);
list.remove(ZooDefs.CONFIG_NODE);
assertEquals(3, list.size());
assertIterableEquals(Arrays.asList("/", "/a", "/a/b"), list);
}

@Test
public void testListNoneRootPathSuccess() throws IOException, InterruptedException, KeeperException {
TestableZooKeeper zk = createClient();
zk.setData("/", "some".getBytes(), -1);
zk.create("/a", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/a/b", "some".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
List<String> aList = ZKUtil.listSubTreeBFS(zk, "/a");
assertEquals(2, aList.size());
assertIterableEquals(Arrays.asList("/a", "/a/b"), aList);

List<String> bList = ZKUtil.listSubTreeBFS(zk, "/a/b");
assertEquals(1, bList.size());
assertIterableEquals(Collections.singletonList("/a/b"), bList);
}
}