[Checkins] SVN: zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/ added test case for tracing application errors.

Alex Smith asmith at zope.com
Tue Sep 2 12:31:55 EDT 2008


Log message for revision 90716:
  
  added test case for tracing application errors.
  

Changed:
  U   zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/README.txt
  U   zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tests.py
  U   zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tracelog.py

-=-
Modified: zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/README.txt
===================================================================
--- zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/README.txt	2008-09-02 16:01:10 UTC (rev 90715)
+++ zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/README.txt	2008-09-02 16:31:55 UTC (rev 90716)
@@ -25,18 +25,17 @@
     ...     zope.app.appsetup.interfaces.ProcessStarting())
     S 0 2008-08-26T11:55:00
 
-The tracelog machinery is implemented as a WSGI layer, so we'll define a fake
-WSGI application for tracelog to use.
+The tracelog machinery is implemented as a WSGI layer, so we'll pass a fake
+WSGI application to tracelog for these examples.
 
-    >>> def faux_application(environ, start_response):
-    ...     """Fake WSGI application.  Doesn't need to do much!"""
+    >>> faux_app = FauxApplication()
 
 Now, let's create an instance of the tracelog server.
 
     >>> addr, port = '127.0.0.1', 12345
 
     >>> trace_server = zc.zservertracelog.tracelog.Server(
-    ...     faux_application, None, addr, port)
+    ...     faux_app, None, addr, port)
 
 Let's also define a convenience function for processing requests.
 
@@ -47,14 +46,41 @@
 Process a simple request.
 
     >>> req1 = """\
-    ... GET / HTTP/1.1
+    ... GET /test-req1 HTTP/1.1
     ... Host: www.example.com
     ...
     ... """
 
     >>> invokeRequest(req1)
-    B 23423600 2008-08-27T10:54:08 GET /
+    B 23423600 2008-08-27T10:54:08 GET /test-req1
     I 23423600 2008-08-27T10:54:08 0
     C 23423600 2008-08-27T10:54:08
     A 23423600 2008-08-27T10:54:08 200 ?
     E 23423600 2008-08-27T10:54:08
+
+
+The tracelog will also log application errors.  To show this, we'll set up
+our test application to raise an error when called.
+
+    >>> def test_failure(*args, **kwargs):
+    ...     raise Exception('oh noes!')
+    >>> faux_app.app_hook = test_failure
+
+We can see that all trace points were hit and that the error was written to
+the log.
+
+    >>> try:
+    ...     invokeRequest(req1)
+    ... except:
+    ...     pass
+    B 21663984 2008-09-02T11:19:26 GET /test-req1
+    I 21663984 2008-09-02T11:19:26 0
+    C 21663984 2008-09-02T11:19:26
+    A 21663984 2008-09-02T11:19:26 Error: oh noes!
+    E 21663984 2008-09-02T11:19:26
+
+
+TODO
+====
+
+  * show a task write exception

Modified: zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tests.py
===================================================================
--- zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tests.py	2008-09-02 16:01:10 UTC (rev 90715)
+++ zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tests.py	2008-09-02 16:31:55 UTC (rev 90716)
@@ -29,6 +29,23 @@
     ])
 
 
+_null_app = lambda environ, start_response: None
+
+
+class FauxApplication(object):
+    """Fake WSGI application.  Doesn't need to do much!"""
+
+    app_hook = None
+
+    def __call__(self, environ, start_response):
+        app = self.app_hook or _null_app
+        return app(environ, start_response)
+
+
+def setUp(test):
+    test.globs['FauxApplication'] = FauxApplication
+
+
 def test_suite():
     return unittest.TestSuite([
         doctest.DocFileTest(
@@ -38,5 +55,6 @@
                 | doctest.ELLIPSIS
                 | doctest.INTERPRET_FOOTNOTES),
             checker=checker,
+            setUp=setUp,
             ),
         ])

Modified: zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tracelog.py
===================================================================
--- zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tracelog.py	2008-09-02 16:01:10 UTC (rev 90715)
+++ zc.zservertracelog/branches/alex-tests/src/zc/zservertracelog/tracelog.py	2008-09-02 16:31:55 UTC (rev 90716)
@@ -77,8 +77,7 @@
             logger.info("E %s %s", id(task.channel), now())
             raise
         else:
-            accumulated_headers = (
-                getattr(task, 'accumulated_headers', ()) or ())
+            accumulated_headers = getattr(task, 'accumulated_headers') or ()
             length = [h.split(': ')[1].strip()
                       for h in accumulated_headers
                       if h.lower().startswith('content-length: ')]



More information about the Checkins mailing list