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_memory.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from __future__ import absolute_import
  2. import socket
  3. from kombu import Connection, Exchange, Queue, Consumer, Producer
  4. from kombu.tests.case import Case
  5. class test_MemoryTransport(Case):
  6. def setUp(self):
  7. self.c = Connection(transport='memory')
  8. self.e = Exchange('test_transport_memory')
  9. self.q = Queue('test_transport_memory',
  10. exchange=self.e,
  11. routing_key='test_transport_memory')
  12. self.q2 = Queue('test_transport_memory2',
  13. exchange=self.e,
  14. routing_key='test_transport_memory2')
  15. self.fanout = Exchange('test_transport_memory_fanout', type='fanout')
  16. self.q3 = Queue('test_transport_memory_fanout1',
  17. exchange=self.fanout)
  18. self.q4 = Queue('test_transport_memory_fanout2',
  19. exchange=self.fanout)
  20. def test_driver_version(self):
  21. self.assertTrue(self.c.transport.driver_version())
  22. def test_produce_consume_noack(self):
  23. channel = self.c.channel()
  24. producer = Producer(channel, self.e)
  25. consumer = Consumer(channel, self.q, no_ack=True)
  26. for i in range(10):
  27. producer.publish({'foo': i}, routing_key='test_transport_memory')
  28. _received = []
  29. def callback(message_data, message):
  30. _received.append(message)
  31. consumer.register_callback(callback)
  32. consumer.consume()
  33. while 1:
  34. if len(_received) == 10:
  35. break
  36. self.c.drain_events()
  37. self.assertEqual(len(_received), 10)
  38. def test_produce_consume_fanout(self):
  39. producer = self.c.Producer()
  40. consumer = self.c.Consumer([self.q3, self.q4])
  41. producer.publish(
  42. {'hello': 'world'},
  43. declare=consumer.queues,
  44. exchange=self.fanout,
  45. )
  46. self.assertEqual(self.q3(self.c).get().payload, {'hello': 'world'})
  47. self.assertEqual(self.q4(self.c).get().payload, {'hello': 'world'})
  48. self.assertIsNone(self.q3(self.c).get())
  49. self.assertIsNone(self.q4(self.c).get())
  50. def test_produce_consume(self):
  51. channel = self.c.channel()
  52. producer = Producer(channel, self.e)
  53. consumer1 = Consumer(channel, self.q)
  54. consumer2 = Consumer(channel, self.q2)
  55. self.q2(channel).declare()
  56. for i in range(10):
  57. producer.publish({'foo': i}, routing_key='test_transport_memory')
  58. for i in range(10):
  59. producer.publish({'foo': i}, routing_key='test_transport_memory2')
  60. _received1 = []
  61. _received2 = []
  62. def callback1(message_data, message):
  63. _received1.append(message)
  64. message.ack()
  65. def callback2(message_data, message):
  66. _received2.append(message)
  67. message.ack()
  68. consumer1.register_callback(callback1)
  69. consumer2.register_callback(callback2)
  70. consumer1.consume()
  71. consumer2.consume()
  72. while 1:
  73. if len(_received1) + len(_received2) == 20:
  74. break
  75. self.c.drain_events()
  76. self.assertEqual(len(_received1) + len(_received2), 20)
  77. # compression
  78. producer.publish({'compressed': True},
  79. routing_key='test_transport_memory',
  80. compression='zlib')
  81. m = self.q(channel).get()
  82. self.assertDictEqual(m.payload, {'compressed': True})
  83. # queue.delete
  84. for i in range(10):
  85. producer.publish({'foo': i}, routing_key='test_transport_memory')
  86. self.assertTrue(self.q(channel).get())
  87. self.q(channel).delete()
  88. self.q(channel).declare()
  89. self.assertIsNone(self.q(channel).get())
  90. # queue.purge
  91. for i in range(10):
  92. producer.publish({'foo': i}, routing_key='test_transport_memory2')
  93. self.assertTrue(self.q2(channel).get())
  94. self.q2(channel).purge()
  95. self.assertIsNone(self.q2(channel).get())
  96. def test_drain_events(self):
  97. with self.assertRaises(socket.timeout):
  98. self.c.drain_events(timeout=0.1)
  99. c1 = self.c.channel()
  100. c2 = self.c.channel()
  101. with self.assertRaises(socket.timeout):
  102. self.c.drain_events(timeout=0.1)
  103. del(c1) # so pyflakes doesn't complain.
  104. del(c2)
  105. def test_drain_events_unregistered_queue(self):
  106. c1 = self.c.channel()
  107. class Cycle(object):
  108. def get(self, timeout=None):
  109. return ('foo', 'foo'), c1
  110. self.c.transport.cycle = Cycle()
  111. with self.assertRaises(KeyError):
  112. self.c.drain_events()
  113. def test_queue_for(self):
  114. chan = self.c.channel()
  115. chan.queues.clear()
  116. x = chan._queue_for('foo')
  117. self.assertTrue(x)
  118. self.assertIs(chan._queue_for('foo'), x)