[CalendarServer-changes] [4159] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun May 3 19:34:18 PDT 2009


Revision: 4159
          http://trac.macosforge.org/projects/calendarserver/changeset/4159
Author:   sagen at apple.com
Date:     2009-05-03 19:34:16 -0700 (Sun, 03 May 2009)
Log Message:
-----------
Added tests for memcacheprops

Modified Paths:
--------------
    CalendarServer/trunk/memcacheclient.py
    CalendarServer/trunk/twistedcaldav/memcacheprops.py
    CalendarServer/trunk/twistedcaldav/test/util.py

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/test/test_memcacheprops.py

Modified: CalendarServer/trunk/memcacheclient.py
===================================================================
--- CalendarServer/trunk/memcacheclient.py	2009-05-03 18:44:16 UTC (rev 4158)
+++ CalendarServer/trunk/memcacheclient.py	2009-05-04 02:34:16 UTC (rev 4159)
@@ -1078,7 +1078,7 @@
             if self.data.has_key(key):
                 stored_val, stored_token = self.data[key]
                 if token != stored_token:
-                    return False
+                    raise TokenMismatchError(key)
 
         self.data[key] = (serialized, str(self.token))
         self.token += 1

Modified: CalendarServer/trunk/twistedcaldav/memcacheprops.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/memcacheprops.py	2009-05-03 18:44:16 UTC (rev 4158)
+++ CalendarServer/trunk/twistedcaldav/memcacheprops.py	2009-05-04 02:34:16 UTC (rev 4159)
@@ -59,6 +59,7 @@
         if not hasattr(MemcachePropertyCollection, "_memcacheClient"):
 
             log.info("Instantiating memcache connection for MemcachePropertyCollection")
