Example #1
0
    def test_open(self, connection, ssl_domain, find):
        url = TEST_URL
        connector = Connector(url)
        connector.ssl.ca_certificate = 'test-ca'
        connector.ssl.client_key = 'test-key'
        connector.ssl.client_certificate = 'test-crt'
        find.return_value = connector

        # test
        c = Connection(url)
        c._ssh = Mock()
        c.open()

        # validation
        ssl_domain.assert_called_once_with(connector)
        connection.assert_called_once_with(
            host=':'.join((connector.host, str(connector.port))),
            virtual_host=connector.virtual_host,
            userid=connector.userid,
            password=connector.password,
            ssl=ssl_domain.return_value,
            confirm_publish=True)

        connection.return_value.connect.assert_called_once_with()
        self.assertEqual(c._impl, connection.return_value)
Example #2
0
 def test_close_failed(self):
     url = 'test-url'
     c = Connection(url)
     impl = Mock()
     impl.close.side_effect = ValueError
     c._impl = impl
     c.close()
Example #3
0
 def test_close(self):
     url = 'test-url'
     c = Connection(url)
     impl = Mock()
     c._impl = impl
     c.close()
     impl.close.assert_called_once_with()
     self.assertEqual(c._impl, None)
Example #4
0
 def __init__(self, node, url):
     """
     :param node: The AMQP node to read.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker url.
     :type url: str
     :see: gofer.messaging.adapter.url.URL
     """
     BaseReader.__init__(self, node, url)
     self.connection = Connection(url)
     self.channel = None
     self.receiver = None
Example #5
0
 def __init__(self, url):
     """
     :param url: The broker url.
     :type url: str
     """
     BaseSender.__init__(self, url)
     self.connection = Connection(url)
     self.channel = None
Example #6
0
    def test_ssl_not_ssl(self):
        url = 'amqp://*****:*****@redhat.com:1234'

        # test
        b = Connector(url)
        ssl = Connection.ssl_domain(b)

        self.assertEqual(str(b.url), url)

        # validation
        self.assertEqual(ssl, None)
Example #7
0
 def __init__(self, node, url):
     """
     :param node: The AMQP node to read.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker url.
     :type url: str
     :see: gofer.messaging.adapter.url.URL
     """
     BaseReader.__init__(self, node, url)
     self.connection = Connection(url)
     self.channel = None
     self.receiver = None
Example #8
0
    def test_ssl_no_certs(self):
        url = TEST_URL

        # test
        b = Connector(url)
        ssl = Connection.ssl_domain(b)

        # validation
        self.assertEqual(
            ssl,
            {
                'ca_certs': None,
                'certfile': None,
                'keyfile': None,
                'cert_reqs': 0
            })
Example #9
0
class Endpoint(Messenger):

    def __init__(self, url):
        super(Endpoint, self).__init__(url)
        self.connection = Connection(url)
        self.channel = None

    def open(self):
        self.connection.open()
        self.channel = self.connection.channel()
    
    def repair(self):
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        try:
            self.channel.close()
        except Exception:
            pass
Example #10
0
    def test_ssl_domain(self, validate):
        url = TEST_URL

        # test
        b = Connector(url)
        b.ssl.ca_certificate = 'test-ca'
        b.ssl.client_key = 'test-key'
        b.ssl.client_certificate = 'test-crt'
        ssl = Connection.ssl_domain(b)

        # validation
        validate.assert_called_once_with()
        self.assertEqual(
            ssl,
            {
                'ca_certs': b.ssl.ca_certificate,
                'cert_reqs': 2,
                'certfile': b.ssl.client_certificate,
                'keyfile': b.ssl.client_key
            })
