Search in sources :

Example 6 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class LocalZooKeeperConnectionServiceTest method testSimpleZooKeeperConnection.

@Test
void testSimpleZooKeeperConnection() throws Exception {
    LocalZooKeeperConnectionService localZkConnectionService = new LocalZooKeeperConnectionService(mockZkClientFactory, "dummy", 1000);
    localZkConnectionService.start(null);
    // Get ZooKeeper client
    MockZooKeeper zk = (MockZooKeeper) localZkConnectionService.getLocalZooKeeper();
    // Check status
    assertTrue(zk.getState().isConnected());
    // Create persistent node
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/path1");
    assertTrue(zk.exists("/path1", false) != null);
    // Delete and re-create existing node
    // The sessionId must be set to except 0L in order to re-create.
    zk.setSessionId(-1L);
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path1", "data1", CreateMode.EPHEMERAL, true);
    assertEquals(zk.getData("/path1", null, null), "data1".getBytes());
    // Try to create existing node (nothing should happen)
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/path1");
    assertTrue(zk.exists("/path1", false) != null);
    // Create new node (data is given as String)
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path2", "data2", CreateMode.EPHEMERAL);
    assertTrue(zk.exists("/path2", false) != null);
    assertEquals(zk.getData("/path2", null, null), "data2".getBytes());
    // Create new node (data is given as bytes)
    LocalZooKeeperConnectionService.createIfAbsent(zk, "/path3", "data3".getBytes(), CreateMode.EPHEMERAL);
    assertTrue(zk.exists("/path3", false) != null);
    assertEquals(zk.getData("/path3", null, null), "data3".getBytes());
    // delete nodes
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path1", -1);
    assertTrue(zk.exists("/path1", false) == null);
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path2", -1);
    assertTrue(zk.exists("/path2", false) == null);
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/path3", -1);
    assertTrue(zk.exists("/path3", false) == null);
    // delete not existing node
    LocalZooKeeperConnectionService.deleteIfExists(zk, "/not_exist", -1);
    // Try to create invalid node (nothing should happen)
    LocalZooKeeperConnectionService.checkAndCreatePersistNode(zk, "/////");
    assertTrue(zk.exists("//////", false) == null);
    localZkConnectionService.close();
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper) Test(org.testng.annotations.Test)

Example 7 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class BaseZKStarterTest method createMockZooKeeper.

/**
     * Create MockZookeeper instance
     * @return
     * @throws Exception
     */
protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.sameThreadExecutor());
    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT, "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    return zk;
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper)

Aggregations

MockZooKeeper (org.apache.zookeeper.MockZooKeeper)7 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ACL (org.apache.zookeeper.data.ACL)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)1 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OrderedSafeExecutor (org.apache.bookkeeper.util.OrderedSafeExecutor)1 KeeperException (org.apache.zookeeper.KeeperException)1 Code (org.apache.zookeeper.KeeperException.Code)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Event (org.apache.zookeeper.Watcher.Event)1