1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#!/usr/bin/env python
"""
Test amqp.channel module
"""
# Copyright (C) 2007-2008 Barry Pederson <bp@barryp.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
import sys
import unittest
import settings
from amqp import ChannelError, Connection, Message, FrameSyntaxError
class TestChannel(unittest.TestCase):
def setUp(self):
self.conn = Connection(**settings.connect_args)
self.ch = self.conn.channel()
def tearDown(self):
self.ch.close()
self.conn.close()
def test_defaults(self):
"""Test how a queue defaults to being bound to an AMQP default
exchange, and how publishing defaults to the default exchange, and
basic_get defaults to getting from the most recently declared queue,
and queue_delete defaults to deleting the most recently declared
queue."""
msg = Message(
'funtest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'},
)
qname, _, _ = self.ch.queue_declare()
self.ch.basic_publish(msg, routing_key=qname)
msg2 = self.ch.basic_get(no_ack=True)
self.assertEqual(msg, msg2)
n = self.ch.queue_purge()
self.assertEqual(n, 0)
n = self.ch.queue_delete()
self.assertEqual(n, 0)
def test_encoding(self):
my_routing_key = 'funtest.test_queue'
qname, _, _ = self.ch.queue_declare()
self.ch.queue_bind(qname, 'amq.direct', routing_key=my_routing_key)
#
# No encoding, body passed through unchanged
#
msg = Message('hello world')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
if sys.version_info[0] < 3:
self.assertFalse(hasattr(msg2, 'content_encoding'))
self.assertTrue(isinstance(msg2.body, str))
self.assertEqual(msg2.body, 'hello world')
#
# Default UTF-8 encoding of unicode body, returned as unicode
#
msg = Message(u'hello world')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'UTF-8')
self.assertTrue(isinstance(msg2.body, unicode))
self.assertEqual(msg2.body, u'hello world')
#
# Explicit latin_1 encoding, still comes back as unicode
#
msg = Message(u'hello world', content_encoding='latin_1')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'latin_1')
self.assertTrue(isinstance(msg2.body, unicode))
self.assertEqual(msg2.body, u'hello world')
#
# Plain string with specified encoding comes back as unicode
#
msg = Message('hello w\xf6rld', content_encoding='latin_1')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'latin_1')
self.assertTrue(isinstance(msg2.body, unicode))
self.assertEqual(msg2.body, u'hello w\u00f6rld')
#
# Plain string (bytes in Python 3.x) with bogus encoding
#
# don't really care about latin_1, just want bytes
test_bytes = u'hello w\xd6rld'.encode('latin_1')
msg = Message(test_bytes, content_encoding='I made this up')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'I made this up')
self.assertTrue(isinstance(msg2.body, bytes))
self.assertEqual(msg2.body, test_bytes)
#
# Turn off auto_decode for remaining tests
#
self.ch.auto_decode = False
#
# Unicode body comes back as utf-8 encoded str
#
msg = Message(u'hello w\u00f6rld')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'UTF-8')
self.assertTrue(isinstance(msg2.body, bytes))
self.assertEqual(msg2.body, u'hello w\xc3\xb6rld'.encode('latin_1'))
#
# Plain string with specified encoding stays plain string
#
msg = Message('hello w\xf6rld', content_encoding='latin_1')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'latin_1')
self.assertTrue(isinstance(msg2.body, bytes))
self.assertEqual(msg2.body, u'hello w\xf6rld'.encode('latin_1'))
#
# Explicit latin_1 encoding, comes back as str
#
msg = Message(u'hello w\u00f6rld', content_encoding='latin_1')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg2.content_encoding, 'latin_1')
self.assertTrue(isinstance(msg2.body, bytes))
self.assertEqual(msg2.body, u'hello w\xf6rld'.encode('latin_1'))
def test_queue_delete_empty(self):
self.assertFalse(
self.ch.queue_delete('bogus_queue_that_does_not_exist')
)
def test_survives_channel_error(self):
with self.assertRaises(ChannelError):
self.ch.queue_declare('krjqheewq_bogus', passive=True)
self.ch.queue_declare('funtest_survive')
self.ch.queue_declare('funtest_survive', passive=True)
self.assertEqual(
0, self.ch.queue_delete('funtest_survive'),
)
def test_invalid_header(self):
"""
Test sending a message with an unserializable object in the header
http://code.google.com/p/py-amqplib/issues/detail?id=17
"""
qname, _, _ = self.ch.queue_declare()
msg = Message(application_headers={'test': object()})
self.assertRaises(
FrameSyntaxError, self.ch.basic_publish, msg, routing_key=qname,
)
def test_large(self):
"""
Test sending some extra large messages.
"""
qname, _, _ = self.ch.queue_declare()
for multiplier in [100, 1000, 10000]:
msg = Message(
'funtest message' * multiplier,
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'},
)
self.ch.basic_publish(msg, routing_key=qname)
msg2 = self.ch.basic_get(no_ack=True)
self.assertEqual(msg, msg2)
def test_publish(self):
self.ch.exchange_declare('funtest.fanout', 'fanout', auto_delete=True)
msg = Message(
'funtest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'},
)
self.ch.basic_publish(msg, 'funtest.fanout')
def test_queue(self):
my_routing_key = 'funtest.test_queue'
msg = Message(
'funtest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'},
)
qname, _, _ = self.ch.queue_declare()
self.ch.queue_bind(qname, 'amq.direct', routing_key=my_routing_key)
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg, msg2)
def test_unbind(self):
my_routing_key = 'funtest.test_queue'
qname, _, _ = self.ch.queue_declare()
self.ch.queue_bind(qname, 'amq.direct', routing_key=my_routing_key)
self.ch.queue_unbind(qname, 'amq.direct', routing_key=my_routing_key)
def test_basic_return(self):
self.ch.exchange_declare('funtest.fanout', 'fanout', auto_delete=True)
msg = Message(
'funtest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'})
self.ch.basic_publish(msg, 'funtest.fanout')
self.ch.basic_publish(msg, 'funtest.fanout', mandatory=True)
self.ch.basic_publish(msg, 'funtest.fanout', mandatory=True)
self.ch.basic_publish(msg, 'funtest.fanout', mandatory=True)
self.ch.close()
#
# 3 of the 4 messages we sent should have been returned
#
self.assertEqual(self.ch.returned_messages.qsize(), 3)
def test_exchange_bind(self):
"""Test exchange binding.
Network configuration is as follows (-> is forwards to :
source_exchange -> dest_exchange -> queue
The test checks that once the message is publish to the
destination exchange(funtest.topic_dest) it is delivered to the queue.
"""
test_routing_key = 'unit_test__key'
dest_exchange = 'funtest.topic_dest_bind'
source_exchange = 'funtest.topic_source_bind'
self.ch.exchange_declare(dest_exchange, 'topic', auto_delete=True)
self.ch.exchange_declare(source_exchange, 'topic', auto_delete=True)
qname, _, _ = self.ch.queue_declare()
self.ch.exchange_bind(destination=dest_exchange,
source=source_exchange,
routing_key=test_routing_key)
self.ch.queue_bind(qname, dest_exchange,
routing_key=test_routing_key)
msg = Message('funtest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'})
self.ch.basic_publish(msg, source_exchange,
routing_key=test_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg, msg2)
def test_exchange_unbind(self):
dest_exchange = 'funtest.topic_dest_unbind'
source_exchange = 'funtest.topic_source_unbind'
test_routing_key = 'unit_test__key'
self.ch.exchange_declare(dest_exchange,
'topic', auto_delete=True)
self.ch.exchange_declare(source_exchange,
'topic', auto_delete=True)
self.ch.exchange_bind(destination=dest_exchange,
source=source_exchange,
routing_key=test_routing_key)
self.ch.exchange_unbind(destination=dest_exchange,
source=source_exchange,
routing_key=test_routing_key)
def main():
suite = unittest.TestLoader().loadTestsFromTestCase(TestChannel)
unittest.TextTestRunner(**settings.test_args).run(suite)
if __name__ == '__main__':
main()
|