Changeset 1998
- Timestamp:
- 06/29/08 09:57:36
- Files:
-
- trunk/cherrypy/process/plugins.py (modified) (3 diffs)
- trunk/cherrypy/test/test_states.py (modified) (6 diffs)
- trunk/cherrypy/test/test_states_demo.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/process/plugins.py
r1996 r1998 60 60 'SIGUSR1': self.bus.graceful, 61 61 } 62 63 self._previous_handlers = {} 62 64 63 65 def subscribe(self): … … 67 69 except ValueError: 68 70 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)) 69 87 70 88 def set_handler(self, signal, listener=None): … … 89 107 signum = signal 90 108 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 94 112 if listener is not None: 95 113 self.bus.log("Listening for %s." % signame) trunk/cherrypy/test/test_states.py
r1997 r1998 76 76 cherrypy._cpserver.wait_for_occupied_port(host, port) 77 77 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 78 84 return result 79 85 … … 159 165 db_connection = Dependency(engine) 160 166 db_connection.subscribe() 167 168 169 170 # ------------ Enough helpers. Time for real live test cases. ------------ # 161 171 162 172 … … 392 402 exit_code = spawn_cp(wait=True, daemonize=True) 393 403 394 # Give the server some time to start up395 time.sleep(2)396 397 404 # Get the PID from the file. 398 405 pid = int(open(PID_file_path).read()) … … 431 438 write_conf(scheme=self.scheme) 432 439 pid = spawn_cp(wait=False, daemonize=False) 433 # Give the engine a wee bit more time to finish STARTING434 time.sleep(1)435 440 # Send a SIGHUP 436 441 os.kill(pid, SIGHUP) … … 459 464 write_conf(scheme=self.scheme) 460 465 exit_code = spawn_cp(wait=True, daemonize=True) 461 462 # Give the server some time to start up463 time.sleep(2)464 466 465 467 # Get the PID from the file. … … 478 480 self.getPage("/exit") 479 481 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.") 481 546 482 547 trunk/cherrypy/test/test_states_demo.py
r1995 r1998 33 33 cherrypy.engine.exit() 34 34 exit.exposed = True 35 36 def unsub_sig(self): 37 cherrypy.engine.signal_handler.unsubscribe() 38 return "OK" 39 unsub_sig.exposed = True 40 41 try: 42 from signal import SIGTERM 43 except ImportError: 44 pass 45 else: 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 35 50 36 51 def starterror():