+
             MemcachePropertyCollection._memcacheClient = MemcacheClientFactory.getClient(["%s:%s" % (config.Memcached.BindAddress, config.Memcached.Port)],
                 debug=0,
                 pickleProtocol=2,

Added: CalendarServer/trunk/twistedcaldav/test/test_memcacheprops.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_memcacheprops.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/test/test_memcacheprops.py	2009-05-04 02:34:16 UTC (rev 4159)
@@ -0,0 +1,194 @@
+##
+# Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+##
+
+"""
+Test memcacheprops.
+"""
+
+import os
+
+from twisted.internet.defer import inlineCallbacks
+from twisted.web2.http import HTTPError
+
+from twistedcaldav.config import config
+from twistedcaldav.memcacheprops import MemcachePropertyCollection
+from twistedcaldav.test.util import InMemoryPropertyStore
+from twistedcaldav.test.util import TestCase
+
+
+
+class StubCollection(object):
+
+    def __init__(self, path, childNames):
+        self.path = path
+        self.fp = StubFP(path)
+        self.children = {}
+
+        for childName in childNames:
+            self.children[childName] = StubResource(self, path, childName)
+
+    def listChildren(self):
+        return self.children.iterkeys()
+
+    def getChild(self, childName):
+        return self.children[childName]
+
+    def propertyCollection(self):
+        if not hasattr(self, "_propertyCollection"):
+            self._propertyCollection = MemcachePropertyCollection(self)
+        return self._propertyCollection
+
+
+class StubResource(object):
+
+    def __init__(self, parent, path, name):
+        self.parent = parent
+        self.fp = StubFP(os.path.join(path, name))
+
+    def deadProperties(self):
+        if not hasattr(self, "_dead_properties"):
+            self._dead_properties = self.parent.propertyCollection().propertyStoreForChild(self, InMemoryPropertyStore())
+        return self._dead_properties
+
+class StubFP(object):
+
+    def __init__(self, path):
+        self.path = path
+
+    def child(self, childName):
+        class _Child(object):
+            def __init__(self, path):
+                self.path = path
+        return _Child(os.path.join(self.path, childName))
+
+    def basename(self):
+        return os.path.basename(self.path)
+
+class StubProperty(object):
+
+    def __init__(self, ns, name, value=None):
+        self.ns = ns
+        self.name = name
+        self.value = value
+
+    def qname(self):
+        return self.ns, self.name
+
+
+    def __repr__(self):
+        return "{%s}%s = %s" % (self.ns, self.name, self.value)
+
+
+class MemcachePropertyCollectionTestCase(TestCase):
+    """
+    Test MemcacheProprtyCollection
+    """
+
+    def getColl(self):
+        return StubCollection("calendars", ["a", "b", "c"])
+
+    # @inlineCallbacks
+    def test_setget(self):
+
+        child1 = self.getColl().getChild("a")
+        child1.deadProperties().set(StubProperty("ns1:", "prop1", value="val1"))
+
+        child2 = self.getColl().getChild("a")
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop1")).value,
+            "val1")
+
+        child2.deadProperties().set(StubProperty("ns1:", "prop1", value="val2"))
+
+        # force memcache to be consulted (once per collection per request)
+        child1 = self.getColl().getChild("a")
+
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop1")).value,
+            "val2")
+
+    def test_merge(self):
+        child1 = self.getColl().getChild("a")
+        child2 = self.getColl().getChild("a")
+        child1.deadProperties().set(StubProperty("ns1:", "prop1", value="val0"))
+        child1.deadProperties().set(StubProperty("ns1:", "prop2", value="val0"))
+        child1.deadProperties().set(StubProperty("ns1:", "prop3", value="val0"))
+
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop1")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop3")).value,
+            "val0")
+
+        child2.deadProperties().set(StubProperty("ns1:", "prop1", value="val1"))
+        child1.deadProperties().set(StubProperty("ns1:", "prop3", value="val3"))
+
+        # force memcache to be consulted (once per collection per request)
+        child2 = self.getColl().getChild("a")
+
+        # verify properties
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop1")).value,
+            "val1")
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop3")).value,
+            "val3")
+
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop1")).value,
+            "val1")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop3")).value,
+            "val3")
+
+    def test_delete(self):
+        child1 = self.getColl().getChild("a")
+        child2 = self.getColl().getChild("a")
+        child1.deadProperties().set(StubProperty("ns1:", "prop1", value="val0"))
+        child1.deadProperties().set(StubProperty("ns1:", "prop2", value="val0"))
+        child1.deadProperties().set(StubProperty("ns1:", "prop3", value="val0"))
+
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop1")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop3")).value,
+            "val0")
+
+        child2.deadProperties().set(StubProperty("ns1:", "prop1", value="val1"))
+        child1.deadProperties().delete(("ns1:", "prop1"))
+        self.assertRaises(HTTPError, child1.deadProperties().get, ("ns1:", "prop1"))
+
+        self.assertFalse(child1.deadProperties().contains(("ns1:", "prop1"))) 
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child1.deadProperties().get(("ns1:", "prop3")).value,
+            "val0")
+
+        # force memcache to be consulted (once per collection per request)
+        child2 = self.getColl().getChild("a")
+
+        # verify properties
+        self.assertFalse(child2.deadProperties().contains(("ns1:", "prop1"))) 
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop2")).value,
+            "val0")
+        self.assertEquals(child2.deadProperties().get(("ns1:", "prop3")).value,
+            "val0")

Modified: CalendarServer/trunk/twistedcaldav/test/util.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/util.py	2009-05-03 18:44:16 UTC (rev 4158)
+++ CalendarServer/trunk/twistedcaldav/test/util.py	2009-05-04 02:34:16 UTC (rev 4159)
@@ -158,8 +158,17 @@
     def set(self, property):
         self._properties[property.qname()] = property
 
+    def delete(self, qname):
+        try:
+            del self._properties[qname]
+        except KeyError:
+            pass
 
 
+    def list(self):
+        return self._properties.iterkeys()
+
+
 class StubCacheChangeNotifier(object):
     def __init__(self, *args, **kwargs):
         pass
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20090503/6cb4a60a/attachment-0001.html>


More information about the calendarserver-changes mailing list