test_eventlet.py 3.5 KB

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