[CalendarServer-changes] [11467] CalendarServer/branches/users/glyph/hang-fix/twext/web2

source_changes at macosforge.org source_changes at macosforge.org
Fri Jul 5 17:42:53 PDT 2013


Revision: 11467
          http://trac.calendarserver.org//changeset/11467
Author:   glyph at apple.com
Date:     2013-07-05 17:42:53 -0700 (Fri, 05 Jul 2013)
Log Message:
-----------
Add a unit test for the case of statusFromMessage that already works; refactor
the code to be functionally identical, but have less indentation and be easier
to read.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/hang-fix/twext/web2/metafd.py
    CalendarServer/branches/users/glyph/hang-fix/twext/web2/test/test_metafd.py

Modified: CalendarServer/branches/users/glyph/hang-fix/twext/web2/metafd.py
===================================================================
--- CalendarServer/branches/users/glyph/hang-fix/twext/web2/metafd.py	2013-07-06 00:42:51 UTC (rev 11466)
+++ CalendarServer/branches/users/glyph/hang-fix/twext/web2/metafd.py	2013-07-06 00:42:53 UTC (rev 11467)
@@ -173,13 +173,14 @@
         self.dispatcher.startDispatching()
 
 
-    def addPortService(self, description, port, interface, backlog):
+    def addPortService(self, description, port, interface, backlog,
+                       serverServiceMaker=MaxAcceptTCPServer):
         """
         Add a L{MaxAcceptTCPServer} to bind a TCP port to a socket description.
         """
         lipf = LimitingInheritingProtocolFactory(self, description)
         self.factories.append(lipf)
-        MaxAcceptTCPServer(
+        serverServiceMaker(
             port, lipf,
             interface=interface,
             backlog=backlog
@@ -194,25 +195,26 @@
         Determine a subprocess socket's status from its previous status and a
         status message.
         """
-        if message in ('-', '0'):
-            if message == '-':
-                # A connection has gone away in a subprocess; we should start
-                # accepting connections again if we paused (see
-                # newConnectionStatus)
-                result = self.intWithNoneAsZero(previousStatus) - 1
-                if result < 0:
-                    log.error("metafd: trying to decrement status below zero")
-                    result = 0
+        if message == '-':
+            # A connection has gone away in a subprocess; we should start
+            # accepting connections again if we paused (see
+            # newConnectionStatus)
+            result = self.intWithNoneAsZero(previousStatus) - 1
+            if result < 0:
+                log.error("metafd: trying to decrement status below zero")
+                result = 0
+        elif message == '0':
+            # A new process just started accepting new connections; zero
+            # out its expected load, but only if previous status is still
+            # None
+            result = 0 if previousStatus is None else previousStatus
+            if previousStatus is None:
+                result = 0
             else:
-                # A new process just started accepting new connections; zero
-                # out its expected load, but only if previous status is still None
-                result = 0 if previousStatus is None else previousStatus
-                if previousStatus is None:
-                    result = 0
-                else:
-                    log.error("metafd: trying to zero status that is not None")
-                    result = previousStatus
+                log.error("metafd: trying to zero status that is not None")
+                result = previousStatus
 
+        if message in ('-', '0'):
             # If load has indeed decreased (i.e. in any case except 'a new,
             # idle process replaced an old, idle process'), then start
             # listening again.

Modified: CalendarServer/branches/users/glyph/hang-fix/twext/web2/test/test_metafd.py
===================================================================
--- CalendarServer/branches/users/glyph/hang-fix/twext/web2/test/test_metafd.py	2013-07-06 00:42:51 UTC (rev 11466)
+++ CalendarServer/branches/users/glyph/hang-fix/twext/web2/test/test_metafd.py	2013-07-06 00:42:53 UTC (rev 11467)
@@ -27,6 +27,7 @@
 from twext.web2.channel.http import HTTPChannel
 from twext.web2.metafd import ReportingHTTPService, ConnectionLimiter
 from twisted.internet.tcp import Server
+from twisted.application.service import Service
 from twisted.trial.unittest import TestCase
 
 
@@ -153,8 +154,11 @@
     
     def test_statusFromMessage(self):
         """
-        Test ConnectionLimiter.statusFromMessage to make sure count cannot go below zero and that
-        zeroing out does not wipe out an existing count.
+        L{ConnectionLimiter.statusFromMessage} will not return a value below
+        zero, and a "0" status - a new process reporting its launching - will
+        leave the previous status value the same, on the assumption that the 0
+        is simply being reported late, and the master has given the worker some
+        work to do before it reports its initial status.
         """
         
         cl = ConnectionLimiter(2, 20)
@@ -177,3 +181,35 @@
         self.assertEqual(cl.statusFromMessage(1, "+"), 1)
         self.assertEqual(cl.statusFromMessage(2, "+"), 2)
 
+
+    def test_statusFromMessageStartsReadingAgain(self):
+        """
+        L{ConnectionLimiter.statusFromMessage} determines whether load has gone
+        up or down based on the previous status being compared to, and, if load
+        has gone down, will always start reading again.
+        """
+        cl = ConnectionLimiter(2, 20)
+        s = Service()
+
+        class NotAPort(object):
+            def startReading(self):
+                self.reading = True
+            def stopReading(self):
+                self.reading = False
+
+        def serverServiceMaker(port, factory, *a, **k):
+            s.factory = factory
+            s.myPort = NotAPort()
+            factory.myServer = s
+            return s
+
+        cl.addPortService("TCP", 4321, "127.0.0.1", 5, serverServiceMaker)
+        # Has to be running in order to add stuff.
+        cl.startService()
+        # Make sure it's stopped.
+        s.myPort.stopReading()
+        self.assertEquals(s.myPort.reading, False) # sanity check
+        cl.statusFromMessage(2, "-")
+        self.assertEquals(s.myPort.reading, True)
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130705/fb14a705/attachment-0001.html>


More information about the calendarserver-changes mailing list