[Checkins] SVN: Sandbox/J1m/resumelb/s The history setting is provided via a settings dictionary to allow the

Jim Fulton jim at zope.com
Wed Dec 28 18:03:11 UTC 2011


Log message for revision 123867:
  The history setting is provided via a settings dictionary to allow the
  setting to be changed externally.
  
  Use sha1, rather than hash to get a stable hash for the tests.
  

Changed:
  U   Sandbox/J1m/resumelb/setup.py
  U   Sandbox/J1m/resumelb/src/zc/resumelb/tests.py
  U   Sandbox/J1m/resumelb/src/zc/resumelb/worker.py
  U   Sandbox/J1m/resumelb/src/zc/resumelb/worker.test

-=-
Modified: Sandbox/J1m/resumelb/setup.py
===================================================================
--- Sandbox/J1m/resumelb/setup.py	2011-12-27 17:50:03 UTC (rev 123866)
+++ Sandbox/J1m/resumelb/setup.py	2011-12-28 18:03:10 UTC (rev 123867)
@@ -13,7 +13,8 @@
 ##############################################################################
 name, version = 'zc.resumelb', '0'
 
-install_requires = ['setuptools', 'gevent', 'WebOb', 'zc.thread']
+install_requires = [
+    'setuptools', 'gevent', 'WebOb', 'zc.thread', 'zc.mappingobject']
 extras_require = dict(
     test=['zope.testing', 'bobo', 'manuel', 'WebTest'])
 

Modified: Sandbox/J1m/resumelb/src/zc/resumelb/tests.py
===================================================================
--- Sandbox/J1m/resumelb/src/zc/resumelb/tests.py	2011-12-27 17:50:03 UTC (rev 123866)
+++ Sandbox/J1m/resumelb/src/zc/resumelb/tests.py	2011-12-28 18:03:10 UTC (rev 123867)
@@ -14,6 +14,7 @@
 import bobo
 import doctest
 import gevent
+import hashlib
 import manuel.capture
 import manuel.doctest
 import manuel.testing
@@ -28,7 +29,7 @@
 def hi(request):
     body = request.environ['wsgi.input'].read()
     return "\n\n%s -> %s %s %s\n\n" % (
-        request.url, pid, len(body), hash(body))
+        request.url, pid, len(body), hashlib.sha1(body).hexdigest())
 
 @bobo.query('/gen.html')
 def gen(size=0):

Modified: Sandbox/J1m/resumelb/src/zc/resumelb/worker.py
===================================================================
--- Sandbox/J1m/resumelb/src/zc/resumelb/worker.py	2011-12-27 17:50:03 UTC (rev 123866)
+++ Sandbox/J1m/resumelb/src/zc/resumelb/worker.py	2011-12-28 18:03:10 UTC (rev 123867)
@@ -7,16 +7,17 @@
 import socket
 import sys
 import time
+import zc.mappingobject
 import zc.resumelb.util
 
 logger = logging.getLogger(__name__)
 
 class Worker(zc.resumelb.util.Worker):
 
-    def __init__(self, app, addr, history):
+    def __init__(self, app, addr, settings):
         self.app = app
+        self.settings = zc.mappingobject.mappingobject(settings)
         self.resume = {}
-        self.time_ring_size = history
         self.time_ring = []
         self.time_ring_pos = 0
         while 1:
@@ -80,7 +81,7 @@
 
             elapsed = max(time.time() - env['zc.resumelb.time'], 1e-9)
             time_ring = self.time_ring
-            time_ring_pos = rno % self.time_ring_size
+            time_ring_pos = rno % self.settings.history
             rclass = env['zc.resumelb.request_class']
             try:
                 time_ring[time_ring_pos] = rclass, elapsed
@@ -88,7 +89,7 @@
                 while len(time_ring) <= time_ring_pos:
                     time_ring.append((rclass, elapsed))
 
-            if rno % self.time_ring_size == 0:
+            if rno % self.settings.history == 0:
                 byrclass = {}
                 for rclass, elapsed in time_ring:
                     sumn = byrclass.get(rclass)
@@ -109,5 +110,5 @@
 def server_runner(app, global_conf, lb, history=500): # paste deploy hook
     logging.basicConfig(level=logging.INFO)
     host, port = lb.split(':')
-    Worker(app, (host, int(port)), history)
+    Worker(app, (host, int(port)), dict(history=history))
 

