Example #1
0
    def test_result(self):
        factory = Factory([("", 1)])

        async_result = factory.queue_work('python -c "exit(0)"')

        returncode = async_result.get()

        self.assertEquals(returncode, 0)
Example #2
0
    def test_result_nonzero(self):
        expected_error = """Command ' python -c "exit(1)"' returned non-zero exit status 1"""
        factory = Factory([("", 1)])

        async_result = factory.queue_work('python -c "exit(1)"')

        exception_message = None
        try:
            async_result.get()

            self.fail("Should have thrown")
        except CalledProcessError as e:
            exception_message = str(e)

        self.assertEquals(exception_message, expected_error)
Example #3
0
    def test_parallel(self):
        notified_commands = []
        expected_commands = """\
 python -c "x=0; import time; time.sleep(0.5)"
 python -c "x=1; import time; time.sleep(0.5)"
 python -c "x=2; import time; time.sleep(0.5)"
 python -c "x=3; import time; time.sleep(0.5)"\
"""

        def callback(command):
            notified_commands.append(command)

        factory = Factory([("", 2)], launch_callback=callback)
        start = time.time()

        for i in range(4):
            script = 'python -c "x=%d; import time; time.sleep(0.5)"' % i
            factory.queue_work(script)

        factory.wait()

        duration = time.time() - start
        notified_commands.sort()
        self.assertEquals('\n'.join(notified_commands), expected_commands)
        self.assertGreater(duration, 0.5)
        self.assertLess(duration, 1.5)
Example #4
0
    def test_wait(self):
        notified_commands = StringIO()
        expected_commands = """\
 python -c "import time; time.sleep(0.5)"
 python -c "x = 1"
 python -c "x = 2"
"""

        def callback(command):
            notified_commands.write(command + "\n")

        factory = Factory([("", 1)], launch_callback=callback)

        factory.queue_work('python -c "import time; time.sleep(0.5)"')
        factory.queue_work('python -c "x = 1"')
        factory.queue_work('python -c "x = 2"')

        factory.wait()

        self.assertEquals(notified_commands.getvalue(), expected_commands)
Example #5
0
    def test_queue_job(self):
        notified_commands = StringIO()
        expected_commands = " python -c pass\n"

        def callback(command):
            notified_commands.write(command + "\n")

        factory = Factory([("", 1)], launch_callback=callback)

        factory.queue_job(Job('python -c pass'))

        factory.wait()

        self.assertEquals(notified_commands.getvalue(), expected_commands)
Example #6
0
    def test_failing_callback(self):
        command = 'python -c pass'
        expected_message = (
            "Failed callback for ' python -c pass'. KeyError: 'missing_key'")

        def failing_callback(command):
            a = {}
            a['x'] = a['missing_key']

        factory = Factory([("", 1)], launch_callback=failing_callback)

        factory.queue_work(command)

        exception_message = None
        try:
            factory.wait()

            self.fail("Should have thrown")
        except RuntimeError as e:
            exception_message = str(e)

        self.assertEquals(exception_message, expected_message)
Example #7
0
    def test_wait_multiple_nonzero(self):
        logging.disable(None)  # Make sure logging isn't disabled.
        expected_error = """Command ' python -c "exit(2)"' returned non-zero exit status 2"""
        factory = Factory([("", 1)])

        factory.queue_work('python -c "exit(1)"')
        factory.queue_work('python -c "exit(2)"')

        exception_message = None
        try:
            with LogCapture() as log:
                factory.wait()

            self.fail("Should have thrown")
        except CalledProcessError as e:
            exception_message = str(e)

        self.assertEquals(exception_message, expected_error)
        log.check((
            'micall.monitor.fifo_scheduler', 'ERROR',
            """Command ' python -c "exit(1)"' returned non-zero exit status 1"""
        ))
Example #8
0
    def test_init(self):
        expected_resources = ["res1", "res1", "res2", "res2", "res2"]
        factory = Factory([("res1", 2), ("res2", 3)])

        resources = [worker.resource_allocated for worker in factory.workers]
        self.assertEquals(resources, expected_resources)