def __init__(self, channel, name, no_ack=None, queue_opts=None, exchange_opts=None, serializer=None, compression=None, **kwargs): queue = name queue_opts = dict(self.queue_opts, **queue_opts or {}) exchange_opts = dict(self.exchange_opts, **exchange_opts or {}) if no_ack is None: no_ack = self.no_ack if not isinstance(queue, entity.Queue): exchange = entity.Exchange(name, "direct", **exchange_opts) queue = entity.Queue(name, exchange, name, **queue_opts) else: name = queue.name exchange = queue.exchange producer = messaging.Producer(channel, exchange, serializer=serializer, routing_key=name, compression=compression) consumer = messaging.Consumer(channel, queue) super(SimpleQueue, self).__init__(channel, producer, consumer, no_ack, **kwargs)
def test_predefined_queues_get_from_sqs(self): connection = Connection(transport=SQS.Transport, transport_options={ 'predefined_queues': example_predefined_queues, }) channel = connection.channel() def message_to_python(message, queue_name, queue): return message channel._message_to_python = Mock(side_effect=message_to_python) queue_name = "queue-1" exchange = Exchange('test_SQS', type='direct') p = messaging.Producer(channel, exchange, routing_key=queue_name) queue = Queue(queue_name, exchange, queue_name) queue(channel).declare() # Getting a single message p.publish('message') result = channel._get(queue_name) assert 'Body' in result.keys() # Getting many messages for i in range(3): p.publish('message: {0}'.format(i)) channel.connection._deliver = Mock(name='_deliver') channel._get_bulk(queue_name, max_if_unlimited=3) channel.connection._deliver.assert_called() assert len(channel.sqs(queue_name)._queues[queue_name].messages) == 0
def test_predefined_queues_put_to_fifo_queue(self): connection = Connection(transport=SQS.Transport, transport_options={ 'predefined_queues': example_predefined_queues, }) channel = connection.channel() queue_name = 'queue-3.fifo' exchange = Exchange('test_SQS', type='direct') p = messaging.Producer(channel, exchange, routing_key=queue_name) queue = Queue(queue_name, exchange, queue_name) queue(channel).declare() channel.sqs = Mock() sqs_queue_mock = Mock() channel.sqs.return_value = sqs_queue_mock p.publish('message') sqs_queue_mock.send_message.assert_called_once() assert 'MessageGroupId' in sqs_queue_mock.send_message.call_args[1] assert 'MessageDeduplicationId' in \ sqs_queue_mock.send_message.call_args[1]
def __init__(self, channel, name, no_ack=None, queue_opts=None, queue_args=None, exchange_opts=None, serializer=None, compression=None, **kwargs): queue = name queue_opts = dict(self.queue_opts, **queue_opts or {}) queue_args = dict(self.queue_args, **queue_args or {}) exchange_opts = dict(self.exchange_opts, **exchange_opts or {}) if no_ack is None: no_ack = self.no_ack if not isinstance(queue, entity.Queue): exchange = entity.Exchange(name, **exchange_opts) queue = entity.Queue(name, exchange, name, queue_arguments=queue_args, **queue_opts) routing_key = name else: exchange = queue.exchange routing_key = queue.routing_key consumer = messaging.Consumer(channel, queue) producer = messaging.Producer(channel, exchange, serializer=serializer, routing_key=routing_key, compression=compression) super(AmqpBuffer, self).__init__(channel, producer, consumer, no_ack, **kwargs)
def setup(self): """Mock the back-end SQS classes""" # Sanity check... if SQS is None, then it did not import and we # cannot execute our tests. SQS.Channel._queue_cache.clear() # Common variables used in the unit tests self.queue_name = 'unittest' # Mock the sqs() method that returns an SQSConnection object and # instead return an SQSConnectionMock() object. sqs_conn_mock = SQSClientMock() self.sqs_conn_mock = sqs_conn_mock predefined_queues_sqs_conn_mocks = { 'queue-1': SQSClientMock(QueueName='queue-1'), 'queue-2': SQSClientMock(QueueName='queue-2'), 'queue-3.fifo': SQSClientMock(QueueName='queue-3.fifo') } def mock_sqs(): def sqs(self, queue=None): if queue in predefined_queues_sqs_conn_mocks: return predefined_queues_sqs_conn_mocks[queue] return sqs_conn_mock return sqs SQS.Channel.sqs = mock_sqs() # Set up a task exchange for passing tasks through the queue self.exchange = Exchange('test_SQS', type='direct') self.queue = Queue(self.queue_name, self.exchange, self.queue_name) # Mock up a test SQS Queue with the QueueMock class (and always # make sure its a clean empty queue) self.sqs_queue_mock = QueueMock('sqs://' + self.queue_name) # Now, create our Connection object with the SQS Transport and store # the connection/channel objects as references for use in these tests. self.connection = Connection(transport=SQS.Transport) self.channel = self.connection.channel() self.queue(self.channel).declare() self.producer = messaging.Producer(self.channel, self.exchange, routing_key=self.queue_name) # Lastly, make sure that we're set up to 'consume' this queue. self.channel.basic_consume(self.queue_name, no_ack=False, callback=self.handleMessageCallback, consumer_tag='unittest')
def mock_queue(mock_asb, mock_asb_management, random_queue) -> MockQueue: exchange = Exchange('test_servicebus', type='direct') queue = Queue(random_queue, exchange, random_queue) conn = Connection(URL_CREDS, transport=azureservicebus.Transport) channel = conn.channel() channel._queue_service = mock_asb channel._queue_mgmt_service = mock_asb_management queue(channel).declare() producer = messaging.Producer(channel, exchange, routing_key=random_queue) return MockQueue(random_queue, mock_asb, mock_asb_management, conn, channel, producer, queue)
def setUp(self): """Mock the back-end SQS classes""" # Sanity check... if SQS is None, then it did not import and we # cannot execute our tests. if SQS is None: raise SkipTest(skip_reason) SQS.Channel._queue_cache.clear() # Common variables used in the unit tests self.queue_name = 'unittest' # Mock the sqs() method that returns an SQSConnection object and # instead return an SQSConnectionMock() object. self.sqs_conn_mock = SQSConnectionMock() def mock_sqs(): return self.sqs_conn_mock SQS.Channel.sqs = mock_sqs() # Set up a task exchange for passing tasks through the queue self.exchange = kombu.Exchange('test_SQS', type='direct') self.queue = kombu.Queue(self.queue_name, self.exchange, self.queue_name) # Mock up a test SQS Queue with the SQSQueueMock class (and always # make sure its a clean empty queue) self.sqs_queue_mock = SQSQueueMock(self.queue_name) # Now, create our Connection object with the SQS Transport and store # the connection/channel objects as references for use in these tests. self.connection = Connection(transport=SQS.Transport) self.channel = self.connection.channel() self.queue(self.channel).declare() self.producer = messaging.Producer(self.channel, self.exchange, routing_key=self.queue_name) # Lastly, make sure that we're set up to 'consume' this queue. self.channel.basic_consume(self.queue_name, no_ack=True, callback=self.handleMessageCallback, consumer_tag='unittest')
def setup(self): self.url = 'azureservicebus://' self.queue_name = 'unittest_queue' self.exchange = Exchange('test_servicebus', type='direct') self.queue = Queue(self.queue_name, self.exchange, self.queue_name) self.connection = _create_mock_connection(self.url) self.channel = self.connection.default_channel self.queue(self.channel).declare() self.producer = messaging.Producer(self.channel, self.exchange, routing_key=self.queue_name) self.channel.basic_consume(self.queue_name, no_ack=False, callback=self.handleMessageCallback, consumer_tag='unittest')
def __init__(self, channel, exchange, **kwargs): self._exchange_declare = kwargs.get("exchange_declare", False) if isinstance(exchange, Queue): self.exchange = exchange.exchange elif isinstance(exchange, basestring): self.exchange = Exchange(exchange, type="fanout") # , durable=True) else: assert isinstance(exchange, Exchange) self.exchange = exchange self.channel = maybe_channel(channel) self.exchange.maybe_bind(self.channel) if self._exchange_declare: self.exchange.declare() self.producer = messaging.Producer(channel, self.exchange, serializer='json', routing_key='', compression=None, auto_declare=False)
def __init__(self, name, force_new_connection=False): """@todo: to be defined :param name: @todo :param force_new_connection: @todo """ if not INITTED: log.warn("QUEUE INIT Not called, calling") init() self._name = RPC_PREFIX + name self._connection = connect(force_new_connection) self._channel = self._connection.channel() self._queue = self._connection.SimpleQueue( "", queue_opts={"exclusive": True}) self._marshal = queue.JsonMarshal() self._producer = messaging.Producer(self._channel, routing_key=self._name)
from kombu import messaging, Exchange exchange = Exchange('reto1') producer = messaging.Producer() producer.publish( {'hello': 'world'}, exchange=exchange # declares exchange, queue and binds. )