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();
}
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;
}
Aggregations