[Checkins] SVN: zc.zk/trunk/src/zc/zk/ Abstrated zookeeper emulation for tests into separate testing module.

Jim Fulton jim at zope.com
Tue Dec 6 16:20:56 UTC 2011


Log message for revision 123593:
  Abstrated zookeeper emulation for tests into separate testing module.
  

Changed:
  U   zc.zk/trunk/src/zc/zk/README.txt
  A   zc.zk/trunk/src/zc/zk/testing.py
  U   zc.zk/trunk/src/zc/zk/tests.py

-=-
Modified: zc.zk/trunk/src/zc/zk/README.txt
===================================================================
--- zc.zk/trunk/src/zc/zk/README.txt	2011-12-06 10:18:03 UTC (rev 123592)
+++ zc.zk/trunk/src/zc/zk/README.txt	2011-12-06 16:20:53 UTC (rev 123593)
@@ -57,9 +57,8 @@
 .. test
 
    >>> import os
-   >>> zc.zk.decode(ZooKeeper.get(
-   ...     0, '/fooservice/providers/192.168.0.42:8080')[0]
-   ...     ) == dict(pid=os.getpid())
+   >>> zk.get_properties('/fooservice/providers/192.168.0.42:8080'
+   ...                   ) == dict(pid=os.getpid())
    True
 
 

Copied: zc.zk/trunk/src/zc/zk/testing.py (from rev 123592, zc.zk/trunk/src/zc/zk/tests.py)
===================================================================
--- zc.zk/trunk/src/zc/zk/testing.py	                        (rev 0)
+++ zc.zk/trunk/src/zc/zk/testing.py	2011-12-06 16:20:53 UTC (rev 123593)
@@ -0,0 +1,198 @@
+##############################################################################
+#
+# Copyright (c) Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+import json
+import mock
+import time
+import zc.zk
+import zookeeper
+
+def assert_(cond, mess=''):
+    if not cond:
+        print 'assertion failed: ', mess
+
+def setUp(test, tree=None, connection_string='zookeeper.example.com:2181'):
+    if tree:
+        zk = ZooKeeper(connection_string, Node())
+    else:
+        zk = ZooKeeper(
+            connection_string,
+            Node(
+                fooservice = Node(
+                    json.dumps(dict(
+                        database = "/databases/foomain",
+                        threads = 1,
+                        favorite_color= "red",
+                        )),
+                    providers = Node()
+                    ),
+                zookeeper = Node('', quota=Node()),
+                ),
+            )
+    teardowns = []
+    for name in ZooKeeper.__dict__:
+        if name[0] == '_':
+            continue
+        cm = mock.patch('zookeeper.'+name)
+        m = cm.__enter__()
+        m.side_effect = getattr(zk, name)
+        teardowns.append(cm.__exit__)
+
+    if tree:
+        zk = zc.zk.ZooKeeper(connection_string)
+        zk.import_tree(tree)
+        zk.close()
+
+    getattr(test, 'globs', test.__dict__)['zc.zk.testing'] = teardowns
+
+def tearDown(test):
+    globs = getattr(test, 'globs', test.__dict__)
+    for cm in globs['zc.zk.testing']:
+        cm()
+
+class ZooKeeper:
+
+    def __init__(self, connection_string, tree):
+        self.connection_string = connection_string
+        self.root = tree
+        self.sessions = set()
+
+    def init(self, addr, watch=None):
+        assert_(addr==self.connection_string, addr)
+        handle = 0
+        while handle in self.sessions:
+            handle += 1
+        self.sessions.add(handle)
+        if watch:
+            watch(handle,
+                  zookeeper.SESSION_EVENT, zookeeper.CONNECTED_STATE, '')
+
+    def _check_handle(self, handle):
+        if handle not in self.sessions:
+            raise zookeeper.ZooKeeperException('handle out of range')
+
+    def _traverse(self, path):
+        node = self.root
+        for name in path.split('/')[1:]:
+            if not name:
+                continue
+            try:
+                node = node.children[name]
+            except KeyError:
+                raise zookeeper.NoNodeException('no node')
+
+        return node
+
+    def close(self, handle):
+        self._check_handle(handle)
+        self.sessions.remove(handle)
+
+    def state(self, handle):
+        self._check_handle(handle)
+        return zookeeper.CONNECTED_STATE
+
+    def create(self, handle, path, data, acl, flags=0):
+        self._check_handle(handle)
+        base, name = path.rsplit('/', 1)
+        node = self._traverse(base)
+        if name in node.children:
+            raise zookeeper.NodeExistsException()
+        node.children[name] = newnode = Node(data)
+        newnode.acls = acl
+        newnode.flags = flags
+        node.children_changed(handle, zookeeper.CONNECTED_STATE, base)
+        return path
+
+    def delete(self, handle, path):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        base, name = path.rsplit('/', 1)
+        bnode = self._traverse(base)
+        del bnode.children[name]
+        node.deleted(handle, zookeeper.CONNECTED_STATE, path)
+        bnode.children_changed(handle, zookeeper.CONNECTED_STATE, base)
+
+    def exists(self, handle, path):
+        self._check_handle(handle)
+        try:
+            self._traverse(path)
+            return True
+        except zookeeper.NoNodeException:
+            return False
+
+    def get_children(self, handle, path, watch=None):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        if watch:
+            node.child_watchers += (watch, )
+        return sorted(node.children)
+
+    def get(self, handle, path, watch=None):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        if watch:
+            node.watchers += (watch, )
+        return node.data, dict(
+            ephemeralOwner=(1 if node.flags & zookeeper.EPHEMERAL else 0),
+            )
+
+    def set(self, handle, path, data):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        node.data = data
+        node.changed(handle, zookeeper.CONNECTED_STATE, path)
+
+    def get_acl(self, handle, path):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        return dict(aversion=node.aversion), node.acl
+
+    def set_acl(self, handle, path, aversion, acl):
+        self._check_handle(handle)
+        node = self._traverse(path)
+        if aversion != node.aversion:
+            raise zookeeper.BadVersionException("bad version")
+        node.aversion += 1
+        node.acl = acl
+
+class Node:
+    watchers = child_watchers = ()
+    flags = 0
+    aversion = 0
+    acl = zc.zk.OPEN_ACL_UNSAFE
+
+    def __init__(self, data='', **children):
+        self.data = data
+        self.children = children
+
+    def children_changed(self, handle, state, path):
+        watchers = self.child_watchers
+        self.child_watchers = ()
+        for w in watchers:
+            w(handle, zookeeper.CHILD_EVENT, state, path)
+
+    def changed(self, handle, state, path):
+        watchers = self.watchers
+        self.watchers = ()
+        for w in watchers:
+            w(handle, zookeeper.CHANGED_EVENT, state, path)
+
+    def deleted(self, handle, state, path):
+        watchers = self.watchers
+        self.watchers = ()
+        for w in watchers:
+            w(handle, zookeeper.DELETED_EVENT, state, path)
+        watchers = self.child_watchers
+        self.watchers = ()
+        for w in watchers:
+            w(handle, zookeeper.DELETED_EVENT, state, path)

