def test_maintain_the_pool_of_workers_if_not_terminating(self):
        # dummy function used in following tests
        def sleep_long(seconds):
            setproctitle("simpleflow Worker(sleep_long, {})".format(seconds))
            time.sleep(seconds)

        # retrieve workers (not great; TODO: move it to Supervisor class)
        def workers():
            return [
                p for p in Process().children(recursive=True)
                if "Worker(sleep_long" in p.name()
            ]

        # create a supervisor sub-process
        supervisor = Supervisor(sleep_long,
                                arguments=(30, ),
                                nb_children=1,
                                background=True)
        supervisor.start()

        # we need to wait a little here so the workers start
        self.wait(0.5)
        old_workers = workers()
        expect(len(old_workers)).to.equal(1)

        # now kill the worker
        old_workers[0].pid
        os.kill(workers()[0].pid, signal.SIGKILL)
        self.wait(0.5)

        # ... and check that the process has been replaced
        new_workers = workers()
        expect(len(new_workers)).to.equal(1)
        expect(new_workers[0].pid).to.not_be.equal(old_workers[0].pid)
    def test_terminate(self):
        # custom function that handles sigterm by changing its name, so we can
        # test it effectively received a SIGTERM (maybe there's a better way?)
        def sigterm_receiver():
            def handle_sigterm(signum, frame):
                setproctitle("simpleflow worker: shutting down")
                time.sleep(10)
                os._exit(0)

            signal.signal(signal.SIGTERM, handle_sigterm)
            setproctitle("simpleflow worker: running")
            time.sleep(60)

        supervisor = Supervisor(sigterm_receiver,
                                nb_children=1,
                                background=True)
        supervisor.start()

        # TODO: find a non-sleep approach
        self.wait(1)
        self.assertProcess(r'worker: running')

        supervisor_process = Process().children()[0]
        os.kill(supervisor_process.pid, signal.SIGTERM)

        # TODO: find a non-sleep approach
        self.wait(1)
        self.assertProcess(r'worker: shutting down')
    def test_payload_friendly_name(self):
        def foo():
            pass

        supervisor = Supervisor(foo, background=True)
        self.assertEqual(supervisor._payload_friendly_name, "foo")

        class Foo(object):
            def bar(self):
                pass

        supervisor = Supervisor(Foo().bar, background=True)
        self.assertEqual(supervisor._payload_friendly_name, "Foo.bar")
Esempio n. 4
0
    def test_start(self):
        # dummy function used in following tests
        def sleep_long(seconds):
            setproctitle("simpleflow Worker(sleep_long, {})".format(seconds))
            time.sleep(seconds)

        # create a supervisor sub-process
        supervisor = Supervisor(
            sleep_long, arguments=(30,), nb_children=2, background=True
        )
        supervisor.start()

        # we need to wait a little here so the process starts and gets its name set
        # TODO: find a non-sleep approach to this
        self.wait(0.5)
        self.assertProcess(
            r"simpleflow Supervisor\(_payload_friendly_name=sleep_long, _nb_children=2\)"
        )
        self.assertProcess(r"simpleflow Worker\(sleep_long, 30\)", count=2)