public void run() { try { CountdownWatcher watcher = new CountdownWatcher(); zk = new TestableZooKeeper(qb.hostPort, CONNECTION_TIMEOUT, watcher); watcher.waitForConnected(CONNECTION_TIMEOUT); while(bang) { zk.create("/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL, this, null); if (zk != null) { try { if (!zk.close(CONNECTION_TIMEOUT)) { failed = true; LOG.error("Client did not shutdown");
@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(); } } } }
/** * 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)); }
/** Exercise the testable functions, verify tostring, etc... */ @Test public void testTestability() throws Exception { TestableZooKeeper zk = createClient(); try { LOG.info("{}",zk.testableLocalSocketAddress()); LOG.info("{}",zk.testableRemoteSocketAddress()); LOG.info("{}",zk.toString()); } finally { zk.close(CONNECTION_TIMEOUT); LOG.info("{}",zk.testableLocalSocketAddress()); LOG.info("{}",zk.testableRemoteSocketAddress()); LOG.info("{}",zk.toString()); } }
/** * 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)); }
String sid = getHexSessionId(zk.getSessionId()); verify(hp, "dirs", "size"); zk.getData("/", true, null); verify(hp, "dirs", "size"); zk.close();
protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp, int timeout) throws IOException, InterruptedException { watcher.reset(); TestableZooKeeper zk = new TestableZooKeeper(hp, timeout, watcher); if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) { Assert.fail("Unable to connect to server"); } synchronized(this) { if (!allClientsSetup) { LOG.error("allClients never setup"); Assert.fail("allClients never setup"); } if (allClients != null) { allClients.add(zk); } else { // test done - close the zk, not needed zk.close(); } } watcher.initializeWatchedClient(zk); return zk; }
protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp, int timeout) throws IOException, InterruptedException { watcher.reset(); TestableZooKeeper zk = new TestableZooKeeper(hp, timeout, watcher); if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) { Assert.fail("Unable to connect to server"); } synchronized(this) { if (!allClientsSetup) { LOG.error("allClients never setup"); Assert.fail("allClients never setup"); } if (allClients != null) { allClients.add(zk); JMXEnv.ensureAll(getHexSessionId(zk.getSessionId())); } else { // test done - close the zk, not needed zk.close(); } } return zk; }
zk1.create("/foo", "foo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); "localhost:" + qu.getPeer(index).peer.getClientPort()); zk2.exists("/foo", true); zk2.testableConnloss(); if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) assertArrayEquals("foo".getBytes(), zk2.getData("/foo", false, null)); zk1.close(); zk2.close(); qu.shutdownAll();
TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); ExistsResponse response = new ExistsResponse(); ReplyHeader r = zk.submitRequest(h, request, response, null); zk.close();
@Override public void tearDown() throws Exception { System.clearProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY); System.clearProperty(EphemeralType.TTL_3_5_3_EMULATION_PROPERTY); super.tearDown(); zk.close(); }
/** * Verify that the server is sending the proper zxid. See ZOOKEEPER-1412. */ @Test public void testFollowerSendsLastZxid() throws Exception { QuorumUtil qu = new QuorumUtil(1); qu.startAll(); int index = 1; while(qu.getPeer(index).peer.follower == null) { index++; } LOG.info("Connecting to follower: {}", index); TestableZooKeeper zk = createTestableClient("localhost:" + qu.getPeer(index).peer.getClientPort()); assertEquals(0L, zk.testableLastZxid()); zk.exists("/", false); long lzxid = zk.testableLastZxid(); assertTrue("lzxid:" + lzxid + " > 0", lzxid > 0); zk.close(); qu.shutdownAll(); }
watches[i] = new MyWatcher(); cbs[i] = new MyStatCallback(); zk.exists("/test", watches[i], cbs[i], count); zk.exists("/test", false); Assert.assertTrue("Failed to pause the connection!", zk.pauseCnxn(3000)); zk2.close(); stopServer(); watches[i] = new MyWatcher(); cbs[i] = new MyStatCallback(); zk.exists("/test", watches[i], cbs[i], count); Assert.assertEquals(null, zk.exists("/test", false)); waitForAllWatchers(); for(int i = 0; i < COUNT/2; i++) { zk.close();
private static TestableZooKeeper createTestableClient( CountdownWatcher watcher, String hp) throws IOException, TimeoutException, InterruptedException { TestableZooKeeper zk = new TestableZooKeeper( hp, ClientBase.CONNECTION_TIMEOUT, watcher); watcher.waitForConnected(CONNECTION_TIMEOUT); return zk; }
@Test(timeout=30000) public void testFourLetterWordsAllDisabledByDefault() throws Exception { stopServer(); FourLetterCommands.resetWhiteList(); System.setProperty("zookeeper.4lw.commands.whitelist", "stat"); startServer(); // Default white list for 3.5.x is empty, so all command should fail. verifyAllCommandsFail(); TestableZooKeeper zk = createClient(); verifyAllCommandsFail(); zk.getData("/", true, null); verifyAllCommandsFail(); zk.close(); verifyFuzzyMatch("stat", "Outstanding"); verifyAllCommandsFail(); }
@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)); }
/** * 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)); } }
@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)); }
@Test public void testMaxTTLs() throws InterruptedException, KeeperException { RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.createTTL); String path = "/bad_ttl"; CreateTTLRequest request = new CreateTTLRequest(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL.toFlag(), EphemeralType.TTL.maxValue()); CreateResponse response = new CreateResponse(); ReplyHeader r = zk.submitRequest(h, request, response, null); Assert.assertEquals("EphemeralType.getMaxTTL() should succeed", r.getErr(), Code.OK.intValue()); Assert.assertNotNull("Node should exist", zk.exists(path, false)); }