def test_read_messages(self): msg = '{"action": "CREATE", "type": "vip", "element": {}}' self.pika_mock.basic_get.return_value = (MagicMock(), None, msg) rabbitmq = RabbitMQClient('', '', '', '', '', 'queue_name') for i in range(3): msg_ret = rabbitmq.read_messages().next() self.assertDictEqual(msg_ret[0], json.loads(msg))
def test_stop_read_messages(self): pika_mock = self._mock_pika() pika_mock.basic_get.return_value = (None, None, None) rabbitmq = RabbitMQClient('localhost', 5672, 'user', 'password', '/', 'queue_name') with self.assertRaises(StopIteration): rabbitmq.read_messages().next()
def test_get_message(self): msg = '{"action": "CREATE", "type": "vip", "element": {}}' self.pika_mock.basic_get.return_value = (MagicMock(), None, msg) rabbitmq = RabbitMQClient('', '', '', '', '', 'queue_name') message = rabbitmq.get_message() self.assertIsNotNone(message) self.pika_mock.basic_get.assert_called_once_with('queue_name')
def test_read_tree_messages(self): rabbitmq = RabbitMQClient('', '', '', '', '', 'queue_name') msgs = [{ 'action': 'CREATE', 'type': 'vip', 'element': {} }, { 'action': 'CREATE', 'type': 'pool', 'element': {} }] rabbitmq.get_message = Mock(side_effect=msgs) msg_ret = rabbitmq.read_messages(3).next() self.assertEqual(len(msg_ret), 2) for i in range(2): self.assertDictEqual(msg_ret[i], msgs[i]) with self.assertRaises(StopIteration): rabbitmq.read_messages().next()
def test_nack_message(self): rabbitmq = RabbitMQClient('', '', '', '', '', 'queue_name') rabbitmq.nack_message(1) self.pika_mock.basic_nack.assert_called_once_with(1)