2011-04-06 13:59:16 +00:00
|
|
|
import random
|
2010-11-11 14:15:17 +00:00
|
|
|
import socket
|
2011-04-06 13:59:16 +00:00
|
|
|
import string
|
|
|
|
import sys
|
2010-11-11 14:15:17 +00:00
|
|
|
import time
|
|
|
|
import unittest2 as unittest
|
2011-04-06 13:59:16 +00:00
|
|
|
import warnings
|
2011-04-07 13:53:29 +00:00
|
|
|
import weakref
|
2010-11-11 14:15:17 +00:00
|
|
|
|
|
|
|
from nose import SkipTest
|
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
from kombu import Connection
|
2011-06-07 14:46:11 +00:00
|
|
|
from kombu import Exchange, Queue
|
2011-04-07 13:53:29 +00:00
|
|
|
from kombu.tests.utils import skip_if_quick
|
2010-11-11 14:15:17 +00:00
|
|
|
|
2011-04-06 13:59:16 +00:00
|
|
|
if sys.version_info >= (2, 5):
|
|
|
|
from hashlib import sha256 as _digest
|
|
|
|
else:
|
2011-04-21 16:29:51 +00:00
|
|
|
from sha import new as _digest # noqa
|
2010-11-11 14:15:17 +00:00
|
|
|
|
2011-04-06 13:59:16 +00:00
|
|
|
|
|
|
|
def say(msg):
|
|
|
|
sys.stderr.write(unicode(msg) + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
def consumeN(conn, consumer, n=1, timeout=30):
|
2010-11-11 14:15:17 +00:00
|
|
|
messages = []
|
|
|
|
|
|
|
|
def callback(message_data, message):
|
|
|
|
messages.append(message_data)
|
|
|
|
message.ack()
|
|
|
|
|
|
|
|
prev, consumer.callbacks = consumer.callbacks, [callback]
|
|
|
|
consumer.consume()
|
|
|
|
|
2011-04-06 13:59:16 +00:00
|
|
|
seconds = 0
|
2010-11-11 14:15:17 +00:00
|
|
|
while True:
|
2010-11-12 12:30:30 +00:00
|
|
|
try:
|
|
|
|
conn.drain_events(timeout=1)
|
|
|
|
except socket.timeout:
|
2011-04-06 13:59:16 +00:00
|
|
|
seconds += 1
|
|
|
|
msg = "Received %s/%s messages. %s seconds passed." % (
|
|
|
|
len(messages), n, seconds)
|
|
|
|
if seconds >= timeout:
|
|
|
|
raise socket.timeout(msg)
|
|
|
|
if seconds > 1:
|
|
|
|
say(msg)
|
2010-11-11 14:15:17 +00:00
|
|
|
if len(messages) >= n:
|
|
|
|
break
|
|
|
|
|
|
|
|
consumer.cancel()
|
|
|
|
consumer.callback = prev
|
|
|
|
return messages
|
|
|
|
|
|
|
|
|
|
|
|
class TransportCase(unittest.TestCase):
|
|
|
|
transport = None
|
|
|
|
prefix = None
|
2011-05-15 11:19:34 +00:00
|
|
|
sep = '.'
|
|
|
|
userid = None
|
|
|
|
password = None
|
2010-11-11 14:15:17 +00:00
|
|
|
event_loop_max = 100
|
2010-11-12 10:33:02 +00:00
|
|
|
connection_options = {}
|
2011-05-25 13:35:04 +00:00
|
|
|
suppress_disorder_warning = False
|
2011-06-07 14:20:09 +00:00
|
|
|
reliable_purge = True
|
2010-11-12 10:33:02 +00:00
|
|
|
|
|
|
|
connected = False
|
|
|
|
skip_test_reason = None
|
|
|
|
|
2011-04-06 13:59:16 +00:00
|
|
|
message_size_limit = None
|
|
|
|
|
2010-11-12 10:33:02 +00:00
|
|
|
def before_connect(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after_connect(self, connection):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
if self.transport:
|
|
|
|
try:
|
|
|
|
self.before_connect()
|
2012-11-08 20:20:41 +00:00
|
|
|
except SkipTest as exc:
|
2010-11-12 10:33:02 +00:00
|
|
|
self.skip_test_reason = str(exc)
|
|
|
|
else:
|
|
|
|
self.do_connect()
|
|
|
|
self.exchange = Exchange(self.prefix, "direct")
|
|
|
|
self.queue = Queue(self.prefix, self.exchange, self.prefix)
|
2010-11-11 14:15:17 +00:00
|
|
|
|
|
|
|
def purge(self, names):
|
|
|
|
chan = self.connection.channel()
|
2011-06-07 14:20:09 +00:00
|
|
|
total = 0
|
|
|
|
for queue in names:
|
|
|
|
while 1:
|
|
|
|
# ensure the queue is completly empty
|
|
|
|
purged = chan.queue_purge(queue=queue)
|
|
|
|
if not purged:
|
|
|
|
break
|
|
|
|
total += purged
|
|
|
|
chan.close()
|
|
|
|
return total
|
2010-11-11 14:15:17 +00:00
|
|
|
|
2011-03-28 15:40:44 +00:00
|
|
|
def get_connection(self, **options):
|
2011-05-15 11:19:34 +00:00
|
|
|
if self.userid:
|
|
|
|
options.setdefault("userid", self.userid)
|
|
|
|
if self.password:
|
|
|
|
options.setdefault("password", self.password)
|
2012-06-24 15:32:17 +00:00
|
|
|
return Connection(transport=self.transport, **options)
|
2011-03-28 15:40:44 +00:00
|
|
|
|
2010-11-11 14:15:17 +00:00
|
|
|
def do_connect(self):
|
2011-03-28 15:40:44 +00:00
|
|
|
self.connection = self.get_connection(**self.connection_options)
|
2010-11-11 14:15:17 +00:00
|
|
|
try:
|
|
|
|
self.connection.connect()
|
|
|
|
self.after_connect(self.connection)
|
|
|
|
except self.connection.connection_errors:
|
2010-11-12 10:33:02 +00:00
|
|
|
self.skip_test_reason = "%s transport can't connect" % (
|
|
|
|
self.transport, )
|
2010-11-11 14:15:17 +00:00
|
|
|
else:
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
def verify_alive(self):
|
2011-04-06 13:59:16 +00:00
|
|
|
if self.transport:
|
|
|
|
if not self.connected:
|
|
|
|
raise SkipTest(self.skip_test_reason)
|
|
|
|
return True
|
2010-11-11 14:15:17 +00:00
|
|
|
|
2011-06-07 14:20:09 +00:00
|
|
|
def purge_consumer(self, consumer):
|
|
|
|
return self.purge([queue.name for queue in consumer.queues])
|
|
|
|
|
2010-11-11 14:15:17 +00:00
|
|
|
def test_produce__consume(self):
|
2011-04-06 13:59:16 +00:00
|
|
|
if not self.verify_alive():
|
2010-11-11 14:15:17 +00:00
|
|
|
return
|
|
|
|
chan1 = self.connection.channel()
|
2011-06-07 14:20:09 +00:00
|
|
|
consumer = chan1.Consumer(self.queue)
|
|
|
|
self.purge_consumer(consumer)
|
|
|
|
producer = chan1.Producer(self.exchange)
|
2010-11-11 14:15:17 +00:00
|
|
|
producer.publish({"foo": "bar"}, routing_key=self.prefix)
|
|
|
|
message = consumeN(self.connection, consumer)
|
|
|
|
self.assertDictEqual(message[0], {"foo": "bar"})
|
|
|
|
chan1.close()
|
|
|
|
self.purge([self.queue.name])
|
|
|
|
|
2011-06-07 14:20:09 +00:00
|
|
|
def test_purge(self):
|
|
|
|
if not self.verify_alive():
|
|
|
|
return
|
|
|
|
chan1 = self.connection.channel()
|
|
|
|
consumer = chan1.Consumer(self.queue)
|
|
|
|
self.purge_consumer(consumer)
|
|
|
|
|
|
|
|
producer = chan1.Producer(self.exchange)
|
|
|
|
for i in xrange(10):
|
|
|
|
producer.publish({"foo": "bar"}, routing_key=self.prefix)
|
|
|
|
if self.reliable_purge:
|
|
|
|
self.assertEqual(consumer.purge(), 10)
|
|
|
|
self.assertEqual(consumer.purge(), 0)
|
|
|
|
else:
|
|
|
|
purged = 0
|
|
|
|
while purged < 9:
|
|
|
|
purged += self.purge_consumer(consumer)
|
|
|
|
|
2011-04-06 13:59:16 +00:00
|
|
|
def _digest(self, data):
|
|
|
|
return _digest(data).hexdigest()
|
|
|
|
|
2011-04-07 13:53:29 +00:00
|
|
|
@skip_if_quick
|
2011-04-06 13:59:16 +00:00
|
|
|
def test_produce__consume_large_messages(self, bytes=1048576, n=10,
|
|
|
|
charset=string.punctuation + string.letters + string.digits):
|
|
|
|
if not self.verify_alive():
|
|
|
|
return
|
|
|
|
bytes = min(filter(None, [bytes, self.message_size_limit]))
|
2011-04-21 16:29:51 +00:00
|
|
|
messages = ["".join(random.choice(charset)
|
2011-04-06 13:59:16 +00:00
|
|
|
for j in xrange(bytes)) + "--%s" % n
|
|
|
|
for i in xrange(n)]
|
|
|
|
digests = []
|
|
|
|
chan1 = self.connection.channel()
|
2012-02-03 18:11:06 +00:00
|
|
|
consumer = chan1.Consumer(self.queue)
|
|
|
|
self.purge_consumer(consumer)
|
2011-06-07 14:20:09 +00:00
|
|
|
producer = chan1.Producer(self.exchange)
|
2011-04-06 13:59:16 +00:00
|
|
|
for i, message in enumerate(messages):
|
|
|
|
producer.publish({"text": message,
|
|
|
|
"i": i}, routing_key=self.prefix)
|
|
|
|
digests.append(self._digest(message))
|
|
|
|
|
|
|
|
received = [(msg["i"], msg["text"])
|
|
|
|
for msg in consumeN(self.connection, consumer, n)]
|
|
|
|
self.assertEqual(len(received), n)
|
|
|
|
ordering = [i for i, _ in received]
|
2011-05-25 13:35:04 +00:00
|
|
|
if ordering != range(n) and not self.suppress_disorder_warning:
|
2011-04-06 13:59:16 +00:00
|
|
|
warnings.warn(
|
|
|
|
"%s did not deliver messages in FIFO order: %r" % (
|
|
|
|
self.transport, ordering))
|
|
|
|
|
|
|
|
for i, text in received:
|
|
|
|
if text != messages[i]:
|
|
|
|
raise AssertionError("%i: %r is not %r" % (
|
|
|
|
i, text[-100:], messages[i][-100:]))
|
|
|
|
self.assertEqual(self._digest(text), digests[i])
|
|
|
|
|
|
|
|
chan1.close()
|
|
|
|
self.purge([self.queue.name])
|
|
|
|
|
2010-11-11 14:15:17 +00:00
|
|
|
def P(self, rest):
|
2011-05-15 11:19:34 +00:00
|
|
|
return "%s%s%s" % (self.prefix, self.sep, rest)
|
2010-11-11 14:15:17 +00:00
|
|
|
|
|
|
|
def test_produce__consume_multiple(self):
|
2011-04-06 13:59:16 +00:00
|
|
|
if not self.verify_alive():
|
2010-11-11 14:15:17 +00:00
|
|
|
return
|
|
|
|
chan1 = self.connection.channel()
|
2011-06-07 14:20:09 +00:00
|
|
|
producer = chan1.Producer(self.exchange)
|
2010-11-11 14:15:17 +00:00
|
|
|
b1 = Queue(self.P("b1"), self.exchange, "b1")(chan1)
|
|
|
|
b2 = Queue(self.P("b2"), self.exchange, "b2")(chan1)
|
|
|
|
b3 = Queue(self.P("b3"), self.exchange, "b3")(chan1)
|
|
|
|
[q.declare() for q in (b1, b2, b3)]
|
2011-06-07 14:20:09 +00:00
|
|
|
self.purge([b1.name, b2.name, b3.name])
|
2010-11-11 14:15:17 +00:00
|
|
|
|
|
|
|
producer.publish("b1", routing_key="b1")
|
|
|
|
producer.publish("b2", routing_key="b2")
|
|
|
|
producer.publish("b3", routing_key="b3")
|
|
|
|
chan1.close()
|
|
|
|
|
|
|
|
chan2 = self.connection.channel()
|
2011-06-07 14:20:09 +00:00
|
|
|
consumer = chan2.Consumer([b1, b2, b3])
|
2010-11-11 14:15:17 +00:00
|
|
|
messages = consumeN(self.connection, consumer, 3)
|
|
|
|
self.assertItemsEqual(messages, ["b1", "b2", "b3"])
|
|
|
|
chan2.close()
|
|
|
|
self.purge([self.P("b1"), self.P("b2"), self.P("b3")])
|
|
|
|
|
|
|
|
def test_timeout(self):
|
2011-04-06 13:59:16 +00:00
|
|
|
if not self.verify_alive():
|
2010-11-11 14:15:17 +00:00
|
|
|
return
|
|
|
|
chan = self.connection.channel()
|
|
|
|
self.purge([self.queue.name])
|
2011-06-07 14:20:09 +00:00
|
|
|
consumer = chan.Consumer(self.queue)
|
2010-11-11 14:15:17 +00:00
|
|
|
self.assertRaises(socket.timeout, self.connection.drain_events,
|
|
|
|
timeout=0.3)
|
|
|
|
consumer.cancel()
|
2011-06-07 14:20:09 +00:00
|
|
|
chan.close()
|
2010-11-11 14:15:17 +00:00
|
|
|
|
|
|
|
def test_basic_get(self):
|
2011-04-06 13:59:16 +00:00
|
|
|
if not self.verify_alive():
|
2010-11-11 14:15:17 +00:00
|
|
|
return
|
|
|
|
chan1 = self.connection.channel()
|
2011-06-07 14:20:09 +00:00
|
|
|
producer = chan1.Producer(self.exchange)
|
2010-11-11 14:15:17 +00:00
|
|
|
chan2 = self.connection.channel()
|
|
|
|
queue = Queue(self.P("basic_get"), self.exchange, "basic_get")
|
|
|
|
queue = queue(chan2)
|
|
|
|
queue.declare()
|
2010-11-12 10:33:02 +00:00
|
|
|
producer.publish({"basic.get": "this"}, routing_key="basic_get")
|
|
|
|
chan1.close()
|
|
|
|
|
2010-11-11 14:15:17 +00:00
|
|
|
for i in range(self.event_loop_max):
|
|
|
|
m = queue.get()
|
|
|
|
if m:
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
self.assertEqual(m.payload, {"basic.get": "this"})
|
2011-06-07 14:20:09 +00:00
|
|
|
self.purge([queue.name])
|
2010-11-11 14:15:17 +00:00
|
|
|
chan2.close()
|
|
|
|
|
2011-04-07 13:53:29 +00:00
|
|
|
def test_cyclic_reference_transport(self):
|
|
|
|
if not self.verify_alive():
|
|
|
|
return
|
|
|
|
|
|
|
|
def _createref():
|
|
|
|
conn = self.get_connection()
|
|
|
|
conn.transport
|
|
|
|
conn.close()
|
|
|
|
return weakref.ref(conn)
|
|
|
|
|
|
|
|
self.assertIsNone(_createref()())
|
|
|
|
|
|
|
|
def test_cyclic_reference_connection(self):
|
|
|
|
if not self.verify_alive():
|
|
|
|
return
|
|
|
|
|
|
|
|
def _createref():
|
|
|
|
conn = self.get_connection()
|
|
|
|
conn.connect()
|
|
|
|
conn.close()
|
|
|
|
return weakref.ref(conn)
|
|
|
|
|
|
|
|
self.assertIsNone(_createref()())
|
|
|
|
|
|
|
|
def test_cyclic_reference_channel(self):
|
|
|
|
if not self.verify_alive():
|
|
|
|
return
|
|
|
|
|
|
|
|
def _createref():
|
|
|
|
conn = self.get_connection()
|
|
|
|
conn.connect()
|
2011-07-15 11:42:36 +00:00
|
|
|
chanrefs = []
|
|
|
|
try:
|
|
|
|
for i in xrange(100):
|
2011-09-05 11:55:48 +00:00
|
|
|
channel = conn.channel()
|
2011-07-15 11:42:36 +00:00
|
|
|
chanrefs.append(weakref.ref(channel))
|
2011-09-05 11:55:48 +00:00
|
|
|
channel.close()
|
2011-07-15 11:42:36 +00:00
|
|
|
finally:
|
2011-09-05 11:55:48 +00:00
|
|
|
conn.close()
|
2011-07-15 11:42:36 +00:00
|
|
|
return chanrefs
|
|
|
|
|
|
|
|
for chanref in _createref():
|
|
|
|
self.assertIsNone(chanref())
|
2011-04-07 13:53:29 +00:00
|
|
|
|
2010-11-11 14:15:17 +00:00
|
|
|
def tearDown(self):
|
|
|
|
if self.transport and self.connected:
|
|
|
|
self.connection.close()
|