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

Changeset 709

Show
Ignore:
Timestamp:
10/03/05 12:01:29
Author:
fumanchu
Message:

More state tests (onStart/Stop tests). Also made it more explicit that onStop's are called even if there's no HTTP server.

Files:

Legend:

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

    r704 r709  
    222222        """Stop, including any HTTP servers.""" 
    223223        self.stop_http_server() 
     224         
     225        for thread_ident, i in seen_threads.iteritems(): 
     226            for func in self.onStopThreadList: 
     227                func(i) 
     228        seen_threads.clear() 
     229         
     230        for func in self.onStopServerList: 
     231            func() 
     232         
    224233        self.state = STOPPED 
    225234        cherrypy.log("CherryPy shut down", "HTTP") 
     
    235244            httpstop() 
    236245            cherrypy.log("HTTP Server shut down", "HTTP") 
    237          
    238         for thread_ident, i in seen_threads.iteritems(): 
    239             for func in self.onStopThreadList: 
    240                 func(i) 
    241         seen_threads.clear() 
    242          
    243         for func in self.onStopServerList: 
    244             func() 
    245246         
    246247        self.httpserver = None 
  • trunk/cherrypy/test/test_states.py

    r699 r709  
    5555}) 
    5656 
     57class Dependency: 
     58     
     59    def __init__(self): 
     60        self.running = False 
     61        self.startcount = 0 
     62        self.threads = {} 
     63     
     64    def start(self): 
     65        self.running = True 
     66        self.startcount += 1 
     67     
     68    def stop(self): 
     69        self.running = False 
     70     
     71    def startthread(self, thread_id): 
     72        self.threads[thread_id] = None 
     73     
     74    def stopthread(self, thread_id): 
     75        del self.threads[thread_id] 
     76 
    5777 
    5878import helper 
    59  
    6079 
    6180class ServerStateTests(helper.CPWebCase): 
     
    6685        self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
    6786         
     87        # And our db_connection should not be running 
     88        self.assertEqual(db_connection.running, False) 
     89        self.assertEqual(db_connection.startcount, 0) 
     90        self.assertEqual(len(db_connection.threads), 0) 
     91         
    6892        # Test server start 
    6993        cherrypy.server.start(True, self.serverClass) 
     
    7599            self.assertRaises(IOError, cherrypy._cpserver.check_port, host, port) 
    76100         
     101        # The db_connection should be running now 
     102        self.assertEqual(db_connection.running, True) 
     103        self.assertEqual(db_connection.startcount, 1) 
     104        self.assertEqual(len(db_connection.threads), 0) 
     105         
    77106        self.getPage("/") 
    78107        self.assertBody("Hello World") 
     108        self.assertEqual(len(db_connection.threads), 1) 
    79109         
    80110        # Test server stop 
     
    84114        # Once the server has stopped, we should get a NotReady error again. 
    85115        self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
     116         
     117        # Verify that the onStopServer function was called 
     118        self.assertEqual(db_connection.running, False) 
     119        self.assertEqual(len(db_connection.threads), 0) 
    86120     
    87121    def test_1_Restart(self): 
    88         # Test server start 
    89122        cherrypy.server.start(True, self.serverClass) 
     123         
     124        # The db_connection should be running now 
     125        self.assertEqual(db_connection.running, True) 
     126        sc = db_connection.startcount 
     127         
    90128        self.getPage("/") 
    91129        self.assertBody("Hello World") 
     130        self.assertEqual(len(db_connection.threads), 1) 
    92131         
    93132        # Test server restart from this thread 
     
    96135        self.getPage("/") 
    97136        self.assertBody("Hello World") 
     137        self.assertEqual(db_connection.running, True) 
     138        self.assertEqual(db_connection.startcount, sc + 1) 
     139        self.assertEqual(len(db_connection.threads), 1) 
    98140         
    99141        # Test server restart from inside a page handler 
     
    101143        self.assertEqual(cherrypy.server.state, 1) 
    102144        self.assertBody("app was restarted succesfully") 
     145        self.assertEqual(db_connection.running, True) 
     146        self.assertEqual(db_connection.startcount, sc + 2) 
     147        # Since we are requesting synchronously, is only one thread used? 
     148        # Note that the "/restart" request has been flushed. 
     149        self.assertEqual(len(db_connection.threads), 0) 
    103150         
    104151        cherrypy.server.stop() 
    105152        self.assertEqual(cherrypy.server.state, 0) 
     153        self.assertEqual(db_connection.running, False) 
     154        self.assertEqual(len(db_connection.threads), 0) 
    106155     
    107156    def test_2_KeyboardInterrupt(self): 
     
    120169            self.assertEqual(cherrypy.server.state, 0) 
    121170            self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
     171            self.assertEqual(db_connection.running, False) 
     172            self.assertEqual(len(db_connection.threads), 0) 
    122173             
    123174            # Raise a keyboard interrupt in a page handler; on multithreaded 
     
    136187            self.assertEqual(cherrypy.server.state, 0) 
    137188            self.assertRaises(cherrypy.NotReady, self.getPage, "/") 
    138  
     189            self.assertEqual(db_connection.running, False) 
     190            self.assertEqual(len(db_connection.threads), 0) 
     191 
     192 
     193db_connection = None 
    139194 
    140195def run(server, conf): 
     
    143198    suite = helper.CPTestLoader.loadTestsFromTestCase(ServerStateTests) 
    144199    try: 
     200        global db_connection 
     201        db_connection = Dependency() 
     202        cherrypy.server.onStartServerList.append(db_connection.start) 
     203        cherrypy.server.onStopServerList.append(db_connection.stop) 
     204        cherrypy.server.onStartThreadList.append(db_connection.startthread) 
     205        cherrypy.server.onStopThreadList.append(db_connection.stopthread) 
     206         
    145207        helper.CPTestRunner.run(suite) 
    146208    finally: 

Hosted by WebFaction

Log in as guest/cpguest to create tickets