def test_send_reply(self): req = Mock() req.content_type = 'application/json' req.content_encoding = 'binary' req.properties = {'reply_to': 'hello', 'correlation_id': 'world'} channel = Mock() exchange = Mock() exchange.is_bound = True exchange.channel = channel producer = Mock() producer.channel = channel producer.channel.connection.client.declared_entities = set() send_reply(exchange, req, {'hello': 'world'}, producer) self.assertTrue(producer.publish.call_count) args = producer.publish.call_args self.assertDictEqual(args[0][0], {'hello': 'world'}) self.assertDictEqual(args[1], {'exchange': exchange, 'routing_key': 'hello', 'correlation_id': 'world', 'serializer': 'json', 'retry': False, 'retry_policy': None, 'content_encoding': 'binary'})
def test_send_reply(self): req = Mock() req.content_type = 'application/json' req.content_encoding = 'binary' req.properties = {'reply_to': 'hello', 'correlation_id': 'world'} channel = Mock() exchange = Mock() exchange.is_bound = True exchange.channel = channel producer = Mock() producer.channel = channel producer.channel.connection.client.declared_entities = set() send_reply(exchange, req, {'hello': 'world'}, producer) assert producer.publish.call_count args = producer.publish.call_args assert args[0][0] == {'hello': 'world'} assert args[1] == { 'exchange': exchange, 'routing_key': 'hello', 'correlation_id': 'world', 'serializer': 'json', 'retry': False, 'retry_policy': None, 'content_encoding': 'binary', }
def test_send_reply(self): req = Mock() req.content_type = 'application/json' req.properties = {'reply_to': 'hello', 'correlation_id': 'world'} channel = Mock() exchange = Mock() exchange.is_bound = True exchange.channel = channel producer = Mock() producer.channel = channel producer.channel.connection.client.declared_entities = set() send_reply(exchange, req, {'hello': 'world'}, producer) self.assertTrue(producer.publish.call_count) args = producer.publish.call_args self.assertDictEqual(args[0][0], {'hello': 'world'}) self.assertDictEqual( args[1], { 'exchange': exchange, 'routing_key': 'hello', 'correlation_id': 'world', 'serializer': 'json' }) exchange.declare.assert_called_with()
def set_message(self, message, body): if body.get('kwargs'): folder = body.get('kwargs')['folder'] qcow_info, _, _, _ = parse_dirs(folder) else: qcow_info, _, _, _ = parse_dirs() with producers[self.connection].acquire(block=True) as producer: #get producer from pool if qcow_info == []: err_mess = "There are no qcow2 files in directory".upper() send_reply(TASK_EXCHANGE, message, err_mess, producer=producer) else: send_reply(TASK_EXCHANGE, message, qcow_info, producer=producer) print("[X] Reply message sent.".upper()) print("---------------------------------") return
def test_send_reply(self): req = Mock() req.content_type = "application/json" req.properties = {"reply_to": "hello", "correlation_id": "world"} exchange = Mock() exchange.is_bound = True producer = Mock() send_reply(exchange, req, {"hello": "world"}, producer) self.assertTrue(producer.publish.call_count) args = producer.publish.call_args self.assertDictEqual(args[0][0], {"hello": "world"}) self.assertDictEqual(args[1], {"exchange": exchange, "routing_key": "hello", "correlation_id": "world", "serializer": "json"}) exchange.declare.assert_called_with()
def test_send_reply(self): req = Mock() req.content_type = "application/json" req.properties = {"reply_to": "hello", "correlation_id": "world"} exchange = Mock() exchange.is_bound = True producer = Mock() producer.channel.connection.client.declared_entities = set() send_reply(exchange, req, {"hello": "world"}, producer) self.assertTrue(producer.publish.call_count) args = producer.publish.call_args self.assertDictEqual(args[0][0], {"hello": "world"}) self.assertDictEqual(args[1], {"exchange": exchange, "routing_key": "hello", "correlation_id": "world", "serializer": "json"}) exchange.declare.assert_called_with()
def respond_to_client(self, message, response=None, queue_name=None): """Send RPC response back to client. :response: datastructure that needs to go back to client. """ if response is None: response = {} if queue_name is None: queue_name = __name__ logger.debug("Replying to queue {!r} with properties: {!r}".format( message.properties['reply_to'], message.properties['correlation_id'])) with Connection(**CONN_DICT) as conn: with producers[conn].acquire(block=True) as producer: try: send_reply(self._exchange, message, response, producer, retry=True, retry_policy={ 'max_retries': 3, 'interval_start': 0, 'interval_step': 0.2, 'interval_max': 0.2, }) except exceptions.AMQPError: logger.error("Problem communicating with rabbit", exc_info=True) except KeyError: logger.error('Missing key in request', exc_info=True) except Exception: logger.error('Unable to reply to request', exc_info=True) else: correlation_id = message.properties['correlation_id'] logger.info('STOPSERVICE:%s;CORRELATION_ID:%s' % (__name__, correlation_id)) logger.debug('Replied with response {!r}'.format(response))