use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testDumpEphemerals.
/**
* For ZOOKEEPER-1755 - Test race condition when taking dumpEphemerals and
* removing the session related ephemerals from DataTree structure
*/
@Test
@Timeout(value = 60)
public void testDumpEphemerals() throws Exception {
int count = 1000;
long session = 1000;
long zxid = 2000;
final DataTree dataTree = new DataTree();
LOG.info("Create {} zkclient sessions and its ephemeral nodes", count);
createEphemeralNode(session, dataTree, count);
final AtomicBoolean exceptionDuringDumpEphemerals = new AtomicBoolean(false);
final AtomicBoolean running = new AtomicBoolean(true);
Thread thread = new Thread() {
public void run() {
PrintWriter pwriter = new PrintWriter(new StringWriter());
try {
while (running.get()) {
dataTree.dumpEphemerals(pwriter);
}
} catch (Exception e) {
LOG.error("Received exception while dumpEphemerals!", e);
exceptionDuringDumpEphemerals.set(true);
}
}
};
thread.start();
LOG.debug("Killing {} zkclient sessions and its ephemeral nodes", count);
killZkClientSession(session, zxid, dataTree, count);
running.set(false);
thread.join();
assertFalse(exceptionDuringDumpEphemerals.get(), "Should have got exception while dumpEphemerals!");
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testDeserializeDoesntLockACLCacheWhileReading.
/* ZOOKEEPER-3531 - similarly for aclCache.deserialize, we should not hold lock either
*/
@Test
@Timeout(value = 60)
public void testDeserializeDoesntLockACLCacheWhileReading() throws Exception {
DataTree tree = new DataTree();
tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
final AtomicBoolean ranTestCase = new AtomicBoolean();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
BinaryOutputArchive oa = new BinaryOutputArchive(out);
tree.serialize(oa, "test");
DataTree tree2 = new DataTree();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
BinaryInputArchive ia = new BinaryInputArchive(in) {
@Override
public long readLong(String tag) throws IOException {
final Semaphore semaphore = new Semaphore(0);
new Thread(new Runnable() {
@Override
public void run() {
synchronized (tree2.getReferenceCountedAclCache()) {
// When we lock ACLCache, allow readLong to continue
semaphore.release();
}
}
}).start();
try {
boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
// This is the real assertion - could another thread lock
// the ACLCache
assertTrue(acquired, "Couldn't acquire a lock on the ACLCache while we were calling tree.deserialize");
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
ranTestCase.set(true);
return super.readLong(tag);
}
};
tree2.deserialize(ia, "test");
// Let's make sure that we hit the code that ran the real assertion above
assertTrue(ranTestCase.get(), "Didn't find the expected node");
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testReconfigACLClearOnDeserialize.
@Test
@Timeout(value = 60)
public void testReconfigACLClearOnDeserialize() throws Exception {
DataTree tree = new DataTree();
// simulate the upgrading scenario, where the reconfig znode
// doesn't exist and the acl cache is empty
tree.deleteNode(ZooDefs.CONFIG_NODE, 1);
tree.getReferenceCountedAclCache().aclIndex = 0;
assertEquals(0, tree.aclCacheSize(), "expected to have 1 acl in acl cache map");
// serialize the data with one znode with acl
tree.createNode("/bug", new byte[20], ZooDefs.Ids.OPEN_ACL_UNSAFE, -1, 1, 1, 1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
tree.serialize(oa, "test");
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BinaryInputArchive ia = BinaryInputArchive.getArchive(bais);
tree.deserialize(ia, "test");
assertEquals(1, tree.aclCacheSize(), "expected to have 1 acl in acl cache map");
assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat()), "expected to have the same acl");
// simulate the upgrading case where the config node will be created
// again after leader election
tree.addConfigNode();
assertEquals(2, tree.aclCacheSize(), "expected to have 2 acl in acl cache map");
assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat()), "expected to have the same acl");
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class CreateContainerTest method testCreateWithNullStat.
@SuppressWarnings("ConstantConditions")
@Test
@Timeout(value = 30)
public void testCreateWithNullStat() throws KeeperException, InterruptedException {
final String name = "/foo";
assertNull(zk.exists(name, false));
Stat stat = null;
// If a null Stat object is passed the create should still
// succeed, but no Stat info will be returned.
zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, stat);
assertNull(stat);
assertNotNull(zk.exists(name, false));
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class CreateContainerTest method testMultiWithContainerSimple.
@Test
@Timeout(value = 30)
public void testMultiWithContainerSimple() throws KeeperException, InterruptedException {
Op createContainer = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
zk.multi(Collections.singletonList(createContainer));
DataTree dataTree = serverFactory.getZooKeeperServer().getZKDatabase().getDataTree();
assertEquals(dataTree.getContainers().size(), 1);
}
Aggregations