Ejemplo n.º 1
0
class TestRabbitMQSubscriber(object):

    @mock.patch('qpaca.backend.rabbitmq.InfluxDB')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def setup_class(self, mock1, mock2):
        self.subscriber = RabbitMQSubscriber()

    @mock.patch('qpaca.backend.rabbitmq.InfluxDB')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def test_call_create_monitor(self, mocked_class, mocked_function):
        RabbitMQSubscriber()
        assert mocked_class.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def test_call_connect(self, mocked_function):
        RabbitMQSubscriber()
        assert mocked_function.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_exchange')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_queue')
    def test_call_create_queue(self, mocked_exchange, mocked_queue):
        self.subscriber.start(callback=mock.Mock())
        assert mocked_queue.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_exchange')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_queue')
    def test_call_create_exchange(self, mocked_exchange, mocked_queue):
        self.subscriber.start(callback=mock.Mock())
        assert mocked_exchange.called

    def test_ack_message_on_message(self):
        message = mock.Mock()
        self.subscriber.on_message(
            body=None, message=message)
        assert message.ack.called

    def test_call_monitor_on_message(self):
        self.subscriber.on_message(body=None, message=mock.Mock())
        assert self.subscriber.monitor.write.called

    def test_call_callback_on_message(self):
        self.subscriber.on_message(body=None, message=mock.Mock())
        assert self.subscriber._callback.called

    def test_return_get_consumers(self):
        self.subscriber.config = {'consumer': {}}
        self.subscriber._queue = mock.Mock()
        self.subscriber.on_message = mock.Mock()
        consumers = self.subscriber.get_consumers(mock.Mock(), mock.Mock())
        assert type(consumers) is list
        assert len(consumers) == 1

    @mock.patch('kombu.Exchange')
    def test_create_exchange(self, mocked_exchange):
        self.subscriber.config = {'exchange': {}}
        exchange = self.subscriber._create_exchange()
        assert exchange is not None

    @mock.patch('kombu.Queue')
    def test_create_queue(self, mocked_queue):
        self.subscriber.config = {'queue': {}}
        connection = self.subscriber._create_queue()
        assert connection is not None

    @mock.patch('kombu.Connection')
    def test_connect(self, mocked_connection):
        self.subscriber.config = {'connection': {}}
        connection = self.subscriber._connect()
        assert connection is not None

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber.run')
    def test_call_consumer_run(self, mocked_function):
        self.subscriber.run_forever()
        assert mocked_function.called
Ejemplo n.º 2
0
 def setup_class(self, mock1, mock2):
     self.subscriber = RabbitMQSubscriber()
Ejemplo n.º 3
0
 def test_call_connect(self, mocked_function):
     RabbitMQSubscriber()
     assert mocked_function.called
Ejemplo n.º 4
0
 def test_call_create_monitor(self, mocked_class, mocked_function):
     RabbitMQSubscriber()
     assert mocked_class.called
Ejemplo n.º 5
0
 def setup_class(self, mock1, mock2):
     self.subscriber = RabbitMQSubscriber()
Ejemplo n.º 6
0
class TestRabbitMQSubscriber(object):
    @mock.patch('qpaca.backend.rabbitmq.InfluxDB')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def setup_class(self, mock1, mock2):
        self.subscriber = RabbitMQSubscriber()

    @mock.patch('qpaca.backend.rabbitmq.InfluxDB')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def test_call_create_monitor(self, mocked_class, mocked_function):
        RabbitMQSubscriber()
        assert mocked_class.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._connect')
    def test_call_connect(self, mocked_function):
        RabbitMQSubscriber()
        assert mocked_function.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_exchange')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_queue')
    def test_call_create_queue(self, mocked_exchange, mocked_queue):
        self.subscriber.start(callback=mock.Mock())
        assert mocked_queue.called

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_exchange')
    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber._create_queue')
    def test_call_create_exchange(self, mocked_exchange, mocked_queue):
        self.subscriber.start(callback=mock.Mock())
        assert mocked_exchange.called

    def test_ack_message_on_message(self):
        message = mock.Mock()
        self.subscriber.on_message(body=None, message=message)
        assert message.ack.called

    def test_call_monitor_on_message(self):
        self.subscriber.on_message(body=None, message=mock.Mock())
        assert self.subscriber.monitor.write.called

    def test_call_callback_on_message(self):
        self.subscriber.on_message(body=None, message=mock.Mock())
        assert self.subscriber._callback.called

    def test_return_get_consumers(self):
        self.subscriber.config = {'consumer': {}}
        self.subscriber._queue = mock.Mock()
        self.subscriber.on_message = mock.Mock()
        consumers = self.subscriber.get_consumers(mock.Mock(), mock.Mock())
        assert type(consumers) is list
        assert len(consumers) == 1

    @mock.patch('kombu.Exchange')
    def test_create_exchange(self, mocked_exchange):
        self.subscriber.config = {'exchange': {}}
        exchange = self.subscriber._create_exchange()
        assert exchange is not None

    @mock.patch('kombu.Queue')
    def test_create_queue(self, mocked_queue):
        self.subscriber.config = {'queue': {}}
        connection = self.subscriber._create_queue()
        assert connection is not None

    @mock.patch('kombu.Connection')
    def test_connect(self, mocked_connection):
        self.subscriber.config = {'connection': {}}
        connection = self.subscriber._connect()
        assert connection is not None

    @mock.patch('qpaca.backend.rabbitmq.RabbitMQSubscriber.run')
    def test_call_consumer_run(self, mocked_function):
        self.subscriber.run_forever()
        assert mocked_function.called
Ejemplo n.º 7
0
import gevent
import sys
sys.path.insert(0, '.')

from qpaca.backend.rabbitmq import RabbitMQSubscriber


def custom_callback(body, message):
    """
    Do something with received messages
    """
    pass


if __name__ == '__main__':
    """
    Create and start a Subscriber. Use gevent to spawn a subscriber greenlet,
    You can start more than one subscriber if you wish.
    """

    subscriber = RabbitMQSubscriber()
    subscriber.start(callback=custom_callback)
    g = gevent.spawn(subscriber.run_forever)
    g.join()