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 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 get_state(self): val = _zookeeper.state(self.zk_handle) if val == 0: # Looks like a bug in the ZooKeeper C bindings -- this is the # initial state and the state after disconnecting. return zookeeper.constants.CONNECTING_STATE return val
def ensureCreated(self,path,data="",flags=zookeeper.EPHEMERAL): """ It's possible not to get the flags you want here if the node already exists """ self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle), "Not connected!") try: self.assertEqual(path, zookeeper.create(self.handle, path, data, [ZOO_OPEN_ACL_UNSAFE], flags)) except zookeeper.NodeExistsException: pass
def ensureDeleted(self,path): self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle), "Not connected!") try: self.assertEqual(zookeeper.OK, zookeeper.delete(self.handle, path)) except zookeeper.NoNodeException: pass