Esempio n. 1
0
    def test_no_logger_ok(self):

        def _os_wait(*args):
            raise KeyboardInterrupt()

        service.os_wait = _os_wait
        service.sustain_workers(1, self.worker_func)
        self.assertEquals(self.killpg_calls, [(0, service.SIGINT)])
Esempio n. 2
0
 def test_workers0(self):
     logger = FakeLogger()
     service.sustain_workers(0, self.worker_func, logger)
     self.assertEquals(self.worker_func_calls, [(0,)])
     self.assertEquals(
         logger.debug_calls,
         [('wid:000 pid:%s Starting inproc worker.' % service.getpid(),)])
     self.assertEquals(
         logger.info_calls,
         [('Exiting due to workers = 0 mode.',)])
Esempio n. 3
0
    def test_keyboard_interrupt_exit(self):
        logger = FakeLogger()

        def _os_wait(*args):
            raise KeyboardInterrupt()

        service.os_wait = _os_wait
        service.sustain_workers(1, self.worker_func, logger)
        self.assertEquals(logger.debug_calls, [])
        self.assertEquals(logger.info_calls, [('Exiting due to SIGINT.',)])
        self.assertEquals(self.killpg_calls, [(0, service.SIGINT)])
Esempio n. 4
0
    def test_child_exception(self):

        def _worker_func(*args):
            raise Exception('testing')

        logger = FakeLogger()
        service.fork = lambda *a: 0
        exc = None
        try:
            service.sustain_workers(1, _worker_func, logger)
        except Exception, err:
            exc = err
Esempio n. 5
0
    def test_oserror_unknown_reraise(self):
        logger = FakeLogger()

        def _os_wait(*args):
            raise OSError('testing')

        service.os_wait = _os_wait
        exc = None
        try:
            service.sustain_workers(1, self.worker_func, logger)
        except Exception, err:
            exc = err
Esempio n. 6
0
    def test_sighup_exit(self):
        logger = FakeLogger()

        def _os_wait(*args):
            self.signal_calls[1][1]()
            return (1, 0)

        service.os_wait = _os_wait
        service.sustain_workers(1, self.worker_func, logger)
        self.assertEquals(logger.debug_calls, [])
        self.assertEquals(logger.info_calls, [('Exiting due to SIGHUP.',)])
        self.assertEquals(self.killpg_calls, [(0, service.SIGHUP)])
Esempio n. 7
0
    def test_no_sleep_on_initial_launch(self):
        fork_calls = []

        def _os_wait(*args):
            raise KeyboardInterrupt()

        def _fork(*args):
            fork_calls.append(args)
            return len(fork_calls)

        service.os_wait = _os_wait
        service.fork = _fork
        service.sustain_workers(5, self.worker_func)
        self.assertEquals(fork_calls, [()] * 5)
        self.assertEquals(self.sleep_calls, [])
Esempio n. 8
0
 def test_child(self):
     logger = FakeLogger()
     service.fork = lambda *a: 0
     service.sustain_workers(1, self.worker_func, logger)
     # Asserts the TERM and HUP signal handlers are cleared with the child.
     self.assertEquals(
         set(self.signal_calls[-2:]),
         set([(service.SIGHUP, 0), (service.SIGTERM, 0)]))
     self.assertEquals(self.worker_func_calls, [(0,)])
     self.assertEquals(logger.debug_calls, [
         ('wid:000 ppid:%s pid:%s Starting worker.' %
             (service.getppid(), service.getpid()),),
         ('wid:000 ppid:%s pid:%s Worker exited.' %
             (service.getppid(), service.getpid()),)])
     self.assertEquals(logger.info_calls, [])
Esempio n. 9
0
    def test_oserror_echild_cycle(self):
        logger = FakeLogger()
        self.called = [0]

        def _os_wait(*args):
            self.called[0] += 1
            if self.called[0] == 2:
                raise KeyboardInterrupt()
            err = OSError('testing')
            err.errno = service.ECHILD
            raise err

        service.os_wait = _os_wait
        service.sustain_workers(1, self.worker_func, logger)
        self.assertEquals(logger.debug_calls, [])
        self.assertEquals(logger.info_calls, [('Exiting due to SIGINT.',)])
        self.assertEquals(self.killpg_calls, [(0, service.SIGINT)])
        self.assertEquals(self.called[0], 2)
Esempio n. 10
0
    def test_sleep_on_relaunches(self):
        fork_calls = []

        def _os_wait_int(*args):
            raise KeyboardInterrupt()

        def _os_wait(*args):
            service.os_wait = _os_wait_int
            return 1, 0

        def _fork(*args):
            fork_calls.append(args)
            return len(fork_calls)

        service.os_wait = _os_wait
        service.fork = _fork
        service.sustain_workers(5, self.worker_func)
        self.assertEquals(fork_calls, [()] * 6)
        self.assertEquals(self.sleep_calls, [(1,)])
Esempio n. 11
0
    def test_child_exception(self):

        def _worker_func(*args):
            raise Exception('testing')

        logger = FakeLogger()
        service.fork = lambda *a: 0
        exc = None
        try:
            service.sustain_workers(1, _worker_func, logger)
        except Exception as err:
            exc = err
        self.assertEqual(str(exc), 'testing')
        self.assertEqual(logger.debug_calls, [
            ('wid:000 ppid:%s pid:%s Starting worker.' %
                (service.getppid(), service.getpid()),)])
        self.assertEqual(logger.info_calls, [])
        self.assertEqual(logger.exception_calls, [(
            'wid:000 ppid:%s pid:%s Worker exited due to exception: testing' %
            (service.getppid(), service.getpid()),)])
Esempio n. 12
0
 def test_workers0_no_logger(self):
     service.sustain_workers(0, self.worker_func)
     self.assertEquals(self.worker_func_calls, [(0,)])