ZooKeeperServerMain { protected void shutdown() { if (containerManager != null) { containerManager.stop(); } if (cnxnFactory != null) { cnxnFactory.shutdown(); } if (secureCnxnFactory != null) { secureCnxnFactory.shutdown(); } try { if (adminServer != null) { adminServer.shutdown(); } } catch (AdminServerException e) { LOG.warn("Problem stopping AdminServer", e); } }  static void main(String[] args); void runFromConfig(ServerConfig config);  }
@Test(timeout = 30000) public void testReadOnlySnapshotDir() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, true, null); File tmpDir = main.tmpDir; main.start(); Assert.assertTrue("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); File snapDir = new File(main.dataDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); snapDir.setWritable(false); main = new MainThread(CLIENT_PORT, false, tmpDir, null); main.start(); Assert.assertFalse("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); snapDir.setWritable(true); main.deleteDirs(); }
@Test(timeout = 30000) public void testReadOnlyTxnLogDir() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, true, null); File tmpDir = main.tmpDir; main.start(); Assert.assertTrue("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); File logDir = new File(main.logDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); logDir.setWritable(false); main = new MainThread(CLIENT_PORT, false, tmpDir, null); main.start(); Assert.assertFalse("waiting for server being up", ClientBase .waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); main.shutdown(); logDir.setWritable(true); main.deleteDirs(); }
@Test public void testStandalone() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, true, null); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); clientConnected = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); zk.close(); main.shutdown(); main.join(); main.deleteDirs(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test(timeout = 30000) public void testWithAutoCreateDataLogDir() throws Exception { ClientBase.setupTestEnv(); System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, "true"); final int CLIENT_PORT = PortAssignment.unique(); MainThread main = new MainThread(CLIENT_PORT, false, null); String args[] = new String[1]; args[0] = main.confFile.toString(); main.start(); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); clientConnected = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); zk.close(); main.shutdown(); main.join(); main.deleteDirs(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test public void testWithOnlyMinSessionTimeout() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final int tickTime = 2000; final int minSessionTimeout = tickTime * 2 - 100; int maxSessionTimeout = 20 * tickTime; final String configs = "minSessionTimeout=" + minSessionTimeout + "\n"; MainThread main = new MainThread(CLIENT_PORT, false, configs); main.start(); String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); main.shutdown(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test public void testMinMaxSessionTimeOut() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final int tickTime = 2000; final int minSessionTimeout = tickTime * 2 - 100; final int maxSessionTimeout = 20 * tickTime + 1000; final String configs = "maxSessionTimeout=" + maxSessionTimeout + "\n" + "minSessionTimeout=" + minSessionTimeout + "\n"; MainThread main = new MainThread(CLIENT_PORT, false, configs); main.start(); String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); main.shutdown(); Assert.assertTrue("waiting for server down", ClientBase .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); }
@Test public void testJMXRegistrationWithNetty() throws Exception { String originalServerCnxnFactory = System .getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName()); try { ClientBase.setupTestEnv(); File tmpDir_1 = ClientBase.createTmpDir(); ServerCnxnFactory server_1 = startServer(tmpDir_1); File tmpDir_2 = ClientBase.createTmpDir(); ServerCnxnFactory server_2 = startServer(tmpDir_2); server_1.shutdown(); server_2.shutdown(); deleteFile(tmpDir_1); deleteFile(tmpDir_2); } finally { if (originalServerCnxnFactory == null || originalServerCnxnFactory.isEmpty()) { System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); } else { System.setProperty( ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, originalServerCnxnFactory); } } }