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_simple.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from __future__ import absolute_import
  2. from kombu import Connection, Exchange, Queue
  3. from .case import Case, Mock
  4. class SimpleBase(Case):
  5. abstract = True
  6. def Queue(self, name, *args, **kwargs):
  7. q = name
  8. if not isinstance(q, Queue):
  9. q = self.__class__.__name__
  10. if name:
  11. q = '%s.%s' % (q, name)
  12. return self._Queue(q, *args, **kwargs)
  13. def _Queue(self, *args, **kwargs):
  14. raise NotImplementedError()
  15. def setUp(self):
  16. if not self.abstract:
  17. self.connection = Connection(transport='memory')
  18. with self.connection.channel() as channel:
  19. channel.exchange_declare('amq.direct')
  20. self.q = self.Queue(None, no_ack=True)
  21. def tearDown(self):
  22. if not self.abstract:
  23. self.q.close()
  24. self.connection.close()
  25. def test_produce__consume(self):
  26. if self.abstract:
  27. return
  28. q = self.Queue('test_produce__consume', no_ack=True)
  29. q.put({'hello': 'Simple'})
  30. self.assertEqual(q.get(timeout=1).payload, {'hello': 'Simple'})
  31. with self.assertRaises(q.Empty):
  32. q.get(timeout=0.1)
  33. def test_produce__basic_get(self):
  34. if self.abstract:
  35. return
  36. q = self.Queue('test_produce__basic_get', no_ack=True)
  37. q.put({'hello': 'SimpleSync'})
  38. self.assertEqual(q.get_nowait().payload, {'hello': 'SimpleSync'})
  39. with self.assertRaises(q.Empty):
  40. q.get_nowait()
  41. q.put({'hello': 'SimpleSync'})
  42. self.assertEqual(q.get(block=False).payload, {'hello': 'SimpleSync'})
  43. with self.assertRaises(q.Empty):
  44. q.get(block=False)
  45. def test_clear(self):
  46. if self.abstract:
  47. return
  48. q = self.Queue('test_clear', no_ack=True)
  49. for i in range(10):
  50. q.put({'hello': 'SimplePurge%d' % (i, )})
  51. self.assertEqual(q.clear(), 10)
  52. def test_enter_exit(self):
  53. if self.abstract:
  54. return
  55. q = self.Queue('test_enter_exit')
  56. q.close = Mock()
  57. self.assertIs(q.__enter__(), q)
  58. q.__exit__()
  59. q.close.assert_called_with()
  60. def test_qsize(self):
  61. if self.abstract:
  62. return
  63. q = self.Queue('test_clear', no_ack=True)
  64. for i in range(10):
  65. q.put({'hello': 'SimplePurge%d' % (i, )})
  66. self.assertEqual(q.qsize(), 10)
  67. self.assertEqual(len(q), 10)
  68. def test_autoclose(self):
  69. if self.abstract:
  70. return
  71. channel = self.connection.channel()
  72. q = self.Queue('test_autoclose', no_ack=True, channel=channel)
  73. q.close()
  74. def test_custom_Queue(self):
  75. if self.abstract:
  76. return
  77. n = self.__class__.__name__
  78. exchange = Exchange('%s-test.custom.Queue' % (n, ))
  79. queue = Queue('%s-test.custom.Queue' % (n, ),
  80. exchange,
  81. 'my.routing.key')
  82. q = self.Queue(queue)
  83. self.assertEqual(q.consumer.queues[0], queue)
  84. q.close()
  85. def test_bool(self):
  86. if self.abstract:
  87. return
  88. q = self.Queue('test_nonzero')
  89. self.assertTrue(q)
  90. class test_SimpleQueue(SimpleBase):
  91. abstract = False
  92. def _Queue(self, *args, **kwargs):
  93. return self.connection.SimpleQueue(*args, **kwargs)
  94. def test_is_ack(self):
  95. q = self.Queue('test_is_no_ack')
  96. self.assertFalse(q.no_ack)
  97. class test_SimpleBuffer(SimpleBase):
  98. abstract = False
  99. def Queue(self, *args, **kwargs):
  100. return self.connection.SimpleBuffer(*args, **kwargs)
  101. def test_is_no_ack(self):
  102. q = self.Queue('test_is_no_ack')
  103. self.assertTrue(q.no_ack)