Example #1
0
 def test_send_result(self):
     channel = MagicMock()
     worker = WorkerProcess(None, None)
     worker._send_result(channel, 'testid', 'test', {})
     channel.basic_publish.assert_called_with(ANY,
                                              exchange='',
                                              routing_key='test')
Example #2
0
 def test_run(self, thread_cls_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess("testapp", ["testqueue"], 10)
     worker._consume = MagicMock(side_effect=SystemExit)
     worker._run()
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     worker._consume.assert_called_with()
Example #3
0
 def test_run(self, thread_cls_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess('testapp', ['testqueue'], 10)
     worker._consume = MagicMock(side_effect=SystemExit)
     worker._run()
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     worker._consume.assert_called_with()
Example #4
0
 def test_run_accessrefused(self, thread_cls_mock, sleep_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess("testapp", ["testqueue"], 10)
     worker._consume = MagicMock(side_effect=[AccessRefused, None])
     worker._run()
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     sleep_mock.assert_called_once_with(ANY)
     worker._consume.assert_called_with()
Example #5
0
 def test_run_exception(self, thread_cls_mock, print_exc_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess('testapp', ['testqueue'], 10)
     worker._consume = MagicMock(side_effect=AssertionError)
     self.assertRaises(AssertionError, worker._run)
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     print_exc_mock.assert_called_with()
     worker._consume.assert_called_with()
Example #6
0
 def test_run_accessrefused(self, thread_cls_mock, sleep_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess('testapp', ['testqueue'], 10)
     worker._consume = MagicMock(side_effect=[AccessRefused, None])
     worker._run()
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     sleep_mock.assert_called_once_with(ANY)
     worker._consume.assert_called_with()
Example #7
0
 def test_run_exception(self, thread_cls_mock, print_exc_mock):
     thread_cls_mock.return_value = thread_mock = MagicMock()
     worker = WorkerProcess("testapp", ["testqueue"], 10)
     worker._consume = MagicMock(side_effect=AssertionError)
     self.assertRaises(AssertionError, worker._run)
     thread_cls_mock.assert_called_with(target=ANY)
     thread_mock.start.assert_called_with()
     print_exc_mock.assert_called_with()
     worker._consume.assert_called_with()
Example #8
0
 def test_start_worker_child(self, exit_mock, fork_mock):
     app = MagicMock()
     worker = WorkerProcess(app, ["testqueue"])
     worker._run = MagicMock()
     master = WorkerMaster()
     fork_mock.return_value = 0
     master._start_worker(worker)
     fork_mock.assert_called_with()
     worker._run.assert_called_with()
     exit_mock.assert_called_with(os.EX_OK)
Example #9
0
 def test_start_worker_child(self, exit_mock, fork_mock):
     app = MagicMock()
     worker = WorkerProcess(app, ['testqueue'])
     worker._run = MagicMock()
     master = WorkerMaster()
     fork_mock.return_value = 0
     master._start_worker(worker)
     fork_mock.assert_called_with()
     worker._run.assert_called_with()
     exit_mock.assert_called_with(os.EX_OK)
Example #10
0
 def test_get_worker_app(self, exit_mock, fork_mock):
     app = MagicMock()
     worker = WorkerProcess(app, ['testqueue'])
     worker._run = MagicMock()
     master = WorkerMaster()
     fork_mock.return_value = 0
     self.assertRaises(RuntimeError, get_worker_app)
     master._start_worker(worker)
     self.assertEquals(app, get_worker_app())
     fork_mock.assert_called_with()
     worker._run.assert_called_with()
     exit_mock.assert_called_with(os.EX_OK)
Example #11
0
 def test_get_worker_app(self, exit_mock, fork_mock):
     app = MagicMock()
     worker = WorkerProcess(app, ["testqueue"])
     worker._run = MagicMock()
     master = WorkerMaster()
     fork_mock.return_value = 0
     self.assertRaises(RuntimeError, get_worker_app)
     master._start_worker(worker)
     self.assertEquals(app, get_worker_app())
     fork_mock.assert_called_with()
     worker._run.assert_called_with()
     exit_mock.assert_called_with(os.EX_OK)
Example #12
0
 def test_on_message_task_fail(self, send_result_mock):
     app = MagicMock()
     app.tasks.func.apply.side_effect = ValueError
     worker = WorkerProcess(app, None, 2)
     worker.counter = 0
     channel = MagicMock()
     body = '{"task": "func", "args": [1], "kwargs": {"two": 2}}'
     msg = MagicMock(body=body, correlation_id="testid", reply_to="test")
     self.assertRaises(ValueError, worker._on_message, channel, msg)
     app.tasks.func.apply.assert_called_with([1], {"two": 2}, "testid")
     send_result_mock.assert_called_with(channel, "testid", "test", ANY)
     channel.basic_reject.assert_called_with(ANY, requeue=False)
     self.assertFalse(channel.basic_cancel.called)
Example #13
0
 def test_on_message_task_fail(self, send_result_mock):
     app = MagicMock()
     app.tasks.func.apply.side_effect = ValueError
     worker = WorkerProcess(app, None, 2)
     worker.counter = 0
     channel = MagicMock()
     body = '{"task": "func", "args": [1], "kwargs": {"two": 2}}'
     msg = MagicMock(body=body, correlation_id='testid', reply_to='test')
     self.assertRaises(ValueError, worker._on_message, channel, msg)
     app.tasks.func.apply.assert_called_with([1], {'two': 2}, 'testid')
     send_result_mock.assert_called_with(channel, 'testid', 'test', ANY)
     channel.basic_reject.assert_called_with(ANY, requeue=False)
     self.assertFalse(channel.basic_cancel.called)
Example #14
0
    def test_on_message_limit(self):
        app = MagicMock()
        worker = WorkerProcess(app, None, 1)
        worker.counter = 0
        channel = MagicMock(callbacks={1: None, 2: None})

        def clear_callbacks(consumer_tag):
            channel.callbacks = {}

        channel.basic_cancel.side_effect = clear_callbacks
        body = '{"task": "func", "args": [1], "kwargs": {"two": 2}}'
        msg = MagicMock(body=body, correlation_id="testid", reply_to=None)
        worker._on_message(channel, msg)
        app.tasks.func.apply.assert_called_with([1], {"two": 2}, "testid")
        channel.basic_ack.assert_called_with(ANY)
        channel.basic_cancel.assert_any_call(1)
        channel.basic_cancel.assert_any_call(2)
Example #15
0
    def test_on_message_limit(self):
        app = MagicMock()
        worker = WorkerProcess(app, None, 1)
        worker.counter = 0
        channel = MagicMock(callbacks={1: None, 2: None})

        def clear_callbacks(consumer_tag):
            channel.callbacks = {}

        channel.basic_cancel.side_effect = clear_callbacks
        body = '{"task": "func", "args": [1], "kwargs": {"two": 2}}'
        msg = MagicMock(body=body, correlation_id='testid', reply_to=None)
        worker._on_message(channel, msg)
        app.tasks.func.apply.assert_called_with([1], {'two': 2}, 'testid')
        channel.basic_ack.assert_called_with(ANY)
        channel.basic_cancel.assert_any_call(1)
        channel.basic_cancel.assert_any_call(2)
Example #16
0
    def test_consume(self, amqp_exit_mock, amqp_enter_mock):
        app = MagicMock()
        worker = WorkerProcess(app, ["testqueue"], 1, exclusive="exclusive")
        channel = MagicMock(callbacks=True)

        def set_done():
            channel.callbacks = False

        channel.connection.drain_events.side_effect = set_done
        amqp_enter_mock.return_value = channel
        worker._consume()
        amqp_enter_mock.assert_called_with()
        amqp_exit_mock.assert_called_with(None, None, None)
        channel.basic_consume.assert_called_with(
            queue="testqueue", consumer_tag="testqueue", callback=ANY, exclusive="exclusive"
        )
        channel.connection.drain_events.assert_called_with()
Example #17
0
 def test_send_heartbeats(self, sleep_mock):
     sleep_mock.side_effect = [None, Exception, StopIteration]
     worker = WorkerProcess(None, None, 1)
     worker.active_connection = conn = MagicMock(heartbeat=30.0)
     self.assertRaises(StopIteration, worker._send_heartbeats)
     self.assertEqual(1, conn.send_heartbeat.call_count)
     self.assertEqual(3, sleep_mock.call_count)
     sleep_mock.assert_any_call(15.0)
     sleep_mock.assert_any_call(1.0)
Example #18
0
    def test_consume(self, amqp_exit_mock, amqp_enter_mock):
        app = MagicMock()
        worker = WorkerProcess(app, ['testqueue'], 1, exclusive='exclusive')
        channel = MagicMock(callbacks=True)

        def set_done():
            channel.callbacks = False

        channel.connection.drain_events.side_effect = set_done
        amqp_enter_mock.return_value = channel
        worker._consume()
        amqp_enter_mock.assert_called_with()
        amqp_exit_mock.assert_called_with(None, None, None)
        channel.basic_consume.assert_called_with(queue='testqueue',
                                                 consumer_tag='testqueue',
                                                 callback=ANY,
                                                 exclusive='exclusive')
        channel.connection.drain_events.assert_called_with()
Example #19
0
 def test_send_result(self):
     channel = MagicMock()
     worker = WorkerProcess(None, None)
     worker._send_result(channel, "testid", "test", {})
     channel.basic_publish.assert_called_with(ANY, exchange="", routing_key="test")