2016-04-06 20:14:05 +00:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-09-07 14:21:47 +00:00
|
|
|
|
2010-06-29 15:31:56 +00:00
|
|
|
from itertools import count
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
from case import ContextMock, Mock
|
|
|
|
|
2012-03-20 14:53:00 +00:00
|
|
|
from kombu.transport import base
|
2014-05-20 20:49:47 +00:00
|
|
|
from kombu.utils import json
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
def PromiseMock(*args, **kwargs):
|
|
|
|
m = Mock(*args, **kwargs)
|
|
|
|
|
|
|
|
def on_throw(exc=None, *args, **kwargs):
|
|
|
|
if exc:
|
|
|
|
raise exc
|
|
|
|
raise
|
|
|
|
m.throw.side_effect = on_throw
|
|
|
|
m.set_error_state.side_effect = on_throw
|
|
|
|
m.throw1.side_effect = on_throw
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
class MockPool(object):
|
|
|
|
|
|
|
|
def __init__(self, value=None):
|
|
|
|
self.value = value or ContextMock()
|
|
|
|
|
|
|
|
def acquire(self, **kwargs):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
|
2010-08-04 11:46:10 +00:00
|
|
|
class Message(base.Message):
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self.throw_decode_error = kwargs.get('throw_decode_error', False)
|
2010-06-29 15:31:56 +00:00
|
|
|
super(Message, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def decode(self):
|
|
|
|
if self.throw_decode_error:
|
|
|
|
raise ValueError("can't decode message")
|
|
|
|
return super(Message, self).decode()
|
|
|
|
|
|
|
|
|
2011-06-29 11:08:48 +00:00
|
|
|
class Channel(base.StdChannel):
|
2010-06-29 15:31:56 +00:00
|
|
|
open = True
|
|
|
|
throw_decode_error = False
|
2013-02-12 16:37:01 +00:00
|
|
|
_ids = count(1)
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2011-09-07 13:03:37 +00:00
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
2010-06-29 15:31:56 +00:00
|
|
|
self.called = []
|
2013-02-12 16:37:01 +00:00
|
|
|
self.deliveries = count(1)
|
2010-11-11 13:41:55 +00:00
|
|
|
self.to_deliver = []
|
2012-11-14 16:46:08 +00:00
|
|
|
self.events = {'basic_return': set()}
|
2013-02-12 16:37:01 +00:00
|
|
|
self.channel_id = next(self._ids)
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def _called(self, name):
|
|
|
|
self.called.append(name)
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self.called
|
|
|
|
|
|
|
|
def exchange_declare(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('exchange_declare')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2012-11-14 16:46:08 +00:00
|
|
|
def prepare_message(self, body, priority=0, content_type=None,
|
2013-01-17 13:50:01 +00:00
|
|
|
content_encoding=None, headers=None, properties={}):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('prepare_message')
|
2012-11-14 16:46:08 +00:00
|
|
|
return dict(body=body,
|
2010-06-29 15:31:56 +00:00
|
|
|
headers=headers,
|
|
|
|
properties=properties,
|
|
|
|
priority=priority,
|
|
|
|
content_type=content_type,
|
|
|
|
content_encoding=content_encoding)
|
|
|
|
|
2012-06-15 17:32:40 +00:00
|
|
|
def basic_publish(self, message, exchange='', routing_key='',
|
2013-01-17 13:50:01 +00:00
|
|
|
mandatory=False, immediate=False, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_publish')
|
2010-06-29 15:31:56 +00:00
|
|
|
return message, exchange, routing_key
|
|
|
|
|
|
|
|
def exchange_delete(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('exchange_delete')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def queue_declare(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('queue_declare')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def queue_bind(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('queue_bind')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2010-11-11 11:52:57 +00:00
|
|
|
def queue_unbind(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('queue_unbind')
|
2010-11-11 11:52:57 +00:00
|
|
|
|
2010-07-31 14:41:35 +00:00
|
|
|
def queue_delete(self, queue, if_unused=False, if_empty=False, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('queue_delete')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_get(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_get')
|
2010-11-11 13:41:55 +00:00
|
|
|
try:
|
|
|
|
return self.to_deliver.pop()
|
|
|
|
except IndexError:
|
|
|
|
pass
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def queue_purge(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('queue_purge')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_consume(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_consume')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_cancel(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_cancel')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_ack(self, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_ack')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_recover(self, requeue=False):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_recover')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2012-09-21 13:13:48 +00:00
|
|
|
def exchange_bind(self, *args, **kwargs):
|
|
|
|
self._called('exchange_bind')
|
|
|
|
|
|
|
|
def exchange_unbind(self, *args, **kwargs):
|
|
|
|
self._called('exchange_unbind')
|
|
|
|
|
2010-11-11 13:41:55 +00:00
|
|
|
def close(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('close')
|
2010-11-11 13:41:55 +00:00
|
|
|
|
2010-06-29 15:31:56 +00:00
|
|
|
def message_to_python(self, message, *args, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('message_to_python')
|
2014-05-20 20:49:47 +00:00
|
|
|
return Message(self, body=json.dumps(message),
|
2013-02-12 16:37:01 +00:00
|
|
|
delivery_tag=next(self.deliveries),
|
2013-01-17 13:50:01 +00:00
|
|
|
throw_decode_error=self.throw_decode_error,
|
|
|
|
content_type='application/json',
|
|
|
|
content_encoding='utf-8')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def flow(self, active):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('flow')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_reject(self, delivery_tag, requeue=False):
|
|
|
|
if requeue:
|
2012-06-15 17:32:40 +00:00
|
|
|
return self._called('basic_reject:requeue')
|
|
|
|
return self._called('basic_reject')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def basic_qos(self, prefetch_size=0, prefetch_count=0,
|
2013-01-17 13:50:01 +00:00
|
|
|
apply_global=False):
|
2012-06-15 17:32:40 +00:00
|
|
|
self._called('basic_qos')
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Connection(object):
|
|
|
|
connected = True
|
|
|
|
|
2011-09-07 13:03:37 +00:00
|
|
|
def __init__(self, client):
|
|
|
|
self.client = client
|
|
|
|
|
2010-06-29 15:31:56 +00:00
|
|
|
def channel(self):
|
2011-09-07 13:03:37 +00:00
|
|
|
return Channel(self)
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
|
2010-08-04 11:46:10 +00:00
|
|
|
class Transport(base.Transport):
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def establish_connection(self):
|
2011-09-07 13:03:37 +00:00
|
|
|
return Connection(self.client)
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def create_channel(self, connection):
|
|
|
|
return connection.channel()
|
|
|
|
|
2010-07-31 14:41:35 +00:00
|
|
|
def drain_events(self, connection, **kwargs):
|
2012-06-15 17:32:40 +00:00
|
|
|
return 'event'
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def close_connection(self, connection):
|
|
|
|
connection.connected = False
|