def testhandlereuse(self): """ Test a) multiple concurrent connections b) reuse of closed handles """ cv = threading.Condition() self.connected = False def connection_watcher(handle, type, state, path): cv.acquire() self.connected = True self.assertEqual(zookeeper.CONNECTED_STATE, state) self.handle = handle cv.notify() cv.release() cv.acquire() handles = [ zookeeper.init(self.host) for i in xrange(10) ] ret = zookeeper.init(self.host, connection_watcher) cv.wait(15.0) cv.release() self.assertEqual(self.connected, True, "Connection timed out to " + self.host) self.assertEqual(True, self.all( [ zookeeper.state(handle) == zookeeper.CONNECTED_STATE for handle in handles ] ), "Not all connections succeeded") oldhandle = handles[3] zookeeper.close(oldhandle) newhandle = zookeeper.init(self.host) # This assertion tests *internal* behaviour; i.e. that the module # correctly reuses closed handles. This is therefore implementation # dependent. self.assertEqual(newhandle, oldhandle, "Didn't get reused handle")
def testmanyhandles(self): """ Test the ability of the module to support many handles. """ # We'd like to do more, but currently the C client doesn't # work with > 83 handles (fails to create a pipe) on MacOS 10.5.8 handles = [ zookeeper.init(self.host) for i in xrange(63) ] cv = threading.Condition() self.connected = False def connection_watcher(handle, type, state, path): cv.acquire() self.connected = True self.assertEqual(zookeeper.CONNECTED_STATE, state) self.handle = handle cv.notify() cv.release() cv.acquire() ret = zookeeper.init(self.host, connection_watcher) cv.wait(15.0) cv.release() self.assertEqual(self.connected, True, "Connection timed out to " + self.host) for i,h in enumerate(handles): path = "/zkpython-test-handles-%s" % str(i) self.assertEqual(path, zookeeper.create(h, path, "", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)) self.assertEqual(True, self.all( zookeeper.close(h) == zookeeper.OK for h in handles ))
def __init__(self, connect_string, session_timeout=10000, watcher=None, client_id=None): """Create a ZooKeeper client object. To create a ZooKeeper client object, the application needs to pass a connection string containing a comma separated list of host:port pairs, each corresponding to a ZooKeeper server. Session establishment is asynchronous. This constructor will initiate connection to the server and return immediately - potentially (usually) before the session is fully established. The watcher argument specifies the watcher that will be notified of any changes in state. This notification can come at any point before or after the constructor call has returned. The instantiated ZooKeeper client object will pick an arbitrary server from the connect_string and attempt to connect to it. If establishment of the connection fails, another server in the connect string will be tried (the order is non-deterministic, as we random shuffle the list), until a connection is established. The client will continue attempts until the session is explicitly closed (or the session is expired by the server). Added in 3.2.0: An optional "chroot" suffix may also be appended to the connection string. This will run the client commands while interpreting all paths relative to this root (similar to the unix chroot command). It is possible to connect using an existing client connection. Use `get_client_id()` to get a (session_id, password) tuple for an established client connection to get parameters. This tuple can be passed to ``__init__()`` as `client_id` to reconnect. :Parameters: - `connect_string`: comma separated host:port pairs, each corresponding to a zk server. - `session_timeout`: session timeout in milliseconds - `watcher`: a watcher function which will be notified of state changes -- it will be passed an Event object - `client_id`: session id and passwd tuple to use if reconnecting :Exceptions: - TODO """ if watcher: watcher = self._wrap_watcher(watcher) if client_id: self._zk_handle = _zookeeper.init( connect_string, watcher, session_timeout, client_id) else: self._zk_handle = _zookeeper.init( connect_string, watcher, session_timeout) assert isinstance(self._zk_handle, int)
def testconnection(self): cv = threading.Condition() self.connected = False def connection_watcher(handle, type, state, path): cv.acquire() self.connected = True self.assertEqual(zookeeper.CONNECTED_STATE, state) self.handle = handle cv.notify() cv.release() cv.acquire() ret = zookeeper.init(self.host, connection_watcher) cv.wait(15.0) cv.release() self.assertEqual(self.connected, True, "Connection timed out to " + self.host) self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle)) self.assertEqual(zookeeper.close(self.handle), zookeeper.OK) # Trying to close the same handle twice is an error, and the C library will segfault on it # so make sure this is caught at the Python module layer self.assertRaises(zookeeper.ZooKeeperException, zookeeper.close, self.handle) self.assertRaises(zookeeper.ZooKeeperException, zookeeper.get, self.handle, "/")
def newConnection(self): cv = threading.Condition() self.pending_connection = False def connection_watcher(handle, type, state, path): cv.acquire() self.pending_connection = True cv.notify() cv.release() cv.acquire() handle = zookeeper.init(self.host, connection_watcher) cv.wait(15.0) cv.release() if not self.pending_connection: raise Exception("Couldn't connect to host -", self.host) return handle
def setUp(self): self.callback_flag = False self.cv = threading.Condition() self.connected = False def connection_watcher(handle, type, state, path): self.cv.acquire() self.connected = True self.cv.notify() self.cv.release() self.cv.acquire() self.handle = zookeeper.init(self.host, connection_watcher) self.cv.wait(15.0) self.cv.release() if not self.connected: raise Exception("Couldn't connect to host -", self.host)
def testclientid(self): cv = threading.Condition() self.connected = False def connection_watcher(handle, type, state, path): cv.acquire() self.connected = True cv.notify() cv.release() cv.acquire() self.handle = zookeeper.init(self.host, connection_watcher,10000,(123456,"mypassword")) self.assertEqual(self.handle, zookeeper.OK) cv.wait(15.0) cv.release() self.assertEqual(self.connected, True, "Connection timed out to " + self.host) (cid,passwd) = zookeeper.client_id(self.handle) self.assertEqual(cid,123456) self.assertEqual(passwd,"mypassword")