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_pools.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from __future__ import absolute_import
  2. from kombu import Connection, Producer
  3. from kombu import pools
  4. from kombu.connection import ConnectionPool
  5. from kombu.utils import eqhash
  6. from .case import Case, Mock
  7. class test_ProducerPool(Case):
  8. Pool = pools.ProducerPool
  9. class MyPool(pools.ProducerPool):
  10. def __init__(self, *args, **kwargs):
  11. self.instance = Mock()
  12. pools.ProducerPool.__init__(self, *args, **kwargs)
  13. def Producer(self, connection):
  14. return self.instance
  15. def setUp(self):
  16. self.connections = Mock()
  17. self.pool = self.Pool(self.connections, limit=10)
  18. def test_close_resource(self):
  19. self.pool.close_resource(Mock(name='resource'))
  20. def test_releases_connection_when_Producer_raises(self):
  21. self.pool.Producer = Mock()
  22. self.pool.Producer.side_effect = IOError()
  23. acq = self.pool._acquire_connection = Mock()
  24. conn = acq.return_value = Mock()
  25. with self.assertRaises(IOError):
  26. self.pool.create_producer()
  27. conn.release.assert_called_with()
  28. def test_prepare_release_connection_on_error(self):
  29. pp = Mock()
  30. p = pp.return_value = Mock()
  31. p.revive.side_effect = IOError()
  32. acq = self.pool._acquire_connection = Mock()
  33. conn = acq.return_value = Mock()
  34. p._channel = None
  35. with self.assertRaises(IOError):
  36. self.pool.prepare(pp)
  37. conn.release.assert_called_with()
  38. def test_release_releases_connection(self):
  39. p = Mock()
  40. p.__connection__ = Mock()
  41. self.pool.release(p)
  42. p.__connection__.release.assert_called_with()
  43. p.__connection__ = None
  44. self.pool.release(p)
  45. def test_init(self):
  46. self.assertIs(self.pool.connections, self.connections)
  47. def test_Producer(self):
  48. self.assertIsInstance(self.pool.Producer(Mock()), Producer)
  49. def test_acquire_connection(self):
  50. self.pool._acquire_connection()
  51. self.connections.acquire.assert_called_with(block=True)
  52. def test_new(self):
  53. promise = self.pool.new()
  54. producer = promise()
  55. self.assertIsInstance(producer, Producer)
  56. self.connections.acquire.assert_called_with(block=True)
  57. def test_setup_unlimited(self):
  58. pool = self.Pool(self.connections, limit=None)
  59. pool.setup()
  60. self.assertFalse(pool._resource.queue)
  61. def test_setup(self):
  62. self.assertEqual(len(self.pool._resource.queue), self.pool.limit)
  63. first = self.pool._resource.get_nowait()
  64. producer = first()
  65. self.assertIsInstance(producer, Producer)
  66. def test_prepare(self):
  67. connection = self.connections.acquire.return_value = Mock()
  68. pool = self.MyPool(self.connections, limit=10)
  69. pool.instance._channel = None
  70. first = pool._resource.get_nowait()
  71. producer = pool.prepare(first)
  72. self.assertTrue(self.connections.acquire.called)
  73. producer.revive.assert_called_with(connection)
  74. def test_prepare_channel_already_created(self):
  75. self.connections.acquire.return_value = Mock()
  76. pool = self.MyPool(self.connections, limit=10)
  77. pool.instance._channel = Mock()
  78. first = pool._resource.get_nowait()
  79. self.connections.acquire.reset()
  80. producer = pool.prepare(first)
  81. self.assertFalse(producer.revive.called)
  82. def test_prepare_not_callable(self):
  83. x = Producer(Mock)
  84. self.pool.prepare(x)
  85. def test_release(self):
  86. p = Mock()
  87. p.channel = Mock()
  88. p.__connection__ = Mock()
  89. self.pool.release(p)
  90. p.__connection__.release.assert_called_with()
  91. self.assertIsNone(p.channel)
  92. class test_PoolGroup(Case):
  93. Group = pools.PoolGroup
  94. class MyGroup(pools.PoolGroup):
  95. def create(self, resource, limit):
  96. return resource, limit
  97. def test_interface_create(self):
  98. g = self.Group()
  99. with self.assertRaises(NotImplementedError):
  100. g.create(Mock(), 10)
  101. def test_getitem_using_global_limit(self):
  102. pools._used[0] = False
  103. g = self.MyGroup(limit=pools.use_global_limit)
  104. res = g['foo']
  105. self.assertTupleEqual(res, ('foo', pools.get_limit()))
  106. self.assertTrue(pools._used[0])
  107. def test_getitem_using_custom_limit(self):
  108. pools._used[0] = True
  109. g = self.MyGroup(limit=102456)
  110. res = g['foo']
  111. self.assertTupleEqual(res, ('foo', 102456))
  112. def test_delitem(self):
  113. g = self.MyGroup()
  114. g['foo']
  115. del(g['foo'])
  116. self.assertNotIn('foo', g)
  117. def test_Connections(self):
  118. conn = Connection('memory://')
  119. p = pools.connections[conn]
  120. self.assertTrue(p)
  121. self.assertIsInstance(p, ConnectionPool)
  122. self.assertIs(p.connection, conn)
  123. self.assertEqual(p.limit, pools.get_limit())
  124. def test_Producers(self):
  125. conn = Connection('memory://')
  126. p = pools.producers[conn]
  127. self.assertTrue(p)
  128. self.assertIsInstance(p, pools.ProducerPool)
  129. self.assertIs(p.connections, pools.connections[conn])
  130. self.assertEqual(p.limit, p.connections.limit)
  131. self.assertEqual(p.limit, pools.get_limit())
  132. def test_all_groups(self):
  133. conn = Connection('memory://')
  134. pools.connections[conn]
  135. self.assertTrue(list(pools._all_pools()))
  136. def test_reset(self):
  137. pools.reset()
  138. class MyGroup(dict):
  139. clear_called = False
  140. def clear(self):
  141. self.clear_called = True
  142. p1 = pools.connections['foo'] = Mock()
  143. g1 = MyGroup()
  144. pools._groups.append(g1)
  145. pools.reset()
  146. p1.force_close_all.assert_called_with()
  147. self.assertTrue(g1.clear_called)
  148. p1 = pools.connections['foo'] = Mock()
  149. p1.force_close_all.side_effect = KeyError()
  150. pools.reset()
  151. def test_set_limit(self):
  152. pools.reset()
  153. pools.set_limit(34576)
  154. limit = pools.get_limit()
  155. self.assertEqual(limit, 34576)
  156. pools.connections[Connection('memory://')]
  157. pools.set_limit(limit + 1)
  158. self.assertEqual(pools.get_limit(), limit + 1)
  159. limit = pools.get_limit()
  160. with self.assertRaises(RuntimeError):
  161. pools.set_limit(limit - 1)
  162. pools.set_limit(limit - 1, force=True)
  163. self.assertEqual(pools.get_limit(), limit - 1)
  164. pools.set_limit(pools.get_limit())
  165. class test_fun_PoolGroup(Case):
  166. def test_connections_behavior(self):
  167. c1u = 'memory://localhost:123'
  168. c2u = 'memory://localhost:124'
  169. c1 = Connection(c1u)
  170. c2 = Connection(c2u)
  171. c3 = Connection(c1u)
  172. assert eqhash(c1) != eqhash(c2)
  173. assert eqhash(c1) == eqhash(c3)
  174. c4 = Connection(c1u, transport_options={'confirm_publish': True})
  175. self.assertNotEqual(eqhash(c3), eqhash(c4))
  176. p1 = pools.connections[c1]
  177. p2 = pools.connections[c2]
  178. p3 = pools.connections[c3]
  179. self.assertIsNot(p1, p2)
  180. self.assertIs(p1, p3)
  181. r1 = p1.acquire()
  182. self.assertTrue(p1._dirty)
  183. self.assertTrue(p3._dirty)
  184. self.assertFalse(p2._dirty)
  185. r1.release()
  186. self.assertFalse(p1._dirty)
  187. self.assertFalse(p3._dirty)