def test_multiple_watchers(self): """ Test whether multiple watchers are correctly called """ cv1, cv2 = threading.Condition(), threading.Condition() def watcher1(*args, **kwargs): cv1.acquire() self.watcher1 = True cv1.notify() cv1.release() def watcher2(*args, **kwargs): cv2.acquire() self.watcher2 = True cv2.notify() cv2.release() nodename = "/zk-python-multiple-watcher-test" self.ensureCreated(nodename, "test") cv1.acquire() cv2.acquire() zookeeper.get(self.handle, nodename, watcher1) zookeeper.get(self.handle, nodename, watcher2) zookeeper.set(self.handle, nodename, "test") cv1.wait(15) cv2.wait(15) self.assertTrue(self.watcher1 and self.watcher2, "One or more watchers failed to fire")
def test_none_callback(self): """ Test that no errors are raised when None is passed as a callback. """ self.ensureCreated("/zk-python-none-callback-test","test") # To do this we need to issue two operations, waiting on the second # to ensure that the first completes zookeeper.get(self.handle, "/zk-python-none-callback-test", None) (d,s) = zookeeper.get(self.handle, "/zk-python-none-callback-test") self.assertEqual(d, "test")
def test_sync_get_large_datanode(self): """ Test that we can retrieve datanode sizes up to 1Mb with default parameters (depends on ZooKeeper server). """ data = ''.join(["A" for x in xrange(1024*1023)]) self.ensureDeleted("/zk-python-test-large-datanode") zookeeper.create(self.handle, "/zk-python-test-large-datanode", data, [{"perms":0x1f, "scheme":"world", "id" :"anyone"}]) (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode") self.assertEqual(len(ret), 1024*1023, "Should have got 1Mb returned, instead got %s" % len(ret)) (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode",None,500) self.assertEqual(len(ret), 500, "Should have got 500 bytes returned, instead got %s" % len(ret))
def test_sync_getset(self): self.assertEqual(self.connected, True, "Not connected!") (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) self.assertEqual(data, "on", "Data is not 'on' as expected: " + data) ret = zookeeper.set(self.handle, "/zk-python-getsettest", "off", stat["version"]) (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) self.assertEqual(data, "off", "Data is not 'off' as expected: " + data) self.assertRaises(zookeeper.BadVersionException, zookeeper.set, self.handle, "/zk-python-getsettest", "test", stat["version"]+1) stat2 = zookeeper.set2(self.handle, "/zk-python-getsettest", "set2", stat["version"]) self.assertNotEqual(stat2, None, "set2 call failed, return should not be None") self.assertEqual(stat2["numChildren"], 0, "set2 call failed, numChildren not 0 in set2 call") (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) self.assertEqual(data, "set2", "Data is not 'set2' as expected: " + data)
def get(self, path, watcher=None): """Return the data and the stat of the node of the given path. If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node. :Parameters: - `path`: node path - `watcher`: (optional) watcher function :Returns: (data, stat) for the node :Exceptions: - `NoNodeException`: given path does not exist """ if watcher: data, stat = _zookeeper.get(self.zk_handle, path, self._wrap_watcher(watcher)) else: data, stat = _zookeeper.get(self.zk_handle, path) return data, Stat(**stat)