test_eventlet.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from __future__ import absolute_import, unicode_literals
  2. import sys
  3. import pytest
  4. from case import Mock, patch, skip
  5. from celery.concurrency.eventlet import TaskPool, Timer, apply_target
  6. eventlet_modules = (
  7. 'eventlet',
  8. 'eventlet.debug',
  9. 'eventlet.greenthread',
  10. 'eventlet.greenpool',
  11. 'greenlet',
  12. )
  13. @skip.if_pypy()
  14. @skip.unless_module('eventlet')
  15. class EventletCase:
  16. def setup(self):
  17. self.patching.modules(*eventlet_modules)
  18. def teardown(self):
  19. for mod in [mod for mod in sys.modules
  20. if mod.startswith('eventlet')]:
  21. try:
  22. del(sys.modules[mod])
  23. except KeyError:
  24. pass
  25. class test_aaa_eventlet_patch(EventletCase):
  26. def test_aaa_is_patched(self):
  27. with patch('eventlet.monkey_patch', create=True) as monkey_patch:
  28. from celery import maybe_patch_concurrency
  29. maybe_patch_concurrency(['x', '-P', 'eventlet'])
  30. monkey_patch.assert_called_with()
  31. @patch('eventlet.debug.hub_blocking_detection', create=True)
  32. @patch('eventlet.monkey_patch', create=True)
  33. def test_aaa_blockdetecet(
  34. self, monkey_patch, hub_blocking_detection, patching):
  35. patching.setenv('EVENTLET_NOBLOCK', '10.3')
  36. from celery import maybe_patch_concurrency
  37. maybe_patch_concurrency(['x', '-P', 'eventlet'])
  38. monkey_patch.assert_called_with()
  39. hub_blocking_detection.assert_called_with(10.3, 10.3)
  40. class test_Timer(EventletCase):
  41. @pytest.fixture(autouse=True)
  42. def setup_patches(self, patching):
  43. self.spawn_after = patching('eventlet.greenthread.spawn_after')
  44. self.GreenletExit = patching('greenlet.GreenletExit')
  45. def test_sched(self):
  46. x = Timer()
  47. x.GreenletExit = KeyError
  48. entry = Mock()
  49. g = x._enter(1, 0, entry)
  50. assert x.queue
  51. x._entry_exit(g, entry)
  52. g.wait.side_effect = KeyError()
  53. x._entry_exit(g, entry)
  54. entry.cancel.assert_called_with()
  55. assert not x._queue
  56. x._queue.add(g)
  57. x.clear()
  58. x._queue.add(g)
  59. g.cancel.side_effect = KeyError()
  60. x.clear()
  61. def test_cancel(self):
  62. x = Timer()
  63. tref = Mock(name='tref')
  64. x.cancel(tref)
  65. tref.cancel.assert_called_with()
  66. x.GreenletExit = KeyError
  67. tref.cancel.side_effect = KeyError()
  68. x.cancel(tref)
  69. class test_TaskPool(EventletCase):
  70. @pytest.fixture(autouse=True)
  71. def setup_patches(self, patching):
  72. self.GreenPool = patching('eventlet.greenpool.GreenPool')
  73. self.greenthread = patching('eventlet.greenthread')
  74. def test_pool(self):
  75. x = TaskPool()
  76. x.on_start()
  77. x.on_stop()
  78. x.on_apply(Mock())
  79. x._pool = None
  80. x.on_stop()
  81. assert x.getpid()
  82. @patch('celery.concurrency.eventlet.base')
  83. def test_apply_target(self, base):
  84. apply_target(Mock(), getpid=Mock())
  85. base.apply_target.assert_called()
  86. def test_grow(self):
  87. x = TaskPool(10)
  88. x._pool = Mock(name='_pool')
  89. x.grow(2)
  90. assert x.limit == 12
  91. x._pool.resize.assert_called_with(12)
  92. def test_shrink(self):
  93. x = TaskPool(10)
  94. x._pool = Mock(name='_pool')
  95. x.shrink(2)
  96. assert x.limit == 8
  97. x._pool.resize.assert_called_with(8)
  98. def test_get_info(self):
  99. x = TaskPool(10)
  100. x._pool = Mock(name='_pool')
  101. assert x._get_info() == {
  102. 'max-concurrency': 10,
  103. 'free-threads': x._pool.free(),
  104. 'running-threads': x._pool.running(),
  105. }