Search in sources :

Example 91 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class NIOServerCnxnTest method testOperationsAfterCnxnClose.

/**
 * Test operations on ServerCnxn after socket closure.
 */
@Test
@Timeout(value = 60)
public void testOperationsAfterCnxnClose() throws IOException, InterruptedException, KeeperException {
    final ZooKeeper zk = createClient();
    final String path = "/a";
    try {
        // make sure zkclient works
        zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        assertNotNull(zk.exists(path, false), "Didn't create znode:" + path);
        // Defaults ServerCnxnFactory would be instantiated with
        // NIOServerCnxnFactory
        assertTrue(serverFactory instanceof NIOServerCnxnFactory, "Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!");
        Iterable<ServerCnxn> connections = serverFactory.getConnections();
        for (ServerCnxn serverCnxn : connections) {
            serverCnxn.close(ServerCnxn.DisconnectReason.CHANNEL_CLOSED_EXCEPTION);
            try {
                serverCnxn.toString();
            } catch (Exception e) {
                LOG.error("Exception while getting connection details!", e);
                fail("Shouldn't throw exception while " + "getting connection details!");
            }
        }
    } finally {
        zk.close();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 92 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class NettyServerCnxnTest method testMaxConnectionPerIpSurpased.

/**
 * In the {@link #setUp()} routine, the maximum number of connections per IP
 * is set to 1. This tests that if more than one connection is attempted, the
 * connection fails.
 */
@Test
@Timeout(value = 40)
public void testMaxConnectionPerIpSurpased() {
    assertTrue(serverFactory instanceof NettyServerCnxnFactory, "Did not instantiate ServerCnxnFactory with NettyServerCnxnFactory!");
    assertThrows(ProtocolException.class, () -> {
        try (final ZooKeeper zk1 = createClient();
            final ZooKeeper zk2 = createClient()) {
        }
    });
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) SSLAuthTest(org.apache.zookeeper.test.SSLAuthTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 93 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class DataTreeTest method testSerializeDoesntLockDataNodeWhileWriting.

/*
     * ZOOKEEPER-2201 - OutputArchive.writeRecord can block for long periods of
     * time, we must call it outside of the node lock.
     * We call tree.serialize, which calls our modified writeRecord method that
     * blocks until it can verify that a separate thread can lock the DataNode
     * currently being written, i.e. that DataTree.serializeNode does not hold
     * the DataNode lock while calling OutputArchive.writeRecord.
     */
@Test
@Timeout(value = 60)
public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception {
    DataTree tree = new DataTree();
    tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
    final DataNode markerNode = tree.getNode("/marker");
    final AtomicBoolean ranTestCase = new AtomicBoolean();
    DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream());
    BinaryOutputArchive oa = new BinaryOutputArchive(out) {

        @Override
        public void writeRecord(Record r, String tag) throws IOException {
            // which adds default ACL to config node.
            if (r instanceof DataNode) {
                DataNode node = (DataNode) r;
                if (node.data.length == 1 && node.data[0] == 42) {
                    final Semaphore semaphore = new Semaphore(0);
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            synchronized (markerNode) {
                                // When we lock markerNode, allow writeRecord to continue
                                semaphore.release();
                            }
                        }
                    }).start();
                    try {
                        boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
                        // This is the real assertion - could another thread lock
                        // the DataNode we're currently writing
                        assertTrue(acquired, "Couldn't acquire a lock on the DataNode while we were calling tree.serialize");
                    } catch (InterruptedException e1) {
                        throw new RuntimeException(e1);
                    }
                    ranTestCase.set(true);
                }
            }
            super.writeRecord(r, tag);
        }
    };
    tree.serialize(oa, "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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Record(org.apache.jute.Record) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 94 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class DataTreeTest method testRootWatchTriggered.

@Test
@Timeout(value = 60)
public void testRootWatchTriggered() throws Exception {
    DataTree dt = new DataTree();
    CompletableFuture<Void> fire = new CompletableFuture<>();
    // set a watch on the root node
    dt.getChildren("/", new Stat(), event -> {
        if (event.getPath().equals("/")) {
            fire.complete(null);
        }
    });
    // add a new node, should trigger a watch
    dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1);
    assertTrue(fire.isDone(), "Root node watch not triggered");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 95 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class DataTreeTest method testPathTrieClearOnDeserialize.

@Test
@Timeout(value = 60)
public void testPathTrieClearOnDeserialize() throws Exception {
    // Create a DataTree with quota nodes so PathTrie get updated
    DataTree dserTree = new DataTree();
    dserTree.createNode("/bug", new byte[20], null, -1, 1, 1, 1);
    dserTree.createNode(Quotas.quotaPath("/bug"), null, null, -1, 1, 1, 1);
    dserTree.createNode(Quotas.limitPath("/bug"), new byte[20], null, -1, 1, 1, 1);
    dserTree.createNode(Quotas.statPath("/bug"), new byte[20], null, -1, 1, 1, 1);
    // deserialize a DataTree; this should clear the old /bug nodes and pathTrie
    DataTree tree = new DataTree();
    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);
    dserTree.deserialize(ia, "test");
    Field pfield = DataTree.class.getDeclaredField("pTrie");
    pfield.setAccessible(true);
    PathTrie pTrie = (PathTrie) pfield.get(dserTree);
    // Check that the node path is removed from pTrie
    assertEquals("/", pTrie.findMaxPrefix("/bug"), "/bug is still in pTrie");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) Field(java.lang.reflect.Field) PathTrie(org.apache.zookeeper.common.PathTrie) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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