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)])
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.',)])
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)])
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
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
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)])
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, [])
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, [])
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)
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,)])
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()),)])
def test_workers0_no_logger(self): service.sustain_workers(0, self.worker_func) self.assertEquals(self.worker_func_calls, [(0,)])