use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method commit.
private List<OpResult> commit(Transaction txn, boolean useAsync) throws KeeperException, InterruptedException {
if (useAsync) {
final MultiResult res = new MultiResult();
txn.commit(new MultiCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> opResults) {
synchronized (res) {
res.rc = rc;
res.results = opResults;
res.finished = true;
res.notifyAll();
}
}
}, null);
synchronized (res) {
while (!res.finished) {
res.wait();
}
}
if (KeeperException.Code.OK.intValue() != res.rc) {
KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
throw ke;
}
return res.results;
} else {
return txn.commit();
}
}
use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method testMultiRead.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiRead(boolean useAsync) throws Exception {
zk.create("/node1", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/node2", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
zk.create("/node1/node1", "data11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/node1/node2", "data12".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
List<OpResult> multiRead = multi(zk, Arrays.asList(Op.getChildren("/node1"), Op.getData("/node1"), Op.getChildren("/node2"), Op.getData("/node2")), useAsync);
assertEquals(multiRead.size(), 4);
assertTrue(multiRead.get(0) instanceof OpResult.GetChildrenResult);
List<String> childrenList = ((OpResult.GetChildrenResult) multiRead.get(0)).getChildren();
assertEquals(childrenList.size(), 2);
assertEquals(new TreeSet<String>(childrenList), new TreeSet<String>(Arrays.asList("node1", "node2")));
assertArrayEquals(((OpResult.GetDataResult) multiRead.get(1)).getData(), "data1".getBytes());
Stat stat = ((OpResult.GetDataResult) multiRead.get(1)).getStat();
assertEquals(stat.getMzxid(), stat.getCzxid());
assertEquals(stat.getCtime(), stat.getMtime());
assertEquals(2, stat.getCversion());
assertEquals(0, stat.getVersion());
assertEquals(0, stat.getAversion());
assertEquals(0, stat.getEphemeralOwner());
assertEquals(5, stat.getDataLength());
assertEquals(2, stat.getNumChildren());
assertTrue(multiRead.get(2) instanceof OpResult.GetChildrenResult);
childrenList = ((OpResult.GetChildrenResult) multiRead.get(2)).getChildren();
assertTrue(childrenList.isEmpty());
assertArrayEquals(((OpResult.GetDataResult) multiRead.get(3)).getData(), "data2".getBytes());
stat = ((OpResult.GetDataResult) multiRead.get(3)).getStat();
assertEquals(stat.getMzxid(), stat.getCzxid());
assertEquals(stat.getMzxid(), stat.getPzxid());
assertEquals(stat.getCtime(), stat.getMtime());
assertEquals(0, stat.getCversion());
assertEquals(0, stat.getVersion());
assertEquals(0, stat.getAversion());
assertEquals(zk.getSessionId(), stat.getEphemeralOwner());
assertEquals(5, stat.getDataLength());
assertEquals(0, stat.getNumChildren());
}
use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method testMultiGetChildrenAuthentication.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiGetChildrenAuthentication(boolean useAsync) throws KeeperException, InterruptedException {
List<ACL> writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone")));
zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT);
// Check for normal behaviour.
List<OpResult> multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth")), useAsync);
assertEquals(multiChildrenList.size(), 1);
assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult);
List<String> childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren();
assertEquals(childrenList.size(), 1);
assertEquals(childrenList.get(0), "bar");
// Check for authentication violation.
multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_no_auth")), useAsync);
assertEquals(multiChildrenList.size(), 1);
assertTrue(multiChildrenList.get(0) instanceof OpResult.ErrorResult);
assertEquals(((OpResult.ErrorResult) multiChildrenList.get(0)).getErr(), KeeperException.Code.NOAUTH.intValue(), "Expected NoAuthException for getting the children of a write only node");
}
use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method testMultiGetChildrenMixedAuthenticationCorrectFirst.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testMultiGetChildrenMixedAuthenticationCorrectFirst(boolean useAsync) throws KeeperException, InterruptedException {
List<ACL> writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone")));
zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT);
// Check for getting the children of the nodes with mixed authentication.
// The getChildren operation returns GetChildrenResult if it happened before the error.
List<OpResult> multiChildrenList;
multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth"), Op.getChildren("/foo_no_auth")), useAsync);
assertSame(multiChildrenList.size(), 2);
assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult);
List<String> childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren();
assertEquals(childrenList.size(), 1);
assertEquals(childrenList.get(0), "bar");
assertTrue(multiChildrenList.get(1) instanceof OpResult.ErrorResult);
assertEquals(((OpResult.ErrorResult) multiChildrenList.get(1)).getErr(), KeeperException.Code.NOAUTH.intValue(), "Expected NoAuthException for getting the children of a write only node");
}
use of org.apache.zookeeper.OpResult in project zookeeper by apache.
the class MultiOperationTest method multiHavingErrors.
private void multiHavingErrors(ZooKeeper zk, Iterable<Op> ops, List<Integer> expectedResultCodes, String expectedErr, boolean useAsync) throws KeeperException, InterruptedException {
if (useAsync) {
final MultiResult res = new MultiResult();
zk.multi(ops, new MultiCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> opResults) {
synchronized (res) {
res.rc = rc;
res.results = opResults;
res.finished = true;
res.notifyAll();
}
}
}, null);
synchronized (res) {
while (!res.finished) {
res.wait();
}
}
for (int i = 0; i < res.results.size(); i++) {
OpResult opResult = res.results.get(i);
assertTrue(opResult instanceof ErrorResult, "Did't receive proper error response");
ErrorResult errRes = (ErrorResult) opResult;
assertEquals(expectedResultCodes.get(i).intValue(), errRes.getErr(), "Did't receive proper error code");
}
} else {
try {
zk.multi(ops);
fail("Shouldn't have validated in ZooKeeper client!");
} catch (KeeperException e) {
assertEquals(expectedErr, e.code().name(), "Wrong exception");
} catch (IllegalArgumentException e) {
assertEquals(expectedErr, e.getMessage(), "Wrong exception");
}
}
}
Aggregations