Modified: zc.zk/trunk/src/zc/zk/tests.py
===================================================================
--- zc.zk/trunk/src/zc/zk/tests.py	2011-12-06 10:18:03 UTC (rev 123592)
+++ zc.zk/trunk/src/zc/zk/tests.py	2011-12-06 16:20:53 UTC (rev 123593)
@@ -25,10 +25,10 @@
 import sys
 import time
 import zc.zk
+import zc.zk.testing
 import zc.thread
 import zookeeper
 import zope.testing.loggingsupport
-import zope.testing.setupstack
 import zope.testing.renormalizing
 import unittest
 
@@ -784,165 +784,11 @@
         /providers
     """
 
-def assert_(cond, mess=''):
-    if not cond:
-        print 'assertion failed: ', mess
-
-def setup(test):
-    test.globs['side_effect'] = side_effect
-    test.globs['assert_'] = assert_
-    test.globs['ZooKeeper'] = zk = ZooKeeper(
-        Node(
-            fooservice = Node(
-                json.dumps(dict(
-                    database = "/databases/foomain",
-                    threads = 1,
-                    favorite_color= "red",
-                    )),
-                providers = Node()
-                ),
-            zookeeper = Node('', quota=Node()),
-            ),
-        )
-    for name in ('state', 'init', 'create', 'get', 'set', 'get_children',
-                 'exists', 'get_acl', 'set_acl', 'delete'):
-        cm = mock.patch('zookeeper.'+name)
-        test.globs[name] = m = cm.__enter__()
-        m.side_effect = getattr(zk, name)
-        zope.testing.setupstack.register(test, cm.__exit__)
-
-class ZooKeeper:
-
-    def __init__(self, tree):
-        self.root = tree
-
-    def init(self, addr, watch=None):
-        self.handle = 0
-        assert_(addr=='zookeeper.example.com:2181', addr)
-        if watch:
-            watch(0, zookeeper.SESSION_EVENT, zookeeper.CONNECTED_STATE, '')
-
-    def state(self, handle):
-        self.check_handle(handle)
-        return zookeeper.CONNECTED_STATE
-
-    def check_handle(self, handle):
-        if handle != self.handle:
-            raise zookeeper.ZooKeeperException('handle out of range')
-
-    def traverse(self, path):
-        node = self.root
-        for name in path.split('/')[1:]:
-            if not name:
-                continue
-            try:
-                node = node.children[name]
-            except KeyError:
-                raise zookeeper.NoNodeException('no node')
-
-        return node
-
-    def create(self, handle, path, data, acl, flags=0):
-        self.check_handle(handle)
-        base, name = path.rsplit('/', 1)
-        node = self.traverse(base)
-        if name in node.children:
-            raise zookeeper.NodeExistsException()
-        node.children[name] = newnode = Node(data)
-        newnode.acls = acl
-        newnode.flags = flags
-        node.children_changed(self.handle, zookeeper.CONNECTED_STATE, base)
-        return path
-
-    def delete(self, handle, path):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        base, name = path.rsplit('/', 1)
-        bnode = self.traverse(base)
-        del bnode.children[name]
-        node.deleted(self.handle, zookeeper.CONNECTED_STATE, path)
-        bnode.children_changed(self.handle, zookeeper.CONNECTED_STATE, base)
-
-    def exists(self, handle, path):
-        self.check_handle(handle)
-        try:
-            self.traverse(path)
-            return True
-        except zookeeper.NoNodeException:
-            return False
-
-    def get_children(self, handle, path, watch=None):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        if watch:
-            node.child_watchers += (watch, )
-        return sorted(node.children)
-
-    def get(self, handle, path, watch=None):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        if watch:
-            node.watchers += (watch, )
-        return node.data, dict(
-            ephemeralOwner=(1 if node.flags & zookeeper.EPHEMERAL else 0),
-            )
-
-    def set(self, handle, path, data):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        node.data = data
-        node.changed(self.handle, zookeeper.CONNECTED_STATE, path)
-
-    def get_acl(self, handle, path):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        return dict(aversion=node.aversion), node.acl
-
-    def set_acl(self, handle, path, aversion, acl):
-        self.check_handle(handle)
-        node = self.traverse(path)
-        if aversion != node.aversion:
-            raise zookeeper.BadVersionException("bad version")
-        node.aversion += 1
-        node.acl = acl
-
-class Node:
-    watchers = child_watchers = ()
-    flags = 0
-    aversion = 0
-    acl = zc.zk.OPEN_ACL_UNSAFE
-
-    def __init__(self, data='', **children):
-        self.data = data
-        self.children = children
-
-    def children_changed(self, handle, state, path):
-        watchers = self.child_watchers
-        self.child_watchers = ()
-        for w in watchers:
-            w(handle, zookeeper.CHILD_EVENT, state, path)
-
-    def changed(self, handle, state, path):
-        watchers = self.watchers
-        self.watchers = ()
-        for w in watchers:
-            w(handle, zookeeper.CHANGED_EVENT, state, path)
-
-    def deleted(self, handle, state, path):
-        watchers = self.watchers
-        self.watchers = ()
-        for w in watchers:
-            w(handle, zookeeper.DELETED_EVENT, state, path)
-        watchers = self.child_watchers
-        self.watchers = ()
-        for w in watchers:
-            w(handle, zookeeper.DELETED_EVENT, state, path)
-
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(Tests),
         doctest.DocTestSuite(
-            setUp=setup, tearDown=zope.testing.setupstack.tearDown,
+            setUp=zc.zk.testing.setUp, tearDown=zc.zk.testing.tearDown,
             ),
         manuel.testing.TestSuite(
             manuel.doctest.Manuel(
@@ -950,7 +796,7 @@
                     (re.compile('pid = \d+'), 'pid = 9999')
                     ])) + manuel.capture.Manuel(),
             'README.txt',
-            setUp=setup, tearDown=zope.testing.setupstack.tearDown,
+            setUp=zc.zk.testing.setUp, tearDown=zc.zk.testing.tearDown,
             ),
         unittest.makeSuite(LoggingTests),
         ))



More information about the checkins mailing list