Search in sources :

Example 96 with Timeout

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!");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringWriter(java.io.StringWriter) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) PrintWriter(java.io.PrintWriter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 97 with Timeout

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");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) DataInputStream(java.io.DataInputStream) BinaryInputArchive(org.apache.jute.BinaryInputArchive) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 98 with Timeout

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");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) Stat(org.apache.zookeeper.data.Stat) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 99 with Timeout

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));
}
Also used : Stat(org.apache.zookeeper.data.Stat) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 100 with Timeout

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);
}
Also used : Op(org.apache.zookeeper.Op) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Timeout (org.junit.jupiter.api.Timeout)291 Test (org.junit.jupiter.api.Test)235 CountDownLatch (java.util.concurrent.CountDownLatch)71 ZooKeeper (org.apache.zookeeper.ZooKeeper)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 ArrayList (java.util.ArrayList)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 RepeatedTest (org.junit.jupiter.api.RepeatedTest)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 LocalChannel (io.netty.channel.local.LocalChannel)27 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 IOException (java.io.IOException)25 Bootstrap (io.netty.bootstrap.Bootstrap)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 Channel (io.netty.channel.Channel)23 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)19