class RabbitMqAgent(ConfigurableCluAgent): """ Rabbitmq agent """ def __init__(self, config, rmqconf): ConfigurableCluAgent.__init__(self, config) defaults = {"channel": {"exchange":"", "type":""}, "messages":{"routing_key":""} } self.__defaults__(defaults) self.rmqclient = RabbitmqClient(rmqconf) def ensure_after_execute(self): try: ConfigurableCluAgent.ensure_after_execute(self) finally: self.rmqclient.disconnect() def basic_publish_json(self, message): real_message = {} real_message["id"] = self.id real_message["payload"] = message self.rmqclient.basic_publish_json(self.config.channel.exchange, self.config.channel.type, self.config.messages.routing_key, real_message)
def test_disconnect_exception(self): conn = Mock() close = Mock() close.side_effect=Exception("In your face") connection_valid = Mock() connection_valid.return_value = True config={"host":"host", "port":5656, "user":"******","password":"******"} co = RabbitmqClient(config) co._connection=conn conn.close=close co.is_connection_valid = connection_valid with self.assertRaises(RabbitmqClientException): co.disconnect()
def test_disconnect(self): conn = Mock() close = Mock() connection_valid = Mock() connection_valid.return_value = True conn.close=close config={"host":"host", "port":5656, "user":"******","password":"******"} co = RabbitmqClient(config) co.is_connection_valid = connection_valid co._connection=conn co.disconnect() close.assert_called_once_with() self.assertTrue(co._connection is None) self.assertTrue(co._channel is None)
def __init__(self, config, rmqconf): ConfigurableCluAgent.__init__(self, config) defaults = {"channel": {"exchange":"", "type":""}, "messages":{"routing_key":""} } self.__defaults__(defaults) self.rmqclient = RabbitmqClient(rmqconf)
def test_connect_exceptions(self): credpatcher = patch('pika.PlainCredentials') mockedcred = credpatcher.start() paramspatcher = patch('pika.ConnectionParameters') mockedparams = paramspatcher.start() conpatcher = patch('pika.BlockingConnection') mockedcon = conpatcher.start() mockedcon.side_effect=Exception("In your face") config={"host":"host", "port":5656, "user":"******","password":"******"} co = RabbitmqClient(config) with self.assertRaises(RabbitmqClientException): co.connect() credpatcher.stop() paramspatcher.stop() conpatcher.stop()
def test_connect(self): credpatcher = patch('pika.PlainCredentials') mockedcred = credpatcher.start() paramspatcher = patch('pika.ConnectionParameters') mockedparams = paramspatcher.start() conpatcher = patch('pika.BlockingConnection') mockedcon = conpatcher.start() config={"host":"host", "port":5656, "user":"******","password":"******"} co = RabbitmqClient(config) co.connect() self.assertFalse(co._channel is None) mockedcred.assert_called_with(config["user"], config["password"]) mockedparams.assert_called_with(host=config["host"], port=config["port"], credentials=mockedcred.return_value) mockedcon.assert_called_with(mockedparams.return_value) credpatcher.stop() paramspatcher.stop() conpatcher.stop()