Make rpc calls work in unit tests by adding extra declare_consumer and consume methods on the FakeRabbit backend.

This commit is contained in:
Soren Hansen
2010-10-05 15:21:08 +02:00
parent 0ac53ee6cb
commit 9687ba16d5

View File

@@ -38,6 +38,7 @@ class Exchange(object):
def publish(self, message, routing_key=None):
logging.debug('(%s) publish (key: %s) %s',
self.name, routing_key, message)
routing_key = routing_key.split('.')[0]
if routing_key in self._routes:
for f in self._routes[routing_key]:
logging.debug('Publishing to route %s', f)
@@ -94,6 +95,20 @@ class Backend(object):
self._exchanges[exchange].bind(self._queues[queue].push,
routing_key)
def declare_consumer(self, queue, callback, *args, **kwargs):
print 'declare_consumer', queue, callback
self.current_queue = queue
self.current_callback = callback
def consume(self, *args, **kwargs):
from eventlet import greenthread
while True:
item = self.get(self.current_queue)
if item:
self.current_callback(item)
raise StopIteration()
greenthread.sleep(0)
def get(self, queue, no_ack=False):
if not queue in self._queues or not self._queues[queue].size():
return None
@@ -102,6 +117,7 @@ class Backend(object):
message = Message(backend=self, body=message_data,
content_type=content_type,
content_encoding=content_encoding)
message.result = True
logging.debug('Getting from %s: %s', queue, message)
return message