def setup_method(self):
     connection = Mock(Connection)
     connection.is_closed = False
     self.channel = ProducerChannel(connection,
                                    delivery_mode=2,
                                    application_id='TEST-APP_ID')
     self.channel.open()
     self.exchange = 'TEST-EXCHANGE'
     self.queue = 'TEST-QUEUE'
Exemple #2
0
 def setup(self):
     connection = ConnectionHandler(common.BROKER_URL_TEST)
     current_connection = connection.get_current_connection()
     self.exchange_name = 'TEST-EXCHANGE-NAME'
     self.application_id = 'TEST-APPLICATION-ID'
     self.delivery_mode = 2
     channel_handler = ProducerChannel(
         current_connection, self.application_id, self.delivery_mode)
     channel_handler.open()
     self.channel = channel_handler.get_channel()
     self.channel = Mock(ProducerChannel)
     self.message = "TEST-CONTENT-MESSAGE"
Exemple #3
0
    def init_app(self, app, queues, on_message_callback):
        """ Init the Broker by using the given configuration instead
        default settings.

        :param app: Current application context
        :param list queues: Queues which the message will be post
        :param callback on_message_callback: callback to execute when new
        message is pulled to RabbitMQ
        """
        self.url = app.config.get('RABBIT_MQ_URL', DEFAULT_URL)
        self.exchange_name = app.config.get('EXCHANGE_NAME', DEFAULT_EXCHANGE)
        self.application_id = app.config.get('APPLICATION_ID', DEFAULT_APP)
        self.delivery_mode = app.config.get('DELIVERY_MODE', DEFAULT_DELIVERY)
        self.queues = queues
        self.on_message_callback = on_message_callback

        # Open Connection to RabbitMQ
        self.connection_handler = ConnectionHandler(self.url)
        connection = self.connection_handler.get_current_connection()

        # Setup default producer for broker_rabbit
        channel = ProducerChannel(connection, self.application_id,
                                  self.delivery_mode)
        self.producer = Producer(channel, self.exchange_name)
        self.producer.bootstrap(self.queues)
Exemple #4
0
    def init_app(self, app, queues, on_message_callback):
        """ Init the Broker by using the given configuration instead
        default settings.

        :param app: Current application context
        :param list queues: Queues which the message will be post
        :param callback on_message_callback: callback to execute when new
        message is pulled to RabbitMQ
        """
        if not hasattr(app, 'extensions'):
            app.extensions = {}

        if 'broker_rabbit' not in app.extensions:
            app.extensions['broker_rabbit'] = self
        else:
            # Raise an exception if extension already initialized as
            # potentially new configuration would not be loaded.
            raise RuntimeError('Extension already initialized')

        self.url = app.config.get('RABBIT_MQ_URL', DEFAULT_URL)
        self.exchange_name = app.config.get('EXCHANGE_NAME', DEFAULT_EXCHANGE)
        self.application_id = app.config.get('APPLICATION_ID', DEFAULT_APP)
        self.delivery_mode = app.config.get('DELIVERY_MODE', DEFAULT_DELIVERY)
        self.queues = queues
        self.on_message_callback = on_message_callback

        # Open Connection to RabbitMQ
        self.connection_handler = ConnectionHandler(self.url)
        connection = self.connection_handler.get_current_connection()

        # Setup default producer for broker_rabbit
        channel = ProducerChannel(connection, self.application_id,
                                  self.delivery_mode)
        self.producer = Producer(channel, self.exchange_name)
        self.producer.bootstrap(self.queues)
class TestProducerChannel:
    def setup_method(self):
        connection = Mock(Connection)
        connection.is_closed = False
        self.channel = ProducerChannel(connection,
                                       delivery_mode=2,
                                       application_id='TEST-APP_ID')
        self.channel.open()
        self.exchange = 'TEST-EXCHANGE'
        self.queue = 'TEST-QUEUE'

    def teardown_method(self):
        self.channel.close()

    def test_send_message(self):
        # Given
        message = {'key': 'value', 'number': 1, 'foo': 'bar'}
        self.channel.basic_properties = 'TEST-PROPERTIES'

        # When
        self.channel.send_message(self.exchange, self.queue, message)

        # Then
        body = json.dumps(message)
        channel = self.channel.get_channel()
        channel.basic_publish.assert_called_with(exchange=self.exchange,
                                                 routing_key=self.queue,
                                                 body=body,
                                                 properties='TEST-PROPERTIES')

    def test_returns_correct_for_properties(self):
        # Given
        application_id = 'TEST-APPLICATION-ID'
        delivery_mode = 2

        # When
        properties = self.channel.apply_basic_properties(
            application_id, delivery_mode)

        # Then
        assert 'TEST-APPLICATION-ID' == properties.app_id
        assert 2 == properties.delivery_mode