Search in sources :

Example 1 with PERSISTENT

use of org.apache.zookeeper.AddWatchMode.PERSISTENT in project zookeeper by apache.

the class PersistentWatcherTest method testBasicAsync.

@Test
public void testBasicAsync() throws IOException, InterruptedException, KeeperException {
    CountdownWatcher watcher = new CountdownWatcher() {

        @Override
        public synchronized void process(WatchedEvent event) {
            super.process(event);
            events.add(event);
        }
    };
    try (ZooKeeper zk = createClient(watcher, hostPort)) {
        final CountDownLatch latch = new CountDownLatch(1);
        AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
            if (rc == KeeperException.Code.OK.intValue()) {
                latch.countDown();
            }
        };
        zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        // clear any events added during client connection
        events.clear();
        internalTestBasic(zk);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ZooDefs(org.apache.zookeeper.ZooDefs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PERSISTENT(org.apache.zookeeper.AddWatchMode.PERSISTENT) AsyncCallback(org.apache.zookeeper.AsyncCallback) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZooKeeper(org.apache.zookeeper.ZooKeeper) AsyncCallback(org.apache.zookeeper.AsyncCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with PERSISTENT

use of org.apache.zookeeper.AddWatchMode.PERSISTENT in project zookeeper by apache.

the class PersistentWatcherTest method testAsyncDefaultWatcher.

@Test
public void testAsyncDefaultWatcher() throws IOException, InterruptedException, KeeperException {
    try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
        final CountDownLatch latch = new CountDownLatch(1);
        AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
            if (rc == KeeperException.Code.OK.intValue()) {
                latch.countDown();
            }
        };
        zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        internalTestBasic(zk);
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ZooDefs(org.apache.zookeeper.ZooDefs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PERSISTENT(org.apache.zookeeper.AddWatchMode.PERSISTENT) AsyncCallback(org.apache.zookeeper.AsyncCallback) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZooKeeper(org.apache.zookeeper.ZooKeeper) AsyncCallback(org.apache.zookeeper.AsyncCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with PERSISTENT

use of org.apache.zookeeper.AddWatchMode.PERSISTENT in project zookeeper by apache.

the class PersistentWatcherTest method testNullWatch.

@Test
public void testNullWatch() throws IOException, InterruptedException, KeeperException {
    try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
        assertThrows(IllegalArgumentException.class, () -> {
            zk.addWatch("/a/b", null, PERSISTENT);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
            };
            zk.addWatch("/a/b", null, PERSISTENT, cb, null);
        });
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ZooDefs(org.apache.zookeeper.ZooDefs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PERSISTENT(org.apache.zookeeper.AddWatchMode.PERSISTENT) AsyncCallback(org.apache.zookeeper.AsyncCallback) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZooKeeper(org.apache.zookeeper.ZooKeeper) AsyncCallback(org.apache.zookeeper.AsyncCallback) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)3 BlockingQueue (java.util.concurrent.BlockingQueue)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 TimeUnit (java.util.concurrent.TimeUnit)3 PERSISTENT (org.apache.zookeeper.AddWatchMode.PERSISTENT)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3 CreateMode (org.apache.zookeeper.CreateMode)3 KeeperException (org.apache.zookeeper.KeeperException)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Watcher (org.apache.zookeeper.Watcher)3 ZooDefs (org.apache.zookeeper.ZooDefs)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Test (org.junit.jupiter.api.Test)3 Logger (org.slf4j.Logger)3