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

Changeset 1998

Show
Ignore:
Timestamp:
06/29/08 09:57:36
Author:
fumanchu
Message:

Test and fix for #826 (SignalHandler? needs an unsubscribe). Need nix testing.

Files:

Legend:

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

    r1996 r1998  
    6060                         'SIGUSR1': self.bus.graceful, 
    6161                         } 
     62         
     63        self._previous_handlers = {} 
    6264     
    6365    def subscribe(self): 
     
    6769            except ValueError: 
    6870                pass 
     71     
     72    def unsubscribe(self): 
     73        for sig, handler in self._previous_handlers.iteritems(): 
     74            signame = self.signals[sig] 
     75             
     76            if handler is None: 
     77                self.bus.log("Restoring %s handler to SIG_DFL." % signame) 
     78                handler = _signal.SIG_DFL 
     79            else: 
     80                self.bus.log("Restoring %s handler %r." % (signame, handler)) 
     81             
     82            try: 
     83                _signal.signal(sig, handler) 
     84            except ValueError: 
     85                self.bus.log("Unable to restore %s handler %r." % 
     86                             (signame, handler)) 
    6987     
    7088    def set_handler(self, signal, listener=None): 
     
    89107            signum = signal 
    90108         
    91         # Should we do something with existing signal handlers? 
    92         # cur = _signal.getsignal(signum) 
    93         _signal.signal(signum, self._handle_signal) 
     109        prev = _signal.signal(signum, self._handle_signal) 
     110        self._previous_handlers[signum] = prev 
     111         
    94112        if listener is not None: 
    95113            self.bus.log("Listening for %s." % signame) 
  • trunk/cherrypy/test/test_states.py

    r1997 r1998  
    7676        cherrypy._cpserver.wait_for_occupied_port(host, port) 
    7777     
     78    # Give the engine a wee bit more time to finish STARTING 
     79    if daemonize: 
     80        time.sleep(2) 
     81    else: 
     82        time.sleep(1) 
     83     
    7884    return result 
    7985 
     
    159165db_connection = Dependency(engine) 
    160166db_connection.subscribe() 
     167 
     168 
     169 
     170# ------------ Enough helpers. Time for real live test cases. ------------ # 
    161171 
    162172 
     
    392402        exit_code = spawn_cp(wait=True, daemonize=True) 
    393403         
    394         # Give the server some time to start up 
    395         time.sleep(2) 
    396          
    397404        # Get the PID from the file. 
    398405        pid = int(open(PID_file_path).read()) 
     
    431438        write_conf(scheme=self.scheme) 
    432439        pid = spawn_cp(wait=False, daemonize=False) 
    433         # Give the engine a wee bit more time to finish STARTING 
    434         time.sleep(1) 
    435440        # Send a SIGHUP 
    436441        os.kill(pid, SIGHUP) 
     
    459464        write_conf(scheme=self.scheme) 
    460465        exit_code = spawn_cp(wait=True, daemonize=True) 
    461          
    462         # Give the server some time to start up 
    463         time.sleep(2) 
    464466         
    465467        # Get the PID from the file. 
     
    478480            self.getPage("/exit") 
    479481        wait(new_pid) 
    480  
     482     
     483    def test_SIGTERM(self): 
     484        # SIGTERM should shut down the server whether daemonized or not. 
     485        if not self.server_class: 
     486            print "skipped (no server) ", 
     487            return 
     488         
     489        try: 
     490            from signal import SIGTERM 
     491        except ImportError: 
     492            print "skipped (no SIGTERM) ", 
     493            return 
     494         
     495        try: 
     496            from os import kill 
     497        except ImportError: 
     498            print "skipped (no os.kill) ", 
     499            return 
     500         
     501        # Spawn a normal, undaemonized process. 
     502        write_conf(scheme=self.scheme) 
     503        pid = spawn_cp(wait=False, daemonize=False) 
     504        # Send a SIGTERM 
     505        os.kill(pid, SIGTERM) 
     506        # This might hang if things aren't working right, but meh. 
     507        wait(pid) 
     508         
     509        if os.name in ['posix']:  
     510            # Spawn a daemonized process and test again. 
     511            exit_code = spawn_cp(wait=True, daemonize=True) 
     512            # Send a SIGTERM 
     513            pid = int(open(PID_file_path).read()) 
     514            os.kill(pid, SIGTERM) 
     515            # This might hang if things aren't working right, but meh. 
     516            wait(pid) 
     517     
     518    def test_signal_handler_unsubscribe(self): 
     519        if not self.server_class: 
     520            print "skipped (no server) ", 
     521            return 
     522         
     523        try: 
     524            from signal import SIGTERM 
     525        except ImportError: 
     526            print "skipped (no SIGTERM) ", 
     527            return 
     528         
     529        try: 
     530            from os import kill 
     531        except ImportError: 
     532            print "skipped (no os.kill) ", 
     533            return 
     534         
     535        # Spawn a normal, undaemonized process. 
     536        write_conf(scheme=self.scheme) 
     537        pid = spawn_cp(wait=False, daemonize=False) 
     538        self.getPage("/unsub_sig") 
     539        self.assertBody("OK") 
     540        os.kill(pid, SIGTERM) 
     541        wait(pid) 
     542        # Assert the old handler ran. 
     543        errlog = os.path.join(thisdir, 'test_states_demo.error.log') 
     544        self.assertEqual(open(errlog, 'rb').readlines()[-1], 
     545                         "I am an old SIGTERM handler.") 
    481546 
    482547 
  • trunk/cherrypy/test/test_states_demo.py

    r1995 r1998  
    3333        cherrypy.engine.exit() 
    3434    exit.exposed = True 
     35     
     36    def unsub_sig(self): 
     37        cherrypy.engine.signal_handler.unsubscribe() 
     38        return "OK" 
     39    unsub_sig.exposed = True 
     40 
     41try: 
     42    from signal import SIGTERM 
     43except ImportError: 
     44    pass 
     45else: 
     46    def old_term_handler(signum=None, frame=None): 
     47        cherrypy.log("I am an old SIGTERM handler.") 
     48    _signal.signal(SIGTERM, old_term_handler) 
     49 
    3550 
    3651def starterror(): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets