private Stat create2EmptyNode(TestableZooKeeper zkClient, String path) throws Exception { Stat stat = new Stat(); zkClient.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat); return stat; }
@Test(expected = KeeperException.UnimplementedException.class) public void testDisabled() throws KeeperException, InterruptedException { // note, setUp() enables this test based on the test name zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 100); }
@Test public void testCreate() throws Exception { zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertArrayEquals( String.format("%s Node created (create) with expected value", serverState), DATA, zkClient.getData(PARENT_PATH, false, null)); }
@Test public void testCreate2() throws Exception { zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); Assert.assertArrayEquals( String.format("%s Node created (create2) with expected value", serverState), DATA, zkClient.getData(PARENT_PATH, false, null)); }
@Test public void doTest() throws Exception { if ( testType.ttlsEnabled && (testType.serverId >= EphemeralType.MAX_EXTENDED_SERVER_ID) ) { return; } TestableZooKeeper zk = null; try { zk = createClient(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.delete("/foo", -1); if ( testType.ttlsEnabled ) { zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 1000); // should work } else { try { zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 1000); Assert.fail("Should have thrown KeeperException.UnimplementedException"); } catch (KeeperException.UnimplementedException e) { // expected } } } finally { if ( zk != null ) { zk.close(); } } } }
while(bang) { zk.create("/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL, this, null);
@Test public void testAuth() throws Exception { // Cannot use createClient here because server may close session before // JMXEnv.ensureAll is called which will fail the test case CountdownWatcher watcher = new CountdownWatcher(); TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { Assert.fail("Unable to connect to server"); } try { zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("Should have gotten exception."); } catch (KeeperException e) { // ok, exception as expected. LOG.info("Got exception as expected: " + e); } finally { zk.close(); } } }
@Test public void testCreateSequential() throws KeeperException, InterruptedException { Stat stat = new Stat(); String path = zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists(path, false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists(path, false)); }
@Test public void testCreate() throws KeeperException, InterruptedException { Stat stat = new Stat(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
@Test public void testCreate() throws KeeperException, InterruptedException { Stat stat = new Stat(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
@Test public void testCreateAsync() throws KeeperException, InterruptedException { AsyncCallback.Create2Callback callback = new AsyncCallback.Create2Callback() { @Override public void processResult(int rc, String path, Object ctx, String name, Stat stat) { // NOP } }; zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, callback, null, 100); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
for ( CreateMode createMode : CreateMode.values() ) { try { zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, new Stat(), createMode.isTTL() ? 0 : 100); Assert.fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException dummy) { zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, callback, null, createMode.isTTL() ? 0 : 100); Assert.fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException dummy) {
@Test public void testModifying() throws KeeperException, InterruptedException { Stat stat = new Stat(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); for ( int i = 0; i < 10; ++i ) { fakeElapsed.set(50); zk.setData("/foo", new byte[i + 1], -1); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); } fakeElapsed.set(200); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
/** * Make sure ephemerals get cleaned up when session disconnects. */ @Test public void testSessionDisconnect() throws KeeperException, InterruptedException, IOException { zk.create("/sdisconnect", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); assertNotNull("Ephemeral node has not been created", zk.exists("/sdisconnect", null)); zk.close(); zk = createClient(); assertNull("Ephemeral node shouldn't exist after client disconnect", zk.exists("/sdisconnect", null)); }
/** * Make sure ephemerals are kept when session restores. */ @Test public void testSessionRestore() throws KeeperException, InterruptedException, IOException { zk.create("/srestore", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); assertNotNull("Ephemeral node has not been created", zk.exists("/srestore", null)); zk.disconnect(); zk.close(); zk = createClient(); assertNotNull("Ephemeral node should be present when session is restored", zk.exists("/srestore", null)); }
/** * Make sure ephemerals are kept when server restarts. */ @Test public void testSessionSurviveServerRestart() throws Exception { zk.create("/sdeath", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); assertNotNull("Ephemeral node has not been created", zk.exists("/sdeath", null)); zk.disconnect(); stopServer(); startServer(); zk = createClient(); assertNotNull("Ephemeral node should be present when server restarted", zk.exists("/sdeath", null)); } }
zk1.create("/foo", "foo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);