test_gevent.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from __future__ import absolute_import
  2. from celery.concurrency.gevent import (
  3. Schedule,
  4. Timer,
  5. TaskPool,
  6. apply_timeout,
  7. )
  8. from celery.tests.case import (
  9. AppCase, Mock, SkipTest, mock_module, patch, patch_many, skip_if_pypy,
  10. )
  11. gevent_modules = (
  12. 'gevent',
  13. 'gevent.monkey',
  14. 'gevent.greenlet',
  15. 'gevent.pool',
  16. 'greenlet',
  17. )
  18. class GeventCase(AppCase):
  19. @skip_if_pypy
  20. def setup(self):
  21. try:
  22. self.gevent = __import__('gevent')
  23. except ImportError:
  24. raise SkipTest(
  25. 'gevent not installed, skipping related tests.')
  26. class test_gevent_patch(GeventCase):
  27. def test_is_patched(self):
  28. with mock_module(*gevent_modules):
  29. with patch('gevent.monkey.patch_all', create=True) as patch_all:
  30. import gevent
  31. gevent.version_info = (1, 0, 0)
  32. from celery import maybe_patch_concurrency
  33. maybe_patch_concurrency(['x', '-P', 'gevent'])
  34. self.assertTrue(patch_all.called)
  35. class test_Schedule(AppCase):
  36. def test_sched(self):
  37. with mock_module(*gevent_modules):
  38. with patch_many('gevent.greenlet',
  39. 'gevent.greenlet.GreenletExit') as (greenlet,
  40. GreenletExit):
  41. greenlet.Greenlet = object
  42. x = Schedule()
  43. greenlet.Greenlet = Mock()
  44. x._Greenlet.spawn_later = Mock()
  45. x._GreenletExit = KeyError
  46. entry = Mock()
  47. g = x._enter(1, 0, entry)
  48. self.assertTrue(x.queue)
  49. x._entry_exit(g)
  50. g.kill.assert_called_with()
  51. self.assertFalse(x._queue)
  52. x._queue.add(g)
  53. x.clear()
  54. x._queue.add(g)
  55. g.kill.side_effect = KeyError()
  56. x.clear()
  57. g = x._Greenlet()
  58. g.cancel()
  59. class test_TaskPool(AppCase):
  60. def test_pool(self):
  61. with mock_module(*gevent_modules):
  62. with patch_many('gevent.spawn_raw', 'gevent.pool.Pool') as (
  63. spawn_raw, Pool):
  64. x = TaskPool()
  65. x.on_start()
  66. x.on_stop()
  67. x.on_apply(Mock())
  68. x._pool = None
  69. x.on_stop()
  70. x._pool = Mock()
  71. x._pool._semaphore.counter = 1
  72. x._pool.size = 1
  73. x.grow()
  74. self.assertEqual(x._pool.size, 2)
  75. self.assertEqual(x._pool._semaphore.counter, 2)
  76. x.shrink()
  77. self.assertEqual(x._pool.size, 1)
  78. self.assertEqual(x._pool._semaphore.counter, 1)
  79. x._pool = [4, 5, 6]
  80. self.assertEqual(x.num_processes, 3)
  81. class test_Timer(AppCase):
  82. def test_timer(self):
  83. with mock_module(*gevent_modules):
  84. x = Timer()
  85. x.ensure_started()
  86. x.schedule = Mock()
  87. x.start()
  88. x.stop()
  89. x.schedule.clear.assert_called_with()
  90. class test_apply_timeout(AppCase):
  91. def test_apply_timeout(self):
  92. class Timeout(Exception):
  93. value = None
  94. def __init__(self, value):
  95. self.__class__.value = value
  96. def __enter__(self):
  97. return self
  98. def __exit__(self, *exc_info):
  99. pass
  100. timeout_callback = Mock(name='timeout_callback')
  101. apply_target = Mock(name='apply_target')
  102. apply_timeout(
  103. Mock(), timeout=10, callback=Mock(name='callback'),
  104. timeout_callback=timeout_callback,
  105. apply_target=apply_target, Timeout=Timeout,
  106. )
  107. self.assertEqual(Timeout.value, 10)
  108. self.assertTrue(apply_target.called)
  109. apply_target.side_effect = Timeout(10)
  110. apply_timeout(
  111. Mock(), timeout=10, callback=Mock(),
  112. timeout_callback=timeout_callback,
  113. apply_target=apply_target, Timeout=Timeout,
  114. )
  115. timeout_callback.assert_called_with(False, 10)