use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class NIOServerCnxnTest method testOperationsAfterCnxnClose.
/**
* Test operations on ServerCnxn after socket closure.
*/
@Test(timeout = 60000)
public void testOperationsAfterCnxnClose() throws IOException, InterruptedException, KeeperException {
final ZooKeeper zk = createClient();
final String path = "/a";
try {
// make sure zkclient works
zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Assert.assertNotNull("Didn't create znode:" + path, zk.exists(path, false));
// Defaults ServerCnxnFactory would be instantiated with
// NIOServerCnxnFactory
Assert.assertTrue("Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!", serverFactory instanceof NIOServerCnxnFactory);
Iterable<ServerCnxn> connections = serverFactory.getConnections();
for (ServerCnxn serverCnxn : connections) {
serverCnxn.close();
try {
serverCnxn.toString();
} catch (Exception e) {
LOG.error("Exception while getting connection details!", e);
Assert.fail("Shouldn't throw exception while " + "getting connection details!");
}
}
} finally {
zk.close();
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ZooKeeperServerMainTest method testStandalone.
/**
* Verify the ability to start a standalone server instance.
*/
@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));
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ZooKeeperServerMainTest method verifySessionTimeOut.
private void verifySessionTimeOut(int sessionTimeout, int expectedSessionTimeout, String HOSTPORT) throws IOException, KeeperException, InterruptedException {
clientConnected = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper(HOSTPORT, sessionTimeout, this);
Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(sessionTimeout, TimeUnit.MILLISECONDS));
Assert.assertEquals("Not able to configure the sessionTimeout values", expectedSessionTimeout, zk.getSessionTimeout());
zk.close();
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ZooKeeperServerMainTest method testNonRecoverableError.
/**
* Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
* Test to verify that even after non recoverable error (error while
* writing transaction log), ZooKeeper is still available.
*/
@Test(timeout = 30000)
public void testNonRecoverableError() 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));
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this);
zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");
// inject problem in server
ZooKeeperServer zooKeeperServer = main.getCnxnFactory().getZooKeeperServer();
FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) {
@Override
public void commit() throws IOException {
throw new IOException("Input/output error");
}
};
ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
zooKeeperServer.setZKDatabase(newDB);
try {
// do create operation, so that injected IOException is thrown
zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
fail("IOException is expected as error is injected in transaction log commit funtionality");
} catch (Exception e) {
// do nothing
}
zk.close();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT));
fileTxnSnapLogWithError.close();
main.shutdown();
main.deleteDirs();
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ZooKeeperServerStartupTest method testClientConnectionRequestDuringStartupWithNettyServerCnxn.
/**
* Test case for
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-2383}.
*/
@Test(timeout = 30000)
public void testClientConnectionRequestDuringStartupWithNettyServerCnxn() throws Exception {
tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
String originalServerCnxnFactory = System.getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
try {
System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName());
startSimpleZKServer(startupDelayLatch);
SimpleZooKeeperServer simplezks = (SimpleZooKeeperServer) zks;
Assert.assertTrue("Failed to invoke zks#startup() method during server startup", simplezks.waitForStartupInvocation(10));
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zkClient = new ZooKeeper(HOSTPORT, ClientBase.CONNECTION_TIMEOUT, watcher);
Assert.assertFalse("Since server is not fully started, zks#createSession() shouldn't be invoked", simplezks.waitForSessionCreation(5));
LOG.info("Decrements the count of the latch, so that server will proceed with startup");
startupDelayLatch.countDown();
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
Assert.assertTrue("Failed to invoke zks#createSession() method during client session creation", simplezks.waitForSessionCreation(5));
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zkClient.close();
} finally {
// reset cnxn factory
if (originalServerCnxnFactory == null) {
System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
return;
}
System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, originalServerCnxnFactory);
}
}
Aggregations