Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1551

Show
Ignore:
Timestamp:
12/21/06 17:43:44
Author:
fumanchu
Message:

Cleaner test_tools.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/test_tools.py

    r1542 r1551  
    33import gzip, StringIO 
    44import time 
     5timeout = 0.2 
     6 
    57import types 
    68from cherrypy.test import test 
     
    5153         
    5254        def __init__(self): 
    53             self.counter = 0 
    5455            self.ended = {} 
    5556            self._name = "nadsat" 
    56              
     57         
    5758        def nadsat(self): 
    5859            def nadsat_it_up(body): 
     
    6768            # This runs after the request has been completely written out. 
    6869            cherrypy.response.body = "razdrez" 
    69             self.ended[cherrypy.request.counter] = True 
     70            id = cherrypy.request.params.get("id") 
     71            if id: 
     72                self.ended[id] = True 
    7073        cleanup.failsafe = True 
    7174         
    7275        def _setup(self): 
    73             cherrypy.request.counter = self.counter = self.counter + 1 
    74             self.ended[cherrypy.request.counter] = False 
    7576            cherrypy.request.hooks.attach('before_finalize', self.nadsat) 
    7677            cherrypy.request.hooks.attach('on_end_request', self.cleanup) 
     
    136137        _cp_config = {"tools.nadsat.on": True} 
    137138         
    138         def index(self): 
     139        def index(self, id=None): 
    139140            return "A good piece of cherry pie" 
    140141         
    141142        def ended(self, id): 
    142             return repr(tools.nadsat.ended[int(id)]) 
    143          
    144         def err(self): 
     143            return repr(tools.nadsat.ended[id]) 
     144         
     145        def err(self, id=None): 
    145146            raise ValueError() 
    146147         
    147         def errinstream(self): 
     148        def errinstream(self, id=None): 
    148149            raise ValueError() 
    149150            yield "confidential" 
     
    160161            return "success!" 
    161162         
    162         def stream(self): 
     163        def stream(self, id=None): 
    163164            for x in xrange(100000000): 
    164165                yield str(x) 
     
    209210class ToolTests(helper.CPWebCase): 
    210211     
    211     def testDemo(self): 
    212         self.getPage("/demo/") 
     212    def testHookErrors(self): 
     213        self.getPage("/demo/?id=1") 
    213214        # If body is "razdrez", then on_end_request is being called too early. 
    214215        self.assertBody("A horrorshow lomtick of cherry 3.14159") 
     
    219220         
    220221        valerr = '\n    raise ValueError()\nValueError' 
    221         self.getPage("/demo/err") 
     222        self.getPage("/demo/err?id=3") 
    222223        # If body is "razdrez", then on_end_request is being called too early. 
    223224        self.assertErrorPage(502, pattern=valerr) 
     
    228229         
    229230        # If body is "razdrez", then on_end_request is being called too early. 
    230         self.getPage("/demo/errinstream") 
     231        self.getPage("/demo/errinstream?id=5") 
    231232        # Because this error is raised after the response body has 
    232233        # started, the status should not change to an error status. 
     
    245246        self.getPage("/demo/userid") 
    246247        self.assertBody("Welcome!") 
    247          
    248         # Test that on_end_request is called even if the client drops. 
    249         self.persistent = Tru
     248     
     249    def testEndRequestOnDrop(self): 
     250        old_timeout = Non
    250251        try: 
    251             conn = self.HTTP_CONN 
    252             conn.putrequest("GET", "/demo/stream", skip_host=True) 
    253             conn.putheader("Host", self.HOST) 
    254             conn.endheaders() 
    255             # Skip the rest of the request and close the conn. This will 
    256             # cause the server's active socket to error, which *should* 
    257             # result in the request being aborted, and request.close being 
    258             # called all the way up the stack (including WSGI middleware), 
    259             # eventually calling our on_end_request hook. 
     252            httpserver = cherrypy.server.httpservers.keys()[0] 
     253            old_timeout = httpserver.timeout 
     254        except (AttributeError, IndexError): 
     255            print "skipped ", 
     256            return 
     257         
     258        try: 
     259            httpserver.timeout = timeout 
     260             
     261            # Test that on_end_request is called even if the client drops. 
     262            self.persistent = True 
     263            try: 
     264                conn = self.HTTP_CONN 
     265                conn.putrequest("GET", "/demo/stream?id=9", skip_host=True) 
     266                conn.putheader("Host", self.HOST) 
     267                conn.endheaders() 
     268                # Skip the rest of the request and close the conn. This will 
     269                # cause the server's active socket to error, which *should* 
     270                # result in the request being aborted, and request.close being 
     271                # called all the way up the stack (including WSGI middleware), 
     272                # eventually calling our on_end_request hook. 
     273            finally: 
     274                self.persistent = False 
     275            time.sleep(timeout * 2) 
     276            # Test that the on_end_request hook was called. 
     277            self.getPage("/demo/ended/9") 
     278            self.assertBody("True") 
    260279        finally: 
    261             self.persistent = False 
    262         time.sleep(0.1) 
    263         # Test that the on_end_request hooks was called. 
    264         self.getPage("/demo/ended/9") 
    265         self.assertBody("True") 
     280            if old_timeout is not None: 
     281                httpserver.timeout = old_timeout 
    266282     
    267283    def testGuaranteedHooks(self): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets