예제 #1
0
 def test_killing(self):
     """Killing processes that don't die after 30 seconds"""
     handlers = {}
     def _install(signum, value):
         handlers[signum] = value
     def _wait():
         raise ValueError()
     sleeps = []
     def _sleep(tm):
         sleeps.append(tm)
     terminations = [0]
     killings = [0]
     def _run(dummyCommand):
         ret = DummyProcess()
         def _terminate():
             terminations[0] += 1
         def _kill():
             killings[0] += 1
         def _poll():
             return None
         ret.terminate = _terminate
         ret.kill = _kill
         ret.poll = _poll
         return ret
     reactor = reaperlib.SyncReactor(install=_install, run=_run, sleep=_sleep, wait=_wait)
     oldStderr = sys.stderr
     def _cleanup():
         sys.stderr = oldStderr
     self.addCleanup(_cleanup)
     sys.stderr = StringIO()
     reaperlib.baseMain(reactor, [None, 'ls'])
     self.assertEquals(terminations[0], 1)
     self.assertEquals(sleeps, [1]*30)
     self.assertEquals(killings[0], 1)
예제 #2
0
 def test_value_error(self):
     """Random errors generate a traceback"""
     handlers = {}
     def _install(signum, value):
         handlers[signum] = value
     def _run(dummyCommand):
         ret = DummyProcess()
         ret.poll = lambda: 0
         return ret
     def _wait():
         raise ValueError()
     def _sleep(dummyTm):
         pass
     reactor = reaperlib.SyncReactor(install=_install, run=_run, sleep=_sleep, wait=_wait)
     oldStderr = sys.stderr
     def _cleanup():
         sys.stderr = oldStderr
     self.addCleanup(_cleanup)
     sys.stderr = StringIO()
     reaperlib.baseMain(reactor, [None, 'ls'])
     ## pylint: disable=no-member
     lines = sys.stderr.getvalue().splitlines()
     ## pylint: enable=no-member
     _cleanup()
     dummyBaseMain, = (line for line in lines if line.endswith('in baseMain'))
예제 #3
0
    def test_termination(self):
        """Terminating processes when we go down"""
        handlers = {}

        def _install(signum, value):
            handlers[signum] = value

        def _wait():
            raise ValueError()

        sleeps = []

        def _sleep(tm):
            sleeps.append(tm)

        terminations = [0]
        killings = [0]
        polls = [None, None, 0, 0]

        def _run(dummyCommand):
            ret = DummyProcess()

            def _terminate():
                terminations[0] += 1

            def _kill():
                killings[0] += 1

            def _poll():
                return polls.pop(0)

            ret.terminate = _terminate
            ret.kill = _kill
            ret.poll = _poll
            return ret

        reactor = reaperlib.SyncReactor(install=_install,
                                        run=_run,
                                        sleep=_sleep,
                                        wait=_wait)
        oldStderr = sys.stderr

        def _cleanup():
            sys.stderr = oldStderr

        self.addCleanup(_cleanup)
        sys.stderr = StringIO()
        reaperlib.baseMain(reactor, [None, 'ls'])
        self.assertEquals(terminations[0], 1)
        self.assertEquals(killings[0], 0)
        self.assertEquals(sleeps, [1, 1])
        self.assertEquals(polls, [0])
예제 #4
0
 def test_system_error(self):
     """System error (generated by signals) works"""
     handlers = {}
     def _install(signum, value):
         handlers[signum] = value
     def _run(dummyCommand):
         ret = DummyProcess()
         ret.poll = lambda: 0
         return ret
     def _wait():
         raise SystemError()
     def _sleep(dummyTm):
         pass
     reactor = reaperlib.SyncReactor(install=_install, run=_run, sleep=_sleep, wait=_wait)
     reaperlib.baseMain(reactor, [None, 'ls'])
     self.assertTrue(True)
예제 #5
0
 def test_ctrl_c(self):
     """KeyboardInterrupt works"""
     handlers = {}
     def _install(signum, value):
         handlers[signum] = value
     def _run(dummyCommand):
         ret = DummyProcess()
         ret.poll = lambda: 0
         return ret
     def _wait():
         raise KeyboardInterrupt()
     def _sleep(dummyTm):
         pass
     reactor = reaperlib.SyncReactor(install=_install, run=_run, sleep=_sleep, wait=_wait)
     reaperlib.baseMain(reactor, [None, 'ls'])
     self.assertTrue(True)
