def test_delete_notfound(self, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() channel.queue_delete.side_effect = amqp.exceptions.NotFound res = AsyncResult('test_results') res.delete() exit_mock.assert_called_with(None, None, None) channel.queue_delete.assert_called_once_with('test_results')
def test_handle_message(self): res = AsyncResult('testid') msg = MagicMock(body='{"task_name": "test", "return": 123}', correlation_id='testid') AsyncResult._handle_message(msg, res) self.assertEqual(123, res._return) self.assertFalse(hasattr(res, '_exc'))
def test_handle_message_fail(self): res = AsyncResult('testid') body = '{"task_name": "test", "exception": {"value": "I123\\n."}}' msg = MagicMock(body=body, correlation_id='testid') AsyncResult._handle_message(msg, res) self.assertEqual(123, res._exc) self.assertFalse(hasattr(res, '_return'))
def test_get_cached(self): res = AsyncResult(None) res._result = True res._return = 123 self.assertEqual(123, res.get()) del res._return res._exc = ValueError self.assertRaises(ValueError, res.get)
def test_get_nonblock(self, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() exit_mock.return_value = None msg = MagicMock(correlation_id='abc123', body='{"return": 123}') channel.basic_get.return_value = msg res = AsyncResult('test_results') self.assertEqual(123, res.get(0.0)) exit_mock.assert_called_with(None, None, None) channel.basic_get.assert_called_with(queue='test_results', no_ack=True)
def test_gather(self, wait_mock): wait_mock.return_value = [MagicMock(correlation_id='abc123', body='{"return": 123}'), MagicMock(correlation_id='def456', body='{"return": "test"}')] res = AsyncResult('test_results') results = list(res.gather()) self.assertEqual(2, len(results)) self.assertEqual(123, results[0].get()) self.assertEqual('test', results[1].get()) wait_mock.assert_called_once_with(None)
def test_gather(self, wait_mock): wait_mock.return_value = [ MagicMock(correlation_id='abc123', body='{"return": 123}'), MagicMock(correlation_id='def456', body='{"return": "test"}') ] res = AsyncResult('test_results') results = list(res.gather()) self.assertEqual(2, len(results)) self.assertEqual(123, results[0].get()) self.assertEqual('test', results[1].get()) wait_mock.assert_called_once_with(None)
def test_get(self, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() exit_mock.return_value = None msg = MagicMock(correlation_id='abc123', body='{"return": 123}') def add_msg(queue, callback): callback(msg) channel.basic_consume.side_effect = add_msg res = AsyncResult('test_results') self.assertEqual(123, res.get()) exit_mock.assert_called_with(GeneratorExit, ANY, ANY) channel.basic_consume.assert_called_with(queue='test_results', callback=ANY) channel.connection.drain_events.assert_called_with(timeout=10.0) self.assertFalse(channel.connection.send_heartbeat.called)
def test_wait(self, get_mock): res = AsyncResult(None) res._return = True res.wait() del res._return get_mock.side_effect = ValueError res.wait()
def test_ready(self, wait_mock): res = AsyncResult(None) self.assertFalse(res.ready()) res._result = True self.assertTrue(res.ready()) res._exc = True self.assertTrue(res.ready()) wait_mock.assert_called_once_with(0.0)
def test_get_timeout(self, time_mock, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() time_mock.side_effect = [0.0, 20.0] channel.connection.drain_events.side_effect = socket_timeout res = AsyncResult('test_results') self.assertRaises(TimeoutError, res.get, 10.0) exit_mock.assert_called_with(None, None, None) channel.basic_consume.assert_called_with(queue='test_results', callback=ANY) channel.connection.drain_events.assert_called_with(timeout=0.0) channel.connection.send_heartbeat.assert_called_with()
def test_get_keyerror(self, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() exit_mock.return_value = None channel.basic_consume.side_effect = amqp.exceptions.NotFound res = AsyncResult('test_results') self.assertRaises(KeyError, res.get) exit_mock.assert_called_with(KeyError, ANY, ANY) channel.basic_consume.assert_called_with(queue='test_results', callback=ANY) self.assertFalse(channel.connection.drain_events.called) self.assertFalse(channel.connection.send_heartbeat.called)
def test_attributes(self): res = AsyncResult('testqueue') self.assertEqual('testqueue', res.result_queue) self.assertRaises(AttributeError, getattr, res, 'correlation_id') self.assertRaises(AttributeError, getattr, res, 'args') self.assertRaises(AttributeError, getattr, res, 'name') self.assertRaises(AttributeError, getattr, res, 'returned') self.assertRaises(AttributeError, getattr, res, 'exception') self.assertRaises(AttributeError, getattr, res, 'traceback') res._correlation_id = 'abc123' res._result = {'args': 123, 'kwargs': 456, 'task': 'test', 'exception': {'traceback': 'test traceback'}} self.assertEqual('abc123', res.correlation_id) self.assertEqual((123, 456), res.args) self.assertEqual('test', res.name) res._return = 789 self.assertEqual(789, res.returned) del res._return res._exc = 987 self.assertEqual(987, res.exception) self.assertEqual('test traceback', res.traceback)
def test_successful(self, ready_mock): res = AsyncResult(None) ready_mock.return_value = False self.assertFalse(res.successful()) ready_mock.return_value = True res._return = True self.assertTrue(res.successful()) del res._return res._exc = True self.assertFalse(res.successful())
def test_attributes(self): res = AsyncResult('testqueue') self.assertEqual('testqueue', res.result_queue) self.assertRaises(AttributeError, getattr, res, 'correlation_id') self.assertRaises(AttributeError, getattr, res, 'args') self.assertRaises(AttributeError, getattr, res, 'name') self.assertRaises(AttributeError, getattr, res, 'returned') self.assertRaises(AttributeError, getattr, res, 'exception') self.assertRaises(AttributeError, getattr, res, 'traceback') res._correlation_id = 'abc123' res._result = { 'args': 123, 'kwargs': 456, 'task': 'test', 'exception': { 'traceback': 'test traceback' } } self.assertEqual('abc123', res.correlation_id) self.assertEqual((123, 456), res.args) self.assertEqual('test', res.name) res._return = 789 self.assertEqual(789, res.returned) del res._return res._exc = 987 self.assertEqual(987, res.exception) self.assertEqual('test traceback', res.traceback)
def test_delete(self, exit_mock, enter_mock): enter_mock.return_value = channel = MagicMock() res = AsyncResult('test_results') res.delete() exit_mock.assert_called_with(None, None, None) channel.queue_delete.assert_called_once_with('test_results')