<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[2439] CalendarServer/branches/unified-cache/twistedcaldav</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/calendarserver/changeset/2439">2439</a></dd>
<dt>Author</dt> <dd>dreid@apple.com</dd>
<dt>Date</dt> <dd>2008-05-21 16:11:19 -0700 (Wed, 21 May 2008)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add twisted memcached protocol implementation</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesunifiedcachetwistedcaldavmemcachepy">CalendarServer/branches/unified-cache/twistedcaldav/memcache.py</a></li>
<li><a href="#CalendarServerbranchesunifiedcachetwistedcaldavtesttest_memcachepy">CalendarServer/branches/unified-cache/twistedcaldav/test/test_memcache.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesunifiedcachetwistedcaldavmemcachepy"></a>
<div class="addfile"><h4>Added: CalendarServer/branches/unified-cache/twistedcaldav/memcache.py (0 => 2439)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/unified-cache/twistedcaldav/memcache.py                                (rev 0)
+++ CalendarServer/branches/unified-cache/twistedcaldav/memcache.py        2008-05-21 23:11:19 UTC (rev 2439)
</span><span class="lines">@@ -0,0 +1,657 @@
</span><ins>+# -*- test-case-name: twisted.test.test_memcache -*-
+# Copyright (c) 2007 Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+&quot;&quot;&quot;
+Memcache client protocol. Memcached is a caching server, storing data in the
+form of pairs key/value, and memcache is the protocol to talk with it.
+
+To connect to a server, create a factory for L{MemCacheProtocol}::
+
+    from twisted.internet import reactor, protocol
+    from twisted.protocols.memcache import MemCacheProtocol, DEFAULT_PORT
+    d = protocol.ClientCreator(reactor, MemCacheProtocol
+        ).connectTCP(&quot;localhost&quot;, DEFAULT_PORT)
+    def doSomething(proto):
+        # Here you call the memcache operations
+        return proto.set(&quot;mykey&quot;, &quot;a lot of data&quot;)
+    d.addCallback(doSomething)
+    reactor.run()
+
+All the operations of the memcache protocol are present, but
+L{MemCacheProtocol.set} and L{MemCacheProtocol.get} are the more important.
+
+See U{http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt} for
+more information about the protocol.
+&quot;&quot;&quot;
+
+try:
+    from collections import deque
+except ImportError:
+    class deque(list):
+        def popleft(self):
+            return self.pop(0)
+
+
+from twisted.protocols.basic import LineReceiver
+from twisted.protocols.policies import TimeoutMixin
+from twisted.internet.defer import Deferred, fail, TimeoutError
+from twisted.python import log
+
+
+
+DEFAULT_PORT = 11211
+
+
+
+class NoSuchCommand(Exception):
+    &quot;&quot;&quot;
+    Exception raised when a non existent command is called.
+    &quot;&quot;&quot;
+
+
+
+class ClientError(Exception):
+    &quot;&quot;&quot;
+    Error caused by an invalid client call.
+    &quot;&quot;&quot;
+
+
+
+class ServerError(Exception):
+    &quot;&quot;&quot;
+    Problem happening on the server.
+    &quot;&quot;&quot;
+
+
+
+class Command(object):
+    &quot;&quot;&quot;
+    Wrap a client action into an object, that holds the values used in the
+    protocol.
+
+    @ivar _deferred: the L{Deferred} object that will be fired when the result
+        arrives.
+    @type _deferred: L{Deferred}
+
+    @ivar command: name of the command sent to the server.
+    @type command: C{str}
+    &quot;&quot;&quot;
+
+    def __init__(self, command, **kwargs):
+        &quot;&quot;&quot;
+        Create a command.
+
+        @param command: the name of the command.
+        @type command: C{str}
+
+        @param kwargs: this values will be stored as attributes of the object
+            for future use
+        &quot;&quot;&quot;
+        self.command = command
+        self._deferred = Deferred()
+        for k, v in kwargs.items():
+            setattr(self, k, v)
+
+
+    def success(self, value):
+        &quot;&quot;&quot;
+        Shortcut method to fire the underlying deferred.
+        &quot;&quot;&quot;
+        self._deferred.callback(value)
+
+
+    def fail(self, error):
+        &quot;&quot;&quot;
+        Make the underlying deferred fails.
+        &quot;&quot;&quot;
+        self._deferred.errback(error)
+
+
+
+class MemCacheProtocol(LineReceiver, TimeoutMixin):
+    &quot;&quot;&quot;
+    MemCache protocol: connect to a memcached server to store/retrieve values.
+
+    @ivar persistentTimeOut: the timeout period used to wait for a response.
+    @type persistentTimeOut: C{int}
+
+    @ivar _current: current list of requests waiting for an answer from the
+        server.
+    @type _current: C{deque} of L{Command}
+
+    @ivar _lenExpected: amount of data expected in raw mode, when reading for
+        a value.
+    @type _lenExpected: C{int}
+
+    @ivar _getBuffer: current buffer of data, used to store temporary data
+        when reading in raw mode.
+    @type _getBuffer: C{list}
+
+    @ivar _bufferLength: the total amount of bytes in C{_getBuffer}.
+    @type _bufferLength: C{int}
+    &quot;&quot;&quot;
+    MAX_KEY_LENGTH = 250
+
+    def __init__(self, timeOut=60):
+        &quot;&quot;&quot;
+        Create the protocol.
+
+        @param timeOut: the timeout to wait before detecting that the
+            connection is dead and close it. It's expressed in seconds.
+        @type timeOut: C{int}
+        &quot;&quot;&quot;
+        self._current = deque()
+        self._lenExpected = None
+        self._getBuffer = None
+        self._bufferLength = None
+        self.persistentTimeOut = self.timeOut = timeOut
+
+
+    def timeoutConnection(self):
+        &quot;&quot;&quot;
+        Close the connection in case of timeout.
+        &quot;&quot;&quot;
+        for cmd in self._current:
+            cmd.fail(TimeoutError(&quot;Connection timeout&quot;))
+        self.transport.loseConnection()
+
+
+    def sendLine(self, line):
+        &quot;&quot;&quot;
+        Override sendLine to add a timeout to response.
+        &quot;&quot;&quot;
+        if not self._current:
+           self.setTimeout(self.persistentTimeOut)
+        LineReceiver.sendLine(self, line)
+
+
+    def rawDataReceived(self, data):
+        &quot;&quot;&quot;
+        Collect data for a get.
+        &quot;&quot;&quot;
+        self.resetTimeout()
+        self._getBuffer.append(data)
+        self._bufferLength += len(data)
+        if self._bufferLength &gt;= self._lenExpected + 2:
+            data = &quot;&quot;.join(self._getBuffer)
+            buf = data[:self._lenExpected]
+            rem = data[self._lenExpected + 2:]
+            val = buf
+            self._lenExpected = None
+            self._getBuffer = None
+            self._bufferLength = None
+            cmd = self._current[0]
+            cmd.value = val
+            self.setLineMode(rem)
+
+
+    def cmd_STORED(self):
+        &quot;&quot;&quot;
+        Manage a success response to a set operation.
+        &quot;&quot;&quot;
+        self._current.popleft().success(True)
+
+
+    def cmd_NOT_STORED(self):
+        &quot;&quot;&quot;
+        Manage a specific 'not stored' response to a set operation: this is not
+        an error, but some condition wasn't met.
+        &quot;&quot;&quot;
+        self._current.popleft().success(False)
+
+
+    def cmd_END(self):
+        &quot;&quot;&quot;
+        This the end token to a get or a stat operation.
+        &quot;&quot;&quot;
+        cmd = self._current.popleft()
+        if cmd.command == &quot;get&quot;:
+            cmd.success((cmd.flags, cmd.value))
+        elif cmd.command == &quot;gets&quot;:
+            cmd.success((cmd.flags, cmd.cas, cmd.value))
+        elif cmd.command == &quot;stats&quot;:
+            cmd.success(cmd.values)
+
+
+    def cmd_NOT_FOUND(self):
+        &quot;&quot;&quot;
+        Manage error response for incr/decr/delete.
+        &quot;&quot;&quot;
+        self._current.popleft().success(False)
+
+
+    def cmd_VALUE(self, line):
+        &quot;&quot;&quot;
+        Prepare the reading a value after a get.
+        &quot;&quot;&quot;
+        cmd = self._current[0]
+        if cmd.command == &quot;get&quot;:
+            key, flags, length = line.split()
+            cas = &quot;&quot;
+        else:
+            key, flags, length, cas = line.split()
+        self._lenExpected = int(length)
+        self._getBuffer = []
+        self._bufferLength = 0
+        if cmd.key != key:
+            raise RuntimeError(&quot;Unexpected commands answer.&quot;)
+        cmd.flags = int(flags)
+        cmd.length = self._lenExpected
+        cmd.cas = cas
+        self.setRawMode()
+
+
+    def cmd_STAT(self, line):
+        &quot;&quot;&quot;
+        Reception of one stat line.
+        &quot;&quot;&quot;
+        cmd = self._current[0]
+        key, val = line.split(&quot; &quot;, 1)
+        cmd.values[key] = val
+
+
+    def cmd_VERSION(self, versionData):
+        &quot;&quot;&quot;
+        Read version token.
+        &quot;&quot;&quot;
+        self._current.popleft().success(versionData)
+
+
+    def cmd_ERROR(self):
+        &quot;&quot;&quot;
+        An non-existent command has been sent.
+        &quot;&quot;&quot;
+        log.err(&quot;Non-existent command sent.&quot;)
+        cmd = self._current.popleft()
+        cmd.fail(NoSuchCommand())
+
+
+    def cmd_CLIENT_ERROR(self, errText):
+        &quot;&quot;&quot;
+        An invalid input as been sent.
+        &quot;&quot;&quot;
+        log.err(&quot;Invalid input: %s&quot; % (errText,))
+        cmd = self._current.popleft()
+        cmd.fail(ClientError(errText))
+
+
+    def cmd_SERVER_ERROR(self, errText):
+        &quot;&quot;&quot;
+        An error has happened server-side.
+        &quot;&quot;&quot;
+        log.err(&quot;Server error: %s&quot; % (errText,))
+        cmd = self._current.popleft()
+        cmd.fail(ServerError(errText))
+
+
+    def cmd_DELETED(self):
+        &quot;&quot;&quot;
+        A delete command has completed successfully.
+        &quot;&quot;&quot;
+        self._current.popleft().success(True)
+
+
+    def cmd_OK(self):
+        &quot;&quot;&quot;
+        The last command has been completed.
+        &quot;&quot;&quot;
+        self._current.popleft().success(True)
+
+
+    def cmd_EXISTS(self):
+        &quot;&quot;&quot;
+        A C{checkAndSet} update has failed.
+        &quot;&quot;&quot;
+        self._current.popleft().success(False)
+
+
+    def lineReceived(self, line):
+        &quot;&quot;&quot;
+        Receive line commands from the server.
+        &quot;&quot;&quot;
+        self.resetTimeout()
+        token = line.split(&quot; &quot;, 1)[0]
+        # First manage standard commands without space
+        cmd = getattr(self, &quot;cmd_%s&quot; % (token,), None)
+        if cmd is not None:
+            args = line.split(&quot; &quot;, 1)[1:]
+            if args:
+                cmd(args[0])
+            else:
+                cmd()
+        else:
+            # Then manage commands with space in it
+            line = line.replace(&quot; &quot;, &quot;_&quot;)
+            cmd = getattr(self, &quot;cmd_%s&quot; % (line,), None)
+            if cmd is not None:
+                cmd()
+            else:
+                # Increment/Decrement response
+                cmd = self._current.popleft()
+                val = int(line)
+                cmd.success(val)
+        if not self._current:
+            # No pending request, remove timeout
+            self.setTimeout(None)
+
+
+    def increment(self, key, val=1):
+        &quot;&quot;&quot;
+        Increment the value of C{key} by given value (default to 1).
+        C{key} must be consistent with an int. Return the new value.
+
+        @param key: the key to modify.
+        @type key: C{str}
+
+        @param val: the value to increment.
+        @type val: C{int}
+
+        @return: a deferred with will be called back with the new value
+            associated with the key (after the increment).
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._incrdecr(&quot;incr&quot;, key, val)
+
+
+    def decrement(self, key, val=1):
+        &quot;&quot;&quot;
+        Decrement the value of C{key} by given value (default to 1).
+        C{key} must be consistent with an int. Return the new value, coerced to
+        0 if negative.
+
+        @param key: the key to modify.
+        @type key: C{str}
+
+        @param val: the value to decrement.
+        @type val: C{int}
+
+        @return: a deferred with will be called back with the new value
+            associated with the key (after the decrement).
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._incrdecr(&quot;decr&quot;, key, val)
+
+
+    def _incrdecr(self, cmd, key, val):
+        &quot;&quot;&quot;
+        Internal wrapper for incr/decr.
+        &quot;&quot;&quot;
+        if not isinstance(key, str):
+            return fail(ClientError(
+                &quot;Invalid type for key: %s, expecting a string&quot; % (type(key),)))
+        if len(key) &gt; self.MAX_KEY_LENGTH:
+            return fail(ClientError(&quot;Key too long&quot;))
+        fullcmd = &quot;%s %s %d&quot; % (cmd, key, int(val))
+        self.sendLine(fullcmd)
+        cmdObj = Command(cmd, key=key)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def replace(self, key, val, flags=0, expireTime=0):
+        &quot;&quot;&quot;
+        Replace the given C{key}. It must already exist in the server.
+
+        @param key: the key to replace.
+        @type key: C{str}
+
+        @param val: the new value associated with the key.
+        @type val: C{str}
+
+        @param flags: the flags to store with the key.
+        @type flags: C{int}
+
+        @param expireTime: if different from 0, the relative time in seconds
+            when the key will be deleted from the store.
+        @type expireTime: C{int}
+
+        @return: a deferred that will fire with C{True} if the operation has
+            succeeded, and C{False} with the key didn't previously exist.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._set(&quot;replace&quot;, key, val, flags, expireTime, &quot;&quot;)
+
+
+    def add(self, key, val, flags=0, expireTime=0):
+        &quot;&quot;&quot;
+        Add the given C{key}. It must not exist in the server.
+
+        @param key: the key to add.
+        @type key: C{str}
+
+        @param val: the value associated with the key.
+        @type val: C{str}
+
+        @param flags: the flags to store with the key.
+        @type flags: C{int}
+
+        @param expireTime: if different from 0, the relative time in seconds
+            when the key will be deleted from the store.
+        @type expireTime: C{int}
+
+        @return: a deferred that will fire with C{True} if the operation has
+            succeeded, and C{False} with the key already exists.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._set(&quot;add&quot;, key, val, flags, expireTime, &quot;&quot;)
+
+
+    def set(self, key, val, flags=0, expireTime=0):
+        &quot;&quot;&quot;
+        Set the given C{key}.
+
+        @param key: the key to set.
+        @type key: C{str}
+
+        @param val: the value associated with the key.
+        @type val: C{str}
+
+        @param flags: the flags to store with the key.
+        @type flags: C{int}
+
+        @param expireTime: if different from 0, the relative time in seconds
+            when the key will be deleted from the store.
+        @type expireTime: C{int}
+
+        @return: a deferred that will fire with C{True} if the operation has
+            succeeded.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._set(&quot;set&quot;, key, val, flags, expireTime, &quot;&quot;)
+
+
+    def checkAndSet(self, key, val, cas, flags=0, expireTime=0):
+        &quot;&quot;&quot;
+        Change the content of C{key} only if the C{cas} value matches the
+        current one associated with the key. Use this to store a value which
+        hasn't been modified since last time you fetched it.
+
+        @param key: The key to set.
+        @type key: C{str}
+
+        @param val: The value associated with the key.
+        @type val: C{str}
+
+        @param cas: Unique 64-bit value returned by previous call of C{get}.
+        @type cas: C{str}
+
+        @param flags: The flags to store with the key.
+        @type flags: C{int}
+
+        @param expireTime: If different from 0, the relative time in seconds
+            when the key will be deleted from the store.
+        @type expireTime: C{int}
+
+        @return: A deferred that will fire with C{True} if the operation has
+            succeeded, C{False} otherwise.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        return self._set(&quot;cas&quot;, key, val, flags, expireTime, cas)
+
+
+    def _set(self, cmd, key, val, flags, expireTime, cas):
+        &quot;&quot;&quot;
+        Internal wrapper for setting values.
+        &quot;&quot;&quot;
+        if not isinstance(key, str):
+            return fail(ClientError(
+                &quot;Invalid type for key: %s, expecting a string&quot; % (type(key),)))
+        if len(key) &gt; self.MAX_KEY_LENGTH:
+            return fail(ClientError(&quot;Key too long&quot;))
+        if not isinstance(val, str):
+            return fail(ClientError(
+                &quot;Invalid type for value: %s, expecting a string&quot; %
+                (type(val),)))
+        if cas:
+            cas = &quot; &quot; + cas
+        length = len(val)
+        fullcmd = &quot;%s %s %d %d %d%s&quot; % (
+            cmd, key, flags, expireTime, length, cas)
+        self.sendLine(fullcmd)
+        self.sendLine(val)
+        cmdObj = Command(cmd, key=key, flags=flags, length=length)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def append(self, key, val):
+        &quot;&quot;&quot;
+        Append given data to the value of an existing key.
+
+        @param key: The key to modify.
+        @type key: C{str}
+
+        @param val: The value to append to the current value associated with
+            the key.
+        @type val: C{str}
+
+        @return: A deferred that will fire with C{True} if the operation has
+            succeeded, C{False} otherwise.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        # Even if flags and expTime values are ignored, we have to pass them
+        return self._set(&quot;append&quot;, key, val, 0, 0, &quot;&quot;)
+
+
+    def prepend(self, key, val):
+        &quot;&quot;&quot;
+        Prepend given data to the value of an existing key.
+
+        @param key: The key to modify.
+        @type key: C{str}
+
+        @param val: The value to prepend to the current value associated with
+            the key.
+        @type val: C{str}
+
+        @return: A deferred that will fire with C{True} if the operation has
+            succeeded, C{False} otherwise.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        # Even if flags and expTime values are ignored, we have to pass them
+        return self._set(&quot;prepend&quot;, key, val, 0, 0, &quot;&quot;)
+
+
+    def get(self, key, withIdentifier=False):
+        &quot;&quot;&quot;
+        Get the given C{key}. It doesn't support multiple keys. If
+        C{withIdentifier} is set to C{True}, the command issued is a C{gets},
+        that will return the current identifier associated with the value. This
+        identifier has to be used when issuing C{checkAndSet} update later,
+        using the corresponding method.
+
+        @param key: The key to retrieve.
+        @type key: C{str}
+
+        @param withIdentifier: If set to C{True}, retrieve the current
+            identifier along with the value and the flags.
+        @type withIdentifier: C{bool}
+
+        @return: A deferred that will fire with the tuple (flags, value) if
+            C{withIdentifier} is C{False}, or (flags, cas identifier, value)
+            if C{True}.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        if not isinstance(key, str):
+            return fail(ClientError(
+                &quot;Invalid type for key: %s, expecting a string&quot; % (type(key),)))
+        if len(key) &gt; self.MAX_KEY_LENGTH:
+            return fail(ClientError(&quot;Key too long&quot;))
+        if withIdentifier:
+            cmd = &quot;gets&quot;
+        else:
+            cmd = &quot;get&quot;
+        fullcmd = &quot;%s %s&quot; % (cmd, key)
+        self.sendLine(fullcmd)
+        cmdObj = Command(cmd, key=key, value=None, flags=0, cas=&quot;&quot;)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def stats(self):
+        &quot;&quot;&quot;
+        Get some stats from the server. It will be available as a dict.
+
+        @return: a deferred that will fire with a C{dict} of the available
+            statistics.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        self.sendLine(&quot;stats&quot;)
+        cmdObj = Command(&quot;stats&quot;, values={})
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def version(self):
+        &quot;&quot;&quot;
+        Get the version of the server.
+
+        @return: a deferred that will fire with the string value of the
+            version.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        self.sendLine(&quot;version&quot;)
+        cmdObj = Command(&quot;version&quot;)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def delete(self, key):
+        &quot;&quot;&quot;
+        Delete an existing C{key}.
+
+        @param key: the key to delete.
+        @type key: C{str}
+
+        @return: a deferred that will be called back with C{True} if the key
+            was successfully deleted, or C{False} if not.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        if not isinstance(key, str):
+            return fail(ClientError(
+                &quot;Invalid type for key: %s, expecting a string&quot; % (type(key),)))
+        self.sendLine(&quot;delete %s&quot; % key)
+        cmdObj = Command(&quot;delete&quot;, key=key)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+    def flushAll(self):
+        &quot;&quot;&quot;
+        Flush all cached values.
+
+        @return: a deferred that will be called back with C{True} when the
+            operation has succeeded.
+        @rtype: L{Deferred}
+        &quot;&quot;&quot;
+        self.sendLine(&quot;flush_all&quot;)
+        cmdObj = Command(&quot;flush_all&quot;)
+        self._current.append(cmdObj)
+        return cmdObj._deferred
+
+
+
+__all__ = [&quot;MemCacheProtocol&quot;, &quot;DEFAULT_PORT&quot;, &quot;NoSuchCommand&quot;, &quot;ClientError&quot;,
+           &quot;ServerError&quot;]
+
</ins></span></pre></div>
<a id="CalendarServerbranchesunifiedcachetwistedcaldavtesttest_memcachepy"></a>
<div class="addfile"><h4>Added: CalendarServer/branches/unified-cache/twistedcaldav/test/test_memcache.py (0 => 2439)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/unified-cache/twistedcaldav/test/test_memcache.py                                (rev 0)
+++ CalendarServer/branches/unified-cache/twistedcaldav/test/test_memcache.py        2008-05-21 23:11:19 UTC (rev 2439)
</span><span class="lines">@@ -0,0 +1,510 @@
</span><ins>+# Copyright (c) 2007 Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+&quot;&quot;&quot;
+Test the memcache client protocol.
+&quot;&quot;&quot;
+
+from twisted.protocols.memcache import MemCacheProtocol, NoSuchCommand
+from twisted.protocols.memcache import ClientError, ServerError
+
+from twisted.trial.unittest import TestCase
+from twisted.test.proto_helpers import StringTransportWithDisconnection
+from twisted.internet.task import Clock
+from twisted.internet.defer import Deferred, gatherResults, TimeoutError
+
+
+
+class MemCacheTestCase(TestCase):
+    &quot;&quot;&quot;
+    Test client protocol class L{MemCacheProtocol}.
+    &quot;&quot;&quot;
+
+    def setUp(self):
+        &quot;&quot;&quot;
+        Create a memcache client, connect it to a string protocol, and make it
+        use a deterministic clock.
+        &quot;&quot;&quot;
+        self.proto = MemCacheProtocol()
+        self.clock = Clock()
+        self.proto.callLater = self.clock.callLater
+        self.transport = StringTransportWithDisconnection()
+        self.transport.protocol = self.proto
+        self.proto.makeConnection(self.transport)
+
+
+    def _test(self, d, send, recv, result):
+        &quot;&quot;&quot;
+        Shortcut method for classic tests.
+
+        @param d: the resulting deferred from the memcache command.
+        @type d: C{Deferred}
+
+        @param send: the expected data to be sent.
+        @type send: C{str}
+
+        @param recv: the data to simulate as reception.
+        @type recv: C{str}
+
+        @param result: the expected result.
+        @type result: C{any}
+        &quot;&quot;&quot;
+        def cb(res):
+            self.assertEquals(res, result)
+        self.assertEquals(self.transport.value(), send)
+        d.addCallback(cb)
+        self.proto.dataReceived(recv)
+        return d
+
+
+    def test_get(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.get} should return a L{Deferred} which is
+        called back with the value and the flag associated with the given key
+        if the server returns a successful result.
+        &quot;&quot;&quot;
+        return self._test(self.proto.get(&quot;foo&quot;), &quot;get foo\r\n&quot;,
+            &quot;VALUE foo 0 3\r\nbar\r\nEND\r\n&quot;, (0, &quot;bar&quot;))
+
+
+    def test_emptyGet(self):
+        &quot;&quot;&quot;
+        Test getting a non-available key: it should succeed but return C{None}
+        as value and C{0} as flag.
+        &quot;&quot;&quot;
+        return self._test(self.proto.get(&quot;foo&quot;), &quot;get foo\r\n&quot;,
+            &quot;END\r\n&quot;, (0, None))
+
+
+    def test_set(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.set} should return a L{Deferred} which is
+        called back with C{True} when the operation succeeds.
+        &quot;&quot;&quot;
+        return self._test(self.proto.set(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;set foo 0 0 3\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_add(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.add} should return a L{Deferred} which is
+        called back with C{True} when the operation succeeds.
+        &quot;&quot;&quot;
+        return self._test(self.proto.add(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;add foo 0 0 3\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_replace(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.replace} should return a L{Deferred} which
+        is called back with C{True} when the operation succeeds.
+        &quot;&quot;&quot;
+        return self._test(self.proto.replace(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;replace foo 0 0 3\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_errorAdd(self):
+        &quot;&quot;&quot;
+        Test an erroneous add: if a L{MemCacheProtocol.add} is called but the
+        key already exists on the server, it returns a B{NOT STORED} answer,
+        which should callback the resulting L{Deferred} with C{False}.
+        &quot;&quot;&quot;
+        return self._test(self.proto.add(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;add foo 0 0 3\r\nbar\r\n&quot;, &quot;NOT STORED\r\n&quot;, False)
+
+
+    def test_errorReplace(self):
+        &quot;&quot;&quot;
+        Test an erroneous replace: if a L{MemCacheProtocol.replace} is called
+        but the key doesn't exist on the server, it returns a B{NOT STORED}
+        answer, which should callback the resulting L{Deferred} with C{False}.
+        &quot;&quot;&quot;
+        return self._test(self.proto.replace(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;replace foo 0 0 3\r\nbar\r\n&quot;, &quot;NOT STORED\r\n&quot;, False)
+
+
+    def test_delete(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.delete} should return a L{Deferred} which is
+        called back with C{True} when the server notifies a success.
+        &quot;&quot;&quot;
+        return self._test(self.proto.delete(&quot;bar&quot;), &quot;delete bar\r\n&quot;,
+            &quot;DELETED\r\n&quot;, True)
+
+
+    def test_errorDelete(self):
+        &quot;&quot;&quot;
+        Test a error during a delete: if key doesn't exist on the server, it
+        returns a B{NOT FOUND} answer which should callback the resulting
+        L{Deferred} with C{False}.
+        &quot;&quot;&quot;
+        return self._test(self.proto.delete(&quot;bar&quot;), &quot;delete bar\r\n&quot;,
+            &quot;NOT FOUND\r\n&quot;, False)
+
+
+    def test_increment(self):
+        &quot;&quot;&quot;
+        Test incrementing a variable: L{MemCacheProtocol.increment} should
+        return a L{Deferred} which is called back with the incremented value of
+        the given key.
+        &quot;&quot;&quot;
+        return self._test(self.proto.increment(&quot;foo&quot;), &quot;incr foo 1\r\n&quot;,
+            &quot;4\r\n&quot;, 4)
+
+
+    def test_decrement(self):
+        &quot;&quot;&quot;
+        Test decrementing a variable: L{MemCacheProtocol.decrement} should
+        return a L{Deferred} which is called back with the decremented value of
+        the given key.
+        &quot;&quot;&quot;
+        return self._test(
+            self.proto.decrement(&quot;foo&quot;), &quot;decr foo 1\r\n&quot;, &quot;5\r\n&quot;, 5)
+
+
+    def test_incrementVal(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.increment} takes an optional argument C{value} which
+        should replace the default value of 1 when specified.
+        &quot;&quot;&quot;
+        return self._test(self.proto.increment(&quot;foo&quot;, 8), &quot;incr foo 8\r\n&quot;,
+            &quot;4\r\n&quot;, 4)
+
+
+    def test_decrementVal(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.decrement} takes an optional argument C{value} which
+        should replace the default value of 1 when specified.
+        &quot;&quot;&quot;
+        return self._test(self.proto.decrement(&quot;foo&quot;, 3), &quot;decr foo 3\r\n&quot;,
+            &quot;5\r\n&quot;, 5)
+
+
+    def test_stats(self):
+        &quot;&quot;&quot;
+        Test retrieving server statistics via the L{MemCacheProtocol.stats}
+        command: it should parse the data sent by the server and call back the
+        resulting L{Deferred} with a dictionary of the received statistics.
+        &quot;&quot;&quot;
+        return self._test(self.proto.stats(), &quot;stats\r\n&quot;,
+            &quot;STAT foo bar\r\nSTAT egg spam\r\nEND\r\n&quot;,
+            {&quot;foo&quot;: &quot;bar&quot;, &quot;egg&quot;: &quot;spam&quot;})
+
+
+    def test_version(self):
+        &quot;&quot;&quot;
+        Test version retrieval via the L{MemCacheProtocol.version} command: it
+        should return a L{Deferred} which is called back with the version sent
+        by the server.
+        &quot;&quot;&quot;
+        return self._test(self.proto.version(), &quot;version\r\n&quot;,
+            &quot;VERSION 1.1\r\n&quot;, &quot;1.1&quot;)
+
+
+    def test_flushAll(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.flushAll} should return a L{Deferred} which is
+        called back with C{True} if the server acknowledges success.
+        &quot;&quot;&quot;
+        return self._test(self.proto.flushAll(), &quot;flush_all\r\n&quot;,
+            &quot;OK\r\n&quot;, True)
+
+
+    def test_invalidGetResponse(self):
+        &quot;&quot;&quot;
+        If the value returned doesn't match the expected key of the current, we
+        should get an error in L{MemCacheProtocol.dataReceived}.
+        &quot;&quot;&quot;
+        self.proto.get(&quot;foo&quot;)
+        s = &quot;spamegg&quot;
+        self.assertRaises(RuntimeError,
+            self.proto.dataReceived,
+            &quot;VALUE bar 0 %s\r\n%s\r\nEND\r\n&quot; % (len(s), s))
+
+
+    def test_timeOut(self):
+        &quot;&quot;&quot;
+        Test the timeout on outgoing requests: when timeout is detected, all
+        current commands should fail with a L{TimeoutError}, and the
+        connection should be closed.
+        &quot;&quot;&quot;
+        d1 = self.proto.get(&quot;foo&quot;)
+        d2 = self.proto.get(&quot;bar&quot;)
+        d3 = Deferred()
+        self.proto.connectionLost = d3.callback
+
+        self.clock.advance(self.proto.persistentTimeOut)
+        self.assertFailure(d1, TimeoutError)
+        self.assertFailure(d2, TimeoutError)
+        def checkMessage(error):
+            self.assertEquals(str(error), &quot;Connection timeout&quot;)
+        d1.addCallback(checkMessage)
+        return gatherResults([d1, d2, d3])
+
+
+    def test_timeoutRemoved(self):
+        &quot;&quot;&quot;
+        When a request gets a response, no pending timeout call should remain
+        around.
+        &quot;&quot;&quot;
+        d = self.proto.get(&quot;foo&quot;)
+
+        self.clock.advance(self.proto.persistentTimeOut - 1)
+        self.proto.dataReceived(&quot;VALUE foo 0 3\r\nbar\r\nEND\r\n&quot;)
+
+        def check(result):
+            self.assertEquals(result, (0, &quot;bar&quot;))
+            self.assertEquals(len(self.clock.calls), 0)
+        d.addCallback(check)
+        return d
+
+
+    def test_timeOutRaw(self):
+        &quot;&quot;&quot;
+        Test the timeout when raw mode was started: the timeout should not be
+        reset until all the data has been received, so we can have a
+        L{TimeoutError} when waiting for raw data.
+        &quot;&quot;&quot;
+        d1 = self.proto.get(&quot;foo&quot;)
+        d2 = Deferred()
+        self.proto.connectionLost = d2.callback
+
+        self.proto.dataReceived(&quot;VALUE foo 0 10\r\n12345&quot;)
+        self.clock.advance(self.proto.persistentTimeOut)
+        self.assertFailure(d1, TimeoutError)
+        return gatherResults([d1, d2])
+
+
+    def test_timeOutStat(self):
+        &quot;&quot;&quot;
+        Test the timeout when stat command has started: the timeout should not
+        be reset until the final B{END} is received.
+        &quot;&quot;&quot;
+        d1 = self.proto.stats()
+        d2 = Deferred()
+        self.proto.connectionLost = d2.callback
+
+        self.proto.dataReceived(&quot;STAT foo bar\r\n&quot;)
+        self.clock.advance(self.proto.persistentTimeOut)
+        self.assertFailure(d1, TimeoutError)
+        return gatherResults([d1, d2])
+
+
+    def test_timeoutPipelining(self):
+        &quot;&quot;&quot;
+        When two requests are sent, a timeout call should remain around for the
+        second request, and its timeout time should be correct.
+        &quot;&quot;&quot;
+        d1 = self.proto.get(&quot;foo&quot;)
+        d2 = self.proto.get(&quot;bar&quot;)
+        d3 = Deferred()
+        self.proto.connectionLost = d3.callback
+
+        self.clock.advance(self.proto.persistentTimeOut - 1)
+        self.proto.dataReceived(&quot;VALUE foo 0 3\r\nbar\r\nEND\r\n&quot;)
+
+        def check(result):
+            self.assertEquals(result, (0, &quot;bar&quot;))
+            self.assertEquals(len(self.clock.calls), 1)
+            for i in range(self.proto.persistentTimeOut):
+                self.clock.advance(1)
+            return self.assertFailure(d2, TimeoutError).addCallback(checkTime)
+        def checkTime(ignored):
+            # Check that the timeout happened C{self.proto.persistentTimeOut}
+            # after the last response
+            self.assertEquals(self.clock.seconds(),
+                    2 * self.proto.persistentTimeOut - 1)
+        d1.addCallback(check)
+        return d1
+
+
+    def test_timeoutNotReset(self):
+        &quot;&quot;&quot;
+        Check that timeout is not resetted for every command, but keep the
+        timeout from the first command without response.
+        &quot;&quot;&quot;
+        d1 = self.proto.get(&quot;foo&quot;)
+        d3 = Deferred()
+        self.proto.connectionLost = d3.callback
+
+        self.clock.advance(self.proto.persistentTimeOut - 1)
+        d2 = self.proto.get(&quot;bar&quot;)
+        self.clock.advance(1)
+        self.assertFailure(d1, TimeoutError)
+        self.assertFailure(d2, TimeoutError)
+        return gatherResults([d1, d2, d3])
+
+
+    def test_tooLongKey(self):
+        &quot;&quot;&quot;
+        Test that an error is raised when trying to use a too long key: the
+        called command should return a L{Deferred} which fail with a
+        L{ClientError}.
+        &quot;&quot;&quot;
+        d1 = self.assertFailure(self.proto.set(&quot;a&quot; * 500, &quot;bar&quot;), ClientError)
+        d2 = self.assertFailure(self.proto.increment(&quot;a&quot; * 500), ClientError)
+        d3 = self.assertFailure(self.proto.get(&quot;a&quot; * 500), ClientError)
+        d4 = self.assertFailure(self.proto.append(&quot;a&quot; * 500, &quot;bar&quot;), ClientError)
+        d5 = self.assertFailure(self.proto.prepend(&quot;a&quot; * 500, &quot;bar&quot;), ClientError)
+        return gatherResults([d1, d2, d3, d4, d5])
+
+
+    def test_invalidCommand(self):
+        &quot;&quot;&quot;
+        When an unknown command is sent directly (not through public API), the
+        server answers with an B{ERROR} token, and the command should fail with
+        L{NoSuchCommand}.
+        &quot;&quot;&quot;
+        d = self.proto._set(&quot;egg&quot;, &quot;foo&quot;, &quot;bar&quot;, 0, 0, &quot;&quot;)
+        self.assertEquals(self.transport.value(), &quot;egg foo 0 0 3\r\nbar\r\n&quot;)
+        self.assertFailure(d, NoSuchCommand)
+        self.proto.dataReceived(&quot;ERROR\r\n&quot;)
+        return d
+
+
+    def test_clientError(self):
+        &quot;&quot;&quot;
+        Test the L{ClientError} error: when the server send a B{CLIENT_ERROR}
+        token, the originating command should fail with L{ClientError}, and the
+        error should contain the text sent by the server.
+        &quot;&quot;&quot;
+        a = &quot;eggspamm&quot;
+        d = self.proto.set(&quot;foo&quot;, a)
+        self.assertEquals(self.transport.value(),
+                          &quot;set foo 0 0 8\r\neggspamm\r\n&quot;)
+        self.assertFailure(d, ClientError)
+        def check(err):
+            self.assertEquals(str(err), &quot;We don't like egg and spam&quot;)
+        d.addCallback(check)
+        self.proto.dataReceived(&quot;CLIENT_ERROR We don't like egg and spam\r\n&quot;)
+        return d
+
+
+    def test_serverError(self):
+        &quot;&quot;&quot;
+        Test the L{ServerError} error: when the server send a B{SERVER_ERROR}
+        token, the originating command should fail with L{ServerError}, and the
+        error should contain the text sent by the server.
+        &quot;&quot;&quot;
+        a = &quot;eggspamm&quot;
+        d = self.proto.set(&quot;foo&quot;, a)
+        self.assertEquals(self.transport.value(),
+                          &quot;set foo 0 0 8\r\neggspamm\r\n&quot;)
+        self.assertFailure(d, ServerError)
+        def check(err):
+            self.assertEquals(str(err), &quot;zomg&quot;)
+        d.addCallback(check)
+        self.proto.dataReceived(&quot;SERVER_ERROR zomg\r\n&quot;)
+        return d
+
+
+    def test_unicodeKey(self):
+        &quot;&quot;&quot;
+        Using a non-string key as argument to commands should raise an error.
+        &quot;&quot;&quot;
+        d1 = self.assertFailure(self.proto.set(u&quot;foo&quot;, &quot;bar&quot;), ClientError)
+        d2 = self.assertFailure(self.proto.increment(u&quot;egg&quot;), ClientError)
+        d3 = self.assertFailure(self.proto.get(1), ClientError)
+        d4 = self.assertFailure(self.proto.delete(u&quot;bar&quot;), ClientError)
+        d5 = self.assertFailure(self.proto.append(u&quot;foo&quot;, &quot;bar&quot;), ClientError)
+        d6 = self.assertFailure(self.proto.prepend(u&quot;foo&quot;, &quot;bar&quot;), ClientError)
+        return gatherResults([d1, d2, d3, d4, d5, d6])
+
+
+    def test_unicodeValue(self):
+        &quot;&quot;&quot;
+        Using a non-string value should raise an error.
+        &quot;&quot;&quot;
+        return self.assertFailure(self.proto.set(&quot;foo&quot;, u&quot;bar&quot;), ClientError)
+
+
+    def test_pipelining(self):
+        &quot;&quot;&quot;
+        Test that multiple requests can be sent subsequently to the server, and
+        that the protocol order the responses correctly and dispatch to the
+        corresponding client command.
+        &quot;&quot;&quot;
+        d1 = self.proto.get(&quot;foo&quot;)
+        d1.addCallback(self.assertEquals, (0, &quot;bar&quot;))
+        d2 = self.proto.set(&quot;bar&quot;, &quot;spamspamspam&quot;)
+        d2.addCallback(self.assertEquals, True)
+        d3 = self.proto.get(&quot;egg&quot;)
+        d3.addCallback(self.assertEquals, (0, &quot;spam&quot;))
+        self.assertEquals(self.transport.value(),
+            &quot;get foo\r\nset bar 0 0 12\r\nspamspamspam\r\nget egg\r\n&quot;)
+        self.proto.dataReceived(&quot;VALUE foo 0 3\r\nbar\r\nEND\r\n&quot;
+                                &quot;STORED\r\n&quot;
+                                &quot;VALUE egg 0 4\r\nspam\r\nEND\r\n&quot;)
+        return gatherResults([d1, d2, d3])
+
+
+    def test_getInChunks(self):
+        &quot;&quot;&quot;
+        If the value retrieved by a C{get} arrive in chunks, the protocol
+        should be able to reconstruct it and to produce the good value.
+        &quot;&quot;&quot;
+        d = self.proto.get(&quot;foo&quot;)
+        d.addCallback(self.assertEquals, (0, &quot;0123456789&quot;))
+        self.assertEquals(self.transport.value(), &quot;get foo\r\n&quot;)
+        self.proto.dataReceived(&quot;VALUE foo 0 10\r\n0123456&quot;)
+        self.proto.dataReceived(&quot;789&quot;)
+        self.proto.dataReceived(&quot;\r\nEND&quot;)
+        self.proto.dataReceived(&quot;\r\n&quot;)
+        return d
+
+
+    def test_append(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.append} behaves like a L{MemCacheProtocol.set}
+        method: it should return a L{Deferred} which is called back with
+        C{True} when the operation succeeds.
+        &quot;&quot;&quot;
+        return self._test(self.proto.append(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;append foo 0 0 3\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_prepend(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.prepend} behaves like a L{MemCacheProtocol.set}
+        method: it should return a L{Deferred} which is called back with
+        C{True} when the operation succeeds.
+        &quot;&quot;&quot;
+        return self._test(self.proto.prepend(&quot;foo&quot;, &quot;bar&quot;),
+            &quot;prepend foo 0 0 3\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_gets(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.get} should handle an additional cas result when
+        C{withIdentifier} is C{True} and forward it in the resulting
+        L{Deferred}.
+        &quot;&quot;&quot;
+        return self._test(self.proto.get(&quot;foo&quot;, True), &quot;gets foo\r\n&quot;,
+            &quot;VALUE foo 0 3 1234\r\nbar\r\nEND\r\n&quot;, (0, &quot;1234&quot;, &quot;bar&quot;))
+
+
+    def test_emptyGets(self):
+        &quot;&quot;&quot;
+        Test getting a non-available key with gets: it should succeed but
+        return C{None} as value, C{0} as flag and an empty cas value.
+        &quot;&quot;&quot;
+        return self._test(self.proto.get(&quot;foo&quot;, True), &quot;gets foo\r\n&quot;,
+            &quot;END\r\n&quot;, (0, &quot;&quot;, None))
+
+
+    def test_checkAndSet(self):
+        &quot;&quot;&quot;
+        L{MemCacheProtocol.checkAndSet} passes an additional cas identifier that the
+        server should handle to check if the data has to be updated.
+        &quot;&quot;&quot;
+        return self._test(self.proto.checkAndSet(&quot;foo&quot;, &quot;bar&quot;, cas=&quot;1234&quot;),
+            &quot;cas foo 0 0 3 1234\r\nbar\r\n&quot;, &quot;STORED\r\n&quot;, True)
+
+
+    def test_casUnknowKey(self):
+        &quot;&quot;&quot;
+        When L{MemCacheProtocol.checkAndSet} response is C{EXISTS}, the resulting
+        L{Deferred} should fire with C{False}.
+        &quot;&quot;&quot;
+        return self._test(self.proto.checkAndSet(&quot;foo&quot;, &quot;bar&quot;, cas=&quot;1234&quot;),
+            &quot;cas foo 0 0 3 1234\r\nbar\r\n&quot;, &quot;EXISTS\r\n&quot;, False)
</ins></span></pre>
</div>
</div>

</body>
</html>