예제 #6
0
 def test_simple(self):
     """Happy path works"""
     handlers = {}
     def _install(signum, value):
         handlers[signum] = value
     args = []
     def _run(command):
         args[:] = command
         ret = DummyProcess()
         ret.poll = lambda: 0
         return ret
     waitCount = [0]
     def _wait():
         waitCount[0] += 1
         return DummyProcess.pid, None
     naps = []
     def _sleep(tm):
         naps.append(tm)
     reactor = reaperlib.SyncReactor(install=_install, run=_run, sleep=_sleep, wait=_wait)
     reaperlib.baseMain(reactor, [None, 'ls'])
     self.assertEquals(waitCount[0], 1)
     self.assertEquals(naps, [])
     self.assertEquals(set(handlers), set([signal.SIGTERM, signal.SIGINT, signal.SIGALRM]))
     self.assertEquals(args, ['ls'])
예제 #7
0
    def test_value_error(self):
        """Random errors generate a traceback"""
        handlers = {}

        def _install(signum, value):
            handlers[signum] = value

        def _run(dummyCommand):
            ret = DummyProcess()
            ret.poll = lambda: 0
            return ret

        def _wait():
            raise ValueError()

        def _sleep(dummyTm):
            pass

        reactor = reaperlib.SyncReactor(install=_install,
                                        run=_run,
                                        sleep=_sleep,
                                        wait=_wait)
        oldStderr = sys.stderr

        def _cleanup():
            sys.stderr = oldStderr

        self.addCleanup(_cleanup)
        sys.stderr = StringIO()
        reaperlib.baseMain(reactor, [None, 'ls'])
        ## pylint: disable=no-member
        lines = sys.stderr.getvalue().splitlines()
        ## pylint: enable=no-member
        _cleanup()
        dummyBaseMain, = (line for line in lines
                          if line.endswith('in baseMain'))
예제 #8
0
    def test_system_error(self):
        """System error (generated by signals) works"""
        handlers = {}

        def _install(signum, value):
            handlers[signum] = value

        def _run(dummyCommand):
            ret = DummyProcess()
            ret.poll = lambda: 0
            return ret

        def _wait():
            raise SystemError()

        def _sleep(dummyTm):
            pass

        reactor = reaperlib.SyncReactor(install=_install,
                                        run=_run,
                                        sleep=_sleep,
                                        wait=_wait)
        reaperlib.baseMain(reactor, [None, 'ls'])
        self.assertTrue(True)
예제 #9
0
    def test_ctrl_c(self):
        """KeyboardInterrupt works"""
        handlers = {}

        def _install(signum, value):
            handlers[signum] = value

        def _run(dummyCommand):
            ret = DummyProcess()
            ret.poll = lambda: 0
            return ret

        def _wait():
            raise KeyboardInterrupt()

        def _sleep(dummyTm):
            pass

        reactor = reaperlib.SyncReactor(install=_install,
                                        run=_run,
                                        sleep=_sleep,
                                        wait=_wait)
        reaperlib.baseMain(reactor, [None, 'ls'])
        self.assertTrue(True)
예제 #10
0
    def test_simple(self):
        """Happy path works"""
        handlers = {}

        def _install(signum, value):
            handlers[signum] = value

        args = []

        def _run(command):
            args[:] = command
            ret = DummyProcess()
            ret.poll = lambda: 0
            return ret

        waitCount = [0]

        def _wait():
            waitCount[0] += 1
            return DummyProcess.pid, None

        naps = []

        def _sleep(tm):
            naps.append(tm)

        reactor = reaperlib.SyncReactor(install=_install,
                                        run=_run,
                                        sleep=_sleep,
                                        wait=_wait)
        reaperlib.baseMain(reactor, [None, 'ls'])
        self.assertEquals(waitCount[0], 1)
        self.assertEquals(naps, [])
        self.assertEquals(set(handlers),
                          set([signal.SIGTERM, signal.SIGINT, signal.SIGALRM]))
        self.assertEquals(args, ['ls'])