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; }
/** 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(); zk.testableWaitForShutdown(CONNECTION_TIMEOUT); LOG.info("{}",zk.testableLocalSocketAddress()); LOG.info("{}",zk.testableRemoteSocketAddress()); LOG.info("{}",zk.toString()); } }
/** * We create a perfectly valid 'exists' request, except that the opcode is wrong. * @return * @throws Exception */ @Test public void testNonExistingOpCode() throws Exception { TestableZooKeeper zk = createClient(); final String path = "/m1"; RequestHeader h = new RequestHeader(); h.setType(888); // This code does not exists ExistsRequest request = new ExistsRequest(); request.setPath(path); request.setWatch(false); ExistsResponse response = new ExistsResponse(); ReplyHeader r = zk.submitRequest(h, request, response, null); Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue()); try { zk.exists("/m1", false); fail("The connection should have been closed"); } catch (KeeperException.ConnectionLossException expected) { } }
/** * 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(); }
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; }
@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(); } }
/** * 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(); }
@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(timeout=30000) public void testFourLetterWordsAllDisabledByDefault() throws Exception { stopServer(); ServerCnxn.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(); }
/** * We create a perfectly valid 'exists' request, except that the opcode is wrong. * @return * @throws Exception */ @Test public void testNonExistingOpCode() throws Exception { TestableZooKeeper zk = createClient(); final String path = "/m1"; RequestHeader h = new RequestHeader(); h.setType(888); // This code does not exists ExistsRequest request = new ExistsRequest(); request.setPath(path); request.setWatch(false); ExistsResponse response = new ExistsResponse(); ReplyHeader r = zk.submitRequest(h, request, response, null); Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue()); zk.testableWaitForShutdown(CONNECTION_TIMEOUT); }
/** * 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(); }
public void run() { try { for (; current < count; current++) { TestableZooKeeper zk = createClient(); zk.close(); // we've asked to close, wait for it to finish closing // all the sub-threads otw the selector may not be // closed when we check (false positive on test Assert.failure zk.testableWaitForShutdown(CONNECTION_TIMEOUT); } } catch (Throwable t) { LOG.error("test Assert.failed", t); } }
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; }
/** * Verify that the server is sending the proper zxid, and as a result * the watch doesn't fire. See ZOOKEEPER-1412. */ @Test public void testFollowerWatcherResync() 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 zk1 = createTestableClient( "localhost:" + qu.getPeer(index).peer.getClientPort()); zk1.create("/foo", "foo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); MyWatcher watcher = new MyWatcher(); TestableZooKeeper zk2 = createTestableClient(watcher, "localhost:" + qu.getPeer(index).peer.getClientPort()); zk2.exists("/foo", true); watcher.reset(); zk2.testableConnloss(); if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { fail("Unable to connect to server"); } assertArrayEquals("foo".getBytes(), zk2.getData("/foo", false, null)); assertNull(watcher.events.poll(5, TimeUnit.SECONDS)); zk1.close(); zk2.close(); }