test_gevent.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from __future__ import absolute_import, unicode_literals
  2. from case import Mock
  3. from celery.concurrency.gevent import (
  4. Timer,
  5. TaskPool,
  6. apply_timeout,
  7. )
  8. gevent_modules = (
  9. 'gevent',
  10. 'gevent.monkey',
  11. 'gevent.greenlet',
  12. 'gevent.pool',
  13. 'greenlet',
  14. )
  15. class test_gevent_patch:
  16. def test_is_patched(self):
  17. self.patching.modules(*gevent_modules)
  18. patch_all = self.patching('gevent.monkey.patch_all')
  19. import gevent
  20. gevent.version_info = (1, 0, 0)
  21. from celery import maybe_patch_concurrency
  22. maybe_patch_concurrency(['x', '-P', 'gevent'])
  23. patch_all.assert_called()
  24. class test_Timer:
  25. def setup(self):
  26. self.patching.modules(*gevent_modules)
  27. self.greenlet = self.patching('gevent.greenlet')
  28. self.GreenletExit = self.patching('gevent.greenlet.GreenletExit')
  29. def test_sched(self):
  30. self.greenlet.Greenlet = object
  31. x = Timer()
  32. self.greenlet.Greenlet = Mock()
  33. x._Greenlet.spawn_later = Mock()
  34. x._GreenletExit = KeyError
  35. entry = Mock()
  36. g = x._enter(1, 0, entry)
  37. assert x.queue
  38. x._entry_exit(g)
  39. g.kill.assert_called_with()
  40. assert not x._queue
  41. x._queue.add(g)
  42. x.clear()
  43. x._queue.add(g)
  44. g.kill.side_effect = KeyError()
  45. x.clear()
  46. g = x._Greenlet()
  47. g.cancel()
  48. class test_TaskPool:
  49. def setup(self):
  50. self.patching.modules(*gevent_modules)
  51. self.spawn_raw = self.patching('gevent.spawn_raw')
  52. self.Pool = self.patching('gevent.pool.Pool')
  53. def test_pool(self):
  54. x = TaskPool()
  55. x.on_start()
  56. x.on_stop()
  57. x.on_apply(Mock())
  58. x._pool = None
  59. x.on_stop()
  60. x._pool = Mock()
  61. x._pool._semaphore.counter = 1
  62. x._pool.size = 1
  63. x.grow()
  64. assert x._pool.size == 2
  65. assert x._pool._semaphore.counter == 2
  66. x.shrink()
  67. assert x._pool.size, 1
  68. assert x._pool._semaphore.counter == 1
  69. x._pool = [4, 5, 6]
  70. assert x.num_processes == 3
  71. class test_apply_timeout:
  72. def test_apply_timeout(self):
  73. self.patching.modules(*gevent_modules)
  74. class Timeout(Exception):
  75. value = None
  76. def __init__(self, value):
  77. self.__class__.value = value
  78. def __enter__(self):
  79. return self
  80. def __exit__(self, *exc_info):
  81. pass
  82. timeout_callback = Mock(name='timeout_callback')
  83. apply_target = Mock(name='apply_target')
  84. apply_timeout(
  85. Mock(), timeout=10, callback=Mock(name='callback'),
  86. timeout_callback=timeout_callback,
  87. apply_target=apply_target, Timeout=Timeout,
  88. )
  89. assert Timeout.value == 10
  90. apply_target.assert_called()
  91. apply_target.side_effect = Timeout(10)
  92. apply_timeout(
  93. Mock(), timeout=10, callback=Mock(),
  94. timeout_callback=timeout_callback,
  95. apply_target=apply_target, Timeout=Timeout,
  96. )
  97. timeout_callback.assert_called_with(False, 10)