def test_sender(self, uuid): url = 'test-url' address = 'test' uuid.return_value = '1234' connection = Connection(url) connection._impl = Mock() # test sender = connection.sender(address) # validation connection._impl.create_sender.assert_called_once_with(address, name=uuid.return_value) self.assertEqual(sender, connection._impl.create_sender.return_value)
def test_sender(self, uuid): url = 'test-url' address = 'test' uuid.return_value = '1234' connection = Connection(url) connection._impl = Mock() # test sender = connection.sender(address) # validation connection._impl.create_sender.assert_called_once_with( address, name=uuid.return_value) self.assertEqual(sender, connection._impl.create_sender.return_value)
class Sender(BaseSender): """ An AMQP message sender. :ivar connection: A proton connection. :type connection: Connection """ def __init__(self, url): """ :param url: The broker url. :type url: str """ BaseSender.__init__(self, url) self.connection = Connection(url) def is_open(self): """ Get whether the messenger has been opened. :return: True if open. :rtype bool """ return self.connection.is_open() @reliable def open(self): """ Open the sender. """ if self.is_open(): # already opened return self.connection.open() def repair(self): """ Repair the sender. """ self.close() self.connection.close() self.connection.open() def close(self): """ Close the sender. """ pass @resend def send(self, address, content, ttl=None): """ Send a message. :param address: An AMQP address. :type address: str :param content: The message content :type content: buf :param ttl: Time to Live (seconds) :type ttl: float """ sender = self.connection.sender(address) try: message = build_message(content, ttl, self.durable) sender.send(message) log.debug('sent (%s)', address) finally: sender.close()
class Method(Messenger): """ QMF method. :ivar url: The broker url. :type url: str :ivar name: The method name. :type name: str :ivar arguments: The method arguments. :type arguments: dict :ivar connection: A broker connection. :type connection: Connection :ivar sender: A message sender. :type sender: proton.utils.BlockingSender :ivar receiver: A message sender. :type receiver: proton.utils.BlockingReceiver """ def __init__(self, url, name, arguments): """ :param url: The broker url. :type url: str :param name: The method name. :type name: str :param arguments: The method arguments. :type arguments: dict """ super(Method, self).__init__(url) self.name = name self.arguments = arguments self.connection = Connection(url) self.sender = None self.receiver = None @property def body(self): return { '_object_id': OBJECT_ID, '_method_name': self.name, '_arguments': self.arguments } @property def properties(self): return { 'qmf.opcode': '_method_request', 'x-amqp-0-10.app-id': 'qmf2', 'method': 'request' } @resend def send(self, request): """ Send the request. :param request: A QMF request. """ self.sender.send(request) def on_reply(self, reply): """ Process the QMF reply. :param reply: The reply. :type reply: Message :raise: Error on failures. """ body = dict(reply.body) opcode = reply.properties['qmf.opcode'] if opcode != '_exception': # succeeded return values = body['_values'] code = values['error_code'] description = values['error_text'] if code == EEXIST: return raise Error(description, code) def is_open(self): """ Get whether the method is open. :return: True if open. :rtype: bool """ return self.sender is not None def open(self): """ Open a connection and get a sender and receiver. """ if self.is_open(): # already open return self.connection.open() self.receiver = self.connection.receiver(ADDRESS, dynamic=True) self.sender = self.connection.sender(ADDRESS) def repair(self): """ Repair the connection and get a sender and receiver. """ self.close() self.connection.close() self.connection.open() self.receiver = self.connection.receiver(ADDRESS, dynamic=True) self.sender = self.connection.sender(ADDRESS) def close(self): """ Close the sender and receiver. """ sender = self.sender self.sender = None receiver = self.receiver self.receiver = None try: sender.close() except Exception: pass try: receiver.close() except Exception: pass @reliable def __call__(self): """ Invoke the method. :raise: Error on failure. """ self.open() try: reply_to = self.receiver.remote_source.address request = Message(body=self.body, reply_to=reply_to, properties=self.properties, correlation_id=utf8(uuid4()), subject=SUBJECT) self.send(request) reply = self.receiver.receive() self.on_reply(reply) finally: self.close()
class Method(Messenger): """ QMF method. :ivar url: The broker url. :type url: str :ivar name: The method name. :type name: str :ivar arguments: The method arguments. :type arguments: dict :ivar connection: A broker connection. :type connection: Connection :ivar sender: A message sender. :type sender: proton.utils.BlockingSender :ivar receiver: A message sender. :type receiver: proton.utils.BlockingReceiver """ def __init__(self, url, name, arguments): """ :param url: The broker url. :type url: str :param name: The method name. :type name: str :param arguments: The method arguments. :type arguments: dict """ super(Method, self).__init__(url) self.name = name self.arguments = arguments self.connection = Connection(url) self.sender = None self.receiver = None @property def body(self): return { '_object_id': OBJECT_ID, '_method_name': self.name, '_arguments': self.arguments } @property def properties(self): return { 'qmf.opcode': '_method_request', 'x-amqp-0-10.app-id': 'qmf2', 'method': 'request' } @resend def send(self, request): """ Send the request. :param request: A QMF request. """ self.sender.send(request) def on_reply(self, reply): """ Process the QMF reply. :param reply: The reply. :type reply: Message :raise: Error on failures. """ body = dict(reply.body) opcode = reply.properties['qmf.opcode'] if opcode != '_exception': # succeeded return values = body['_values'] code = values['error_code'] description = values['error_text'] if code == EEXIST: return raise Error(description, code) def is_open(self): """ Get whether the method is open. :return: True if open. :rtype: bool """ return self.sender is not None def open(self): """ Open a connection and get a sender and receiver. """ if self.is_open(): # already open return self.connection.open() self.receiver = self.connection.receiver(ADDRESS, dynamic=True) self.sender = self.connection.sender(ADDRESS) def repair(self): """ Repair the connection and get a sender and receiver. """ self.close() self.connection.close() self.connection.open() self.receiver = self.connection.receiver(ADDRESS, dynamic=True) self.sender = self.connection.sender(ADDRESS) def close(self): """ Close the sender and receiver. """ sender = self.sender self.sender = None receiver = self.receiver self.receiver = None try: sender.close() except Exception: pass try: receiver.close() except Exception: pass @reliable def __call__(self): """ Invoke the method. :raise: Error on failure. """ self.open() try: reply_to = self.receiver.remote_source.address request = Message( body=self.body, reply_to=reply_to, properties=self.properties, correlation_id=utf8(uuid4()), subject=SUBJECT) self.send(request) reply = self.receiver.receive() self.on_reply(reply) finally: self.close()