Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_amqplib.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from __future__ import absolute_import
  2. import sys
  3. from kombu import Connection
  4. from kombu.tests.case import Case, SkipTest, Mock, mask_modules
  5. class MockConnection(dict):
  6. def __setattr__(self, key, value):
  7. self[key] = value
  8. try:
  9. __import__('amqplib')
  10. except ImportError:
  11. amqplib = Channel = None
  12. else:
  13. from kombu.transport import amqplib
  14. class Channel(amqplib.Channel):
  15. wait_returns = []
  16. def _x_open(self, *args, **kwargs):
  17. pass
  18. def wait(self, *args, **kwargs):
  19. return self.wait_returns
  20. def _send_method(self, *args, **kwargs):
  21. pass
  22. class amqplibCase(Case):
  23. def setUp(self):
  24. if amqplib is None:
  25. raise SkipTest('amqplib not installed')
  26. self.setup()
  27. def setup(self):
  28. pass
  29. class test_Channel(amqplibCase):
  30. def setup(self):
  31. self.conn = Mock()
  32. self.conn.channels = {}
  33. self.channel = Channel(self.conn, 0)
  34. def test_init(self):
  35. self.assertFalse(self.channel.no_ack_consumers)
  36. def test_prepare_message(self):
  37. self.assertTrue(self.channel.prepare_message(
  38. 'foobar', 10, 'application/data', 'utf-8',
  39. properties={},
  40. ))
  41. def test_message_to_python(self):
  42. message = Mock()
  43. message.headers = {}
  44. message.properties = {}
  45. self.assertTrue(self.channel.message_to_python(message))
  46. def test_close_resolves_connection_cycle(self):
  47. self.assertIsNotNone(self.channel.connection)
  48. self.channel.close()
  49. self.assertIsNone(self.channel.connection)
  50. def test_basic_consume_registers_ack_status(self):
  51. self.channel.wait_returns = 'my-consumer-tag'
  52. self.channel.basic_consume('foo', no_ack=True)
  53. self.assertIn('my-consumer-tag', self.channel.no_ack_consumers)
  54. self.channel.wait_returns = 'other-consumer-tag'
  55. self.channel.basic_consume('bar', no_ack=False)
  56. self.assertNotIn('other-consumer-tag', self.channel.no_ack_consumers)
  57. self.channel.basic_cancel('my-consumer-tag')
  58. self.assertNotIn('my-consumer-tag', self.channel.no_ack_consumers)
  59. class test_Transport(amqplibCase):
  60. def setup(self):
  61. self.connection = Connection('amqplib://')
  62. self.transport = self.connection.transport
  63. def test_create_channel(self):
  64. connection = Mock()
  65. self.transport.create_channel(connection)
  66. connection.channel.assert_called_with()
  67. def test_drain_events(self):
  68. connection = Mock()
  69. self.transport.drain_events(connection, timeout=10.0)
  70. connection.drain_events.assert_called_with(timeout=10.0)
  71. def test_dnspython_localhost_resolve_bug(self):
  72. class Conn(object):
  73. def __init__(self, **kwargs):
  74. vars(self).update(kwargs)
  75. self.transport.Connection = Conn
  76. self.transport.client.hostname = 'localhost'
  77. conn1 = self.transport.establish_connection()
  78. self.assertEqual(conn1.host, '127.0.0.1:5672')
  79. self.transport.client.hostname = 'example.com'
  80. conn2 = self.transport.establish_connection()
  81. self.assertEqual(conn2.host, 'example.com:5672')
  82. def test_close_connection(self):
  83. connection = Mock()
  84. connection.client = Mock()
  85. self.transport.close_connection(connection)
  86. self.assertIsNone(connection.client)
  87. connection.close.assert_called_with()
  88. def test_verify_connection(self):
  89. connection = Mock()
  90. connection.channels = None
  91. self.assertFalse(self.transport.verify_connection(connection))
  92. connection.channels = {1: 1, 2: 2}
  93. self.assertTrue(self.transport.verify_connection(connection))
  94. @mask_modules('ssl')
  95. def test_import_no_ssl(self):
  96. pm = sys.modules.pop('kombu.transport.amqplib')
  97. try:
  98. from kombu.transport.amqplib import SSLError
  99. self.assertEqual(SSLError.__module__, 'kombu.transport.amqplib')
  100. finally:
  101. if pm is not None:
  102. sys.modules['kombu.transport.amqplib'] = pm
  103. class test_amqplib(amqplibCase):
  104. def test_default_port(self):
  105. class Transport(amqplib.Transport):
  106. Connection = MockConnection
  107. c = Connection(port=None, transport=Transport).connect()
  108. self.assertEqual(c['host'],
  109. '127.0.0.1:%s' % (Transport.default_port, ))
  110. def test_custom_port(self):
  111. class Transport(amqplib.Transport):
  112. Connection = MockConnection
  113. c = Connection(port=1337, transport=Transport).connect()
  114. self.assertEqual(c['host'], '127.0.0.1:1337')