Modified: Sandbox/J1m/resumelb/src/zc/resumelb/worker.test
===================================================================
--- Sandbox/J1m/resumelb/src/zc/resumelb/worker.test	2011-12-27 17:50:03 UTC (rev 123866)
+++ Sandbox/J1m/resumelb/src/zc/resumelb/worker.test	2011-12-28 18:03:10 UTC (rev 123867)
@@ -20,7 +20,7 @@
     ...   zc.resumelb.worker.Worker,
     ...   zc.resumelb.tests.app(),
     ...   ('127.0.0.1', server_port),
-    ...   history=5)
+    ...   dict(history=5))
 
 Here we created a worker using a test application, telling it to
 connect to our server address and to update it's resume after every
@@ -102,12 +102,12 @@
 
     >>> print_response(worker_socket, 1)
     1 200 OK
-    Content-Length: 40
+    Content-Length: 79
     Content-Type: text/html; charset=UTF-8; charset=UTF-8
     <BLANKLINE>
     <BLANKLINE>
     <BLANKLINE>
-    http://localhost/hi.html -> 6115 0 0
+    http://localhost/hi.html -> 6115 0 da39a3ee5e6b4b0d3255bfef95601890afd80709
     <BLANKLINE>
 
 We can have multiple outstanding requests:
@@ -125,9 +125,16 @@
     >>> b3 = 'y'*10000
     >>> b4 = 'z'*100000
 
-    >>> hash(b2), hash(b3), hash(b4)
-    (8412291732507076776, 5203428436375121216, -3961752530033413152)
+    >>> import hashlib
+    >>> sha1 = lambda s: hashlib.sha1(s).hexdigest()
 
+    >>> sha1(b2)
+    'c3efa690fa3fdd2e2526853eed670538ea127638'
+    >>> sha1(b3)
+    'c1d5e830a1027a7b5de9e0620f3a2497d6b60c3e'
+    >>> sha1(b4)
+    '3235771c66bf77697df635e1bce4173668d2ea32'
+
 and send them:
 
     >>> write_message(worker_socket, 2, b2)
@@ -139,36 +146,39 @@
     ...     write_message(worker_socket, 4, b4[i*10000:(i+1)*10000])
 
     >>> write_message(worker_socket, 4, '')
-    >>> print_response(worker_socket, 4)
+    >>> print_response(worker_socket, 4) # doctest: +NORMALIZE_WHITESPACE
     4 200 OK
-    Content-Length: 64
+    Content-Length: 84
     Content-Type: text/html; charset=UTF-8; charset=UTF-8
     <BLANKLINE>
     <BLANKLINE>
     <BLANKLINE>
-    http://localhost/hi.html -> 6115 100000 -3961752530033413152
+    http://localhost/hi.html ->
+    6115 100000 3235771c66bf77697df635e1bce4173668d2ea32
     <BLANKLINE>
 
     >>> write_message(worker_socket, 2, '')
-    >>> print_response(worker_socket, 2)
+    >>> print_response(worker_socket, 2) # doctest: +NORMALIZE_WHITESPACE
     2 200 OK
-    Content-Length: 61
+    Content-Length: 82
     Content-Type: text/html; charset=UTF-8; charset=UTF-8
     <BLANKLINE>
     <BLANKLINE>
     <BLANKLINE>
-    http://localhost/hi.html -> 6115 1000 8412291732507076776
+    http://localhost/hi.html ->
+    6115 1000 c3efa690fa3fdd2e2526853eed670538ea127638
     <BLANKLINE>
 
     >>> write_message(worker_socket, 3, '')
-    >>> print_response(worker_socket, 3)
+    >>> print_response(worker_socket, 3) # doctest: +NORMALIZE_WHITESPACE
     3 200 OK
-    Content-Length: 62
+    Content-Length: 83
     Content-Type: text/html; charset=UTF-8; charset=UTF-8
     <BLANKLINE>
     <BLANKLINE>
     <BLANKLINE>
-    http://localhost/hi.html -> 6115 10000 5203428436375121216
+    http://localhost/hi.html ->
+    6115 10000 c1d5e830a1027a7b5de9e0620f3a2497d6b60c3e
     <BLANKLINE>
 
 The handler for hi.html outputs the url, the pid, the request body



More information about the checkins mailing list