def test_daemonize(self): if not HAS_DAEMONIZE: raise Skip("need `daemonize` for this test case") def daemon_helper(): time.sleep(100) self.fail() with NamedTemporaryFile() as tf: try: daemon.daemonize(daemon_helper, name='sparts_unittest', pidfile=tf.name, logger=self.logger) except SystemExit: # Catch the daemonize library's attempt to sys.exit() pass def checkdaemon(): try: return daemon.status(tf.name, self.logger) except ValueError: return False # Eliminate the race condition waiting for daemonize.Daemonize() # to create *and* write the pid to the the pidfile. timer.run_until_true(checkdaemon, timeout=1.0) self.assertTrue(daemon.status(tf.name, self.logger)) self.assertTrue(daemon.kill(tf.name, self.logger)) self.assertFalse(daemon.status(tf.name, self.logger))
def test_daemonize(self): if not HAS_DAEMONIZE: raise Skip("need `daemonize` for this test case") def daemon_helper(): time.sleep(100) self.fail() with NamedTemporaryFile() as tf: # Fork so daemonizing the current process does not mess up with the # test suite. child_pid = os.fork() if child_pid == 0: try: daemon.daemonize(daemon_helper, name='sparts_unittest', pidfile=tf.name, logger=self.logger) except SystemExit: # Catch the daemonize library's attempt to sys.exit() pass else: def checkdaemon(): try: return daemon.status(tf.name, self.logger) except ValueError: return False # Eliminate the race condition waiting for # daemonize.Daemonize() to create *and* write the pid to the # pidfile. timer.run_until_true(checkdaemon, timeout=1.0) self.assertTrue(daemon.status(tf.name, self.logger)) self.assertTrue(daemon.kill(tf.name, self.logger)) self.assertFalse(daemon.status(tf.name, self.logger))
def testTimeout(self): with self.assertRaises(Exception): run_until_true(lambda: False, timeout=0.1) self.assertTrue(True)
def testTrue(self): run_until_true(lambda: True, timeout=5.0) self.assertTrue(True)