Example #11
0
class Reader(BaseReader):
    """
    An AMQP message reader.
    """

    def __init__(self, node, url):
        """
        :param node: The AMQP node to read.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker url.
        :type url: str
        :see: gofer.messaging.adapter.url.URL
        """
        BaseReader.__init__(self, node, url)
        self.connection = Connection(url)
        self.channel = None
        self.receiver = None

    def is_open(self):
        """
        Get whether the messenger has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.receiver is not None

    @reliable
    def open(self):
        """
        Open the reader.
        :raise: NotFound
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def repair(self):
        """
        Repair the reader.
        :raise: NotFound
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def close(self):
        """
        Close the reader.
        """
        receiver = self.receiver
        self.receiver = None
        channel = self.channel
        self.channel = None
        try:
            receiver.close()
        except Exception:
            pass
        try:
            channel.close()
        except Exception:
            pass


    @reliable
    def get(self, timeout=None):
        """
        Get the next message from the queue.
        :param timeout: The read timeout in seconds.
        :type timeout: int
        :return: The next message or None.
        :rtype: Message
        """
        try:
            impl = self.receiver.fetch(timeout or NO_DELAY)
            return Message(self, impl, impl.body)
        except Empty:
            pass

    @reliable
    def ack(self, message):
        """
        Ack the specified message.
        :param message: The message to acknowledge.
        :type message: amqp.Message
        """
        self.channel.basic_ack(message.delivery_info[DELIVERY_TAG])

    @reliable
    def reject(self, message, requeue=True):
        """
        Reject the specified message.
        :param message: The message to reject.
        :type message: amqp.Message
        :param requeue: Requeue the message or discard it.
        :type requeue: bool
        """
        self.channel.basic_reject(message.delivery_info[DELIVERY_TAG], requeue)
Example #12
0
class Sender(BaseSender):
    """
    An AMQP message sender.
    """

    def __init__(self, url):
        """
        :param url: The broker url.
        :type url: str
        """
        BaseSender.__init__(self, url)
        self.connection = Connection(url)
        self.channel = None

    def is_open(self):
        """
        Get whether the sender has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.channel is not None

    @reliable
    def open(self):
        """
        Open the reader.
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()

    def repair(self):
        """
        Repair the reader.
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        """
        Close the reader.
        """
        channel = self.channel
        self.channel = None
        try:
            channel.close()
        except Exception:
            pass

    @reliable
    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
        """
        parts = address.split('/')
        if len(parts) > 1:
            exchange = parts[0]
        else:
            exchange = ''
        key = parts[-1]
        message = build_message(content, ttl, self.durable)
        self.channel.basic_publish(message, mandatory=True, exchange=exchange, routing_key=key)
        log.debug('sent (%s)', address)
Example #13
0
 def test_channel(self):
     url = TEST_URL
     c = Connection(url)
     c._impl = Mock()
     ch = c.channel()
     self.assertEqual(ch, c._impl.channel.return_value)
Example #14
0
 def test_init(self):
     url = TEST_URL
     c = Connection(url)
     self.assertTrue(isinstance(c, BaseConnection))
     self.assertEqual(c.url, url)
Example #15
0
 def test_open_already(self):
     url = TEST_URL
     c = Connection(url)
     c._impl = Mock()
     c.open()
     self.assertFalse(c._impl.open.called)
Example #16
0
 def __init__(self, url):
     super(Endpoint, self).__init__(url)
     self.connection = Connection(url)
     self.channel = None
Example #17
0
class Reader(BaseReader):
    """
    An AMQP message reader.
    """

    def __init__(self, node, url):
        """
        :param node: The AMQP node to read.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker url.
        :type url: str
        :see: gofer.messaging.adapter.url.URL
        """
        BaseReader.__init__(self, node, url)
        self.connection = Connection(url)
        self.channel = None
        self.receiver = None

    def is_open(self):
        """
        Get whether the messenger has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.receiver is not None

    @reliable
    def open(self):
        """
        Open the reader.
        :raise: NotFound
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def repair(self):
        """
        Repair the reader.
        :raise: NotFound
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def close(self):
        """
        Close the reader.
        """
        receiver = self.receiver
        self.receiver = None
        channel = self.channel
        self.channel = None
        try:
            receiver.close()
        except Exception:
            pass
        try:
            channel.close()
        except Exception:
            pass

    @reliable
    def get(self, timeout=None):
        """
        Get the next message from the queue.
        :param timeout: The read timeout in seconds.
        :type timeout: int
        :return: The next message or None.
        :rtype: Message
        """
        try:
            impl = self.receiver.fetch(timeout or NO_DELAY)
            return Message(self, impl, impl.body)
        except Empty:
            pass

    @reliable
    def ack(self, message):
        """
        Ack the specified message.
        :param message: The message to acknowledge.
        :type message: amqp.Message
        """
        self.channel.basic_ack(message.delivery_info[DELIVERY_TAG])

    @reliable
    def reject(self, message, requeue=True):
        """
        Reject the specified message.
        :param message: The message to reject.
        :type message: amqp.Message
        :param requeue: Requeue the message or discard it.
        :type requeue: bool
        """
        self.channel.basic_reject(message.delivery_info[DELIVERY_TAG], requeue)