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_pyamqp.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from __future__ import absolute_import
  2. import sys
  3. from itertools import count
  4. try:
  5. import amqp # noqa
  6. except ImportError:
  7. pyamqp = None # noqa
  8. else:
  9. from kombu.transport import pyamqp
  10. from kombu import Connection
  11. from kombu.five import nextfun
  12. from kombu.tests.case import Case, Mock, SkipTest, mask_modules, patch
  13. class MockConnection(dict):
  14. def __setattr__(self, key, value):
  15. self[key] = value
  16. class test_Channel(Case):
  17. def setUp(self):
  18. if pyamqp is None:
  19. raise SkipTest('py-amqp not installed')
  20. class Channel(pyamqp.Channel):
  21. wait_returns = []
  22. def _x_open(self, *args, **kwargs):
  23. pass
  24. def wait(self, *args, **kwargs):
  25. return self.wait_returns
  26. def _send_method(self, *args, **kwargs):
  27. pass
  28. self.conn = Mock()
  29. self.conn._get_free_channel_id.side_effect = nextfun(count(0))
  30. self.conn.channels = {}
  31. self.channel = Channel(self.conn, 0)
  32. def test_init(self):
  33. self.assertFalse(self.channel.no_ack_consumers)
  34. def test_prepare_message(self):
  35. self.assertTrue(self.channel.prepare_message(
  36. 'foobar', 10, 'application/data', 'utf-8',
  37. properties={},
  38. ))
  39. def test_message_to_python(self):
  40. message = Mock()
  41. message.headers = {}
  42. message.properties = {}
  43. self.assertTrue(self.channel.message_to_python(message))
  44. def test_close_resolves_connection_cycle(self):
  45. self.assertIsNotNone(self.channel.connection)
  46. self.channel.close()
  47. self.assertIsNone(self.channel.connection)
  48. def test_basic_consume_registers_ack_status(self):
  49. self.channel.wait_returns = 'my-consumer-tag'
  50. self.channel.basic_consume('foo', no_ack=True)
  51. self.assertIn('my-consumer-tag', self.channel.no_ack_consumers)
  52. self.channel.wait_returns = 'other-consumer-tag'
  53. self.channel.basic_consume('bar', no_ack=False)
  54. self.assertNotIn('other-consumer-tag', self.channel.no_ack_consumers)
  55. self.channel.basic_cancel('my-consumer-tag')
  56. self.assertNotIn('my-consumer-tag', self.channel.no_ack_consumers)
  57. class test_Transport(Case):
  58. def setUp(self):
  59. if pyamqp is None:
  60. raise SkipTest('py-amqp not installed')
  61. self.connection = Connection('pyamqp://')
  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_driver_version(self):
  68. self.assertTrue(self.transport.driver_version())
  69. def test_drain_events(self):
  70. connection = Mock()
  71. self.transport.drain_events(connection, timeout=10.0)
  72. connection.drain_events.assert_called_with(timeout=10.0)
  73. def test_dnspython_localhost_resolve_bug(self):
  74. class Conn(object):
  75. def __init__(self, **kwargs):
  76. vars(self).update(kwargs)
  77. self.transport.Connection = Conn
  78. self.transport.client.hostname = 'localhost'
  79. conn1 = self.transport.establish_connection()
  80. self.assertEqual(conn1.host, '127.0.0.1:5672')
  81. self.transport.client.hostname = 'example.com'
  82. conn2 = self.transport.establish_connection()
  83. self.assertEqual(conn2.host, 'example.com:5672')
  84. def test_close_connection(self):
  85. connection = Mock()
  86. connection.client = Mock()
  87. self.transport.close_connection(connection)
  88. self.assertIsNone(connection.client)
  89. connection.close.assert_called_with()
  90. @mask_modules('ssl')
  91. def test_import_no_ssl(self):
  92. pm = sys.modules.pop('amqp.connection')
  93. try:
  94. from amqp.connection import SSLError
  95. self.assertEqual(SSLError.__module__, 'amqp.connection')
  96. finally:
  97. if pm is not None:
  98. sys.modules['amqp.connection'] = pm
  99. class test_pyamqp(Case):
  100. def setUp(self):
  101. if pyamqp is None:
  102. raise SkipTest('py-amqp not installed')
  103. def test_default_port(self):
  104. class Transport(pyamqp.Transport):
  105. Connection = MockConnection
  106. c = Connection(port=None, transport=Transport).connect()
  107. self.assertEqual(c['host'],
  108. '127.0.0.1:%s' % (Transport.default_port, ))
  109. def test_custom_port(self):
  110. class Transport(pyamqp.Transport):
  111. Connection = MockConnection
  112. c = Connection(port=1337, transport=Transport).connect()
  113. self.assertEqual(c['host'], '127.0.0.1:1337')
  114. def test_register_with_event_loop(self):
  115. t = pyamqp.Transport(Mock())
  116. conn = Mock(name='conn')
  117. loop = Mock(name='loop')
  118. t.register_with_event_loop(conn, loop)
  119. loop.add_reader.assert_called_with(
  120. conn.sock, t.on_readable, conn, loop,
  121. )
  122. def test_heartbeat_check(self):
  123. t = pyamqp.Transport(Mock())
  124. conn = Mock()
  125. t.heartbeat_check(conn, rate=4.331)
  126. conn.heartbeat_tick.assert_called_with(rate=4.331)
  127. def test_get_manager(self):
  128. with patch('kombu.transport.pyamqp.get_manager') as get_manager:
  129. t = pyamqp.Transport(Mock())
  130. t.get_manager(1, kw=2)
  131. get_manager.assert_called_with(t.client, 1, kw=2)