[CalendarServer-changes] [9083] CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Fri Apr 13 11:46:22 PDT 2012


Revision: 9083
          http://trac.macosforge.org/projects/calendarserver/changeset/9083
Author:   glyph at apple.com
Date:     2012-04-13 11:46:22 -0700 (Fri, 13 Apr 2012)
Log Message:
-----------
Replace connectTCP in the memcache pool with endpoints, and make the tests a bit less overly strict to accommodate the change.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/memcachepool.py
    CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/test/test_memcachepool.py

Modified: CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/memcachepool.py
===================================================================
--- CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/memcachepool.py	2012-04-13 18:46:17 UTC (rev 9082)
+++ CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/memcachepool.py	2012-04-13 18:46:22 UTC (rev 9083)
@@ -15,10 +15,13 @@
 ##
 
 from twisted.python.failure import Failure
-from twisted.internet.address import IPv4Address
+
 from twisted.internet.defer import Deferred, fail
 from twisted.internet.protocol import ReconnectingClientFactory
 
+from twext.internet.gaiendpoint import GAIEndpoint
+from twext.internet.adaptendpoint import connect
+
 from twext.python.log import LoggingMixIn
 from twext.protocols.memcache import MemCacheProtocol, NoSuchCommand
 
@@ -116,13 +119,17 @@
         for each protocol.
 
     @ivar _maxClients: A C{int} indicating the maximum number of clients.
-    @ivar _serverAddress: An L{IAddress} provider indicating the server to
-        connect to.  (Only L{IPv4Address} currently supported.)
+
+    @ivar _endpoint: An L{IStreamClientEndpoint} provider indicating the server
+        to connect to.
+
     @ivar _reactor: The L{IReactorTCP} provider used to initiate new
         connections.
 
     @ivar _busyClients: A C{set} that contains all currently busy clients.
+
     @ivar _freeClients: A C{set} that contains all currently free clients.
+
     @ivar _pendingConnects: A C{int} indicating how many connections are in
         progress.
     """
@@ -130,15 +137,17 @@
 
     REQUEST_LOGGING_SIZE = 1024
 
-    def __init__(self, serverAddress, maxClients=5, reactor=None):
+    def __init__(self, endpoint, maxClients=5, reactor=None):
         """
-        @param serverAddress: An L{IPv4Address} indicating the server to
+        @param endpoint: An L{IStreamClientEndpoint} indicating the server to
             connect to.
+
         @param maxClients: A C{int} indicating the maximum number of clients.
-        @param reactor: An L{IReactorTCP{ provider used to initiate new
+
+        @param reactor: An L{IReactorTCP} provider used to initiate new
             connections.
         """
-        self._serverAddress = serverAddress
+        self._endpoint = endpoint
         self._maxClients = maxClients
 
         if reactor is None:
@@ -175,7 +184,7 @@
         @return: A L{Deferred} that fires with the L{IProtocol} instance.
         """
         self.log_debug("Initating new client connection to: %r" % (
-                self._serverAddress,))
+                self._endpoint,))
         self._logClientStats()
 
         self._pendingConnects += 1
@@ -190,9 +199,7 @@
 
         factory.connectionPool = self
 
-        self._reactor.connectTCP(self._serverAddress.host,
-                                 self._serverAddress.port,
-                                 factory)
+        connect(self._endpoint, factory)
         d = factory.deferred
 
         d.addCallback(_connected)
@@ -404,31 +411,29 @@
 _memCachePoolHandler = {}   # Maps a handler id to a named pool
 
 def installPools(pools, maxClients=5, reactor=None):
-    
+    if reactor is None:
+        from twisted.internet import reactor
     for name, pool in pools.items():
         if pool["ClientEnabled"]:
             _installPool(
                 name,
                 pool["HandleCacheTypes"],
-                IPv4Address(
-                    "TCP",
-                    pool["BindAddress"],
-                    pool["Port"],
-                ),
+                GAIEndpoint(reactor, pool["BindAddress"], pool["Port"]),
                 maxClients,
                 reactor,
             )
 
-def _installPool(name, handleTypes, serverAddress, maxClients=5, reactor=None):
 
-    pool = MemCachePool(serverAddress,
-                                 maxClients=maxClients,
-                                 reactor=None)
+
+def _installPool(name, handleTypes, serverEndpoint, maxClients=5, reactor=None):
+    pool = MemCachePool(serverEndpoint, maxClients=maxClients, reactor=None)
     _memCachePools[name] = pool
 
     for handle in handleTypes:
         _memCachePoolHandler[handle] = pool
 
+
+
 def defaultCachePool(name):
     if name not in _memCachePoolHandler:
         name = "Default"

Modified: CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/test/test_memcachepool.py
===================================================================
--- CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/test/test_memcachepool.py	2012-04-13 18:46:17 UTC (rev 9082)
+++ CalendarServer/branches/users/glyph/ipv6-client/twistedcaldav/test/test_memcachepool.py	2012-04-13 18:46:22 UTC (rev 9083)
@@ -17,6 +17,7 @@
 from zope.interface import implements
 
 from twisted.internet.interfaces import IConnector, IReactorTCP
+from twisted.internet.endpoints import TCP4ClientEndpoint
 from twisted.internet.address import IPv4Address
 
 from twistedcaldav.test.util import InMemoryMemcacheProtocol
@@ -203,10 +204,19 @@
         """
         TestCase.setUp(self)
         self.reactor = StubReactor()
-        self.pool = MemCachePool(MC_ADDRESS,
-                                 maxClients=5,
-                                 reactor=self.reactor)
+        self.pool = MemCachePool(
+            TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port),
+            maxClients=5, reactor=self.reactor
+        )
+        realClientFactory = self.pool.clientFactory
+        self.clientFactories = []
+        def capturingClientFactory(*a, **k):
+            cf = realClientFactory(*a, **k)
+            self.clientFactories.append(cf)
+            return cf
+        self.pool.clientFactory = capturingClientFactory
 
+
     def test_clientFreeAddsNewClient(self):
         """
         Test that a client not in the busy set gets added to the free set.
@@ -286,27 +296,21 @@
         Test that L{MemCachePool.performRequest} on a fresh instance causes
         a new connection to be created.
         """
-        def _checkResult(result):
-            self.assertEquals(result, (0, 'bar'))
-
-
+        results = []
         p = InMemoryMemcacheProtocol()
         p.set('foo', 'bar')
 
         d = self.pool.performRequest('get', 'foo')
-        d.addCallback(_checkResult)
+        d.addCallback(results.append)
 
         args, kwargs = self.reactor.calls.pop()
 
         self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
-        self.failUnless(isinstance(args[2], MemCacheClientFactory))
-        self.assertEquals(kwargs, {})
 
-        args[2].deferred.callback(p)
+        self.clientFactories[-1].deferred.callback(p)
+        self.assertEquals(results, [(0, 'bar')])
 
-        return d
 
-
     def test_performRequestUsesFreeConnection(self):
         """
         Test that L{MemCachePool.performRequest} doesn't create a new connection
@@ -323,7 +327,6 @@
 
         d = self.pool.performRequest('get', 'foo')
         d.addCallback(_checkResult)
-
         return d
 
 
@@ -374,19 +377,13 @@
 
         self.pool.clientBusy(p)
 
-        d = self.pool.performRequest('get', 'foo')
+        self.pool.performRequest('get', 'foo')
 
         args, kwargs = self.reactor.calls.pop()
 
         self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
-        self.failUnless(isinstance(args[2], MemCacheClientFactory))
-        self.assertEquals(kwargs, {})
 
-        args[2].deferred.callback(p1)
 
-        return d
-
-
     def test_pendingConnectionsCountAgainstMaxClients(self):
         """
         Test that L{MemCachePool.performRequest} will not initiate a new
@@ -395,17 +392,11 @@
         """
         self.pool.suggestMaxClients(1)
 
-        d = self.pool.performRequest('get', 'foo')
+        self.pool.performRequest('get', 'foo')
 
         args, kwargs = self.reactor.calls.pop()
 
         self.assertEquals(args[:2], (MC_ADDRESS.host, MC_ADDRESS.port))
-        self.failUnless(isinstance(args[2], MemCacheClientFactory))
-        self.assertEquals(kwargs, {})
 
         self.pool.performRequest('get', 'bar')
         self.assertEquals(self.reactor.calls, [])
-
-        args[2].deferred.callback(InMemoryMemcacheProtocol())
-
-        return d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120413/992df927/attachment.html>


More information about the calendarserver-changes mailing list