test_gevent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from __future__ import absolute_import, unicode_literals
  2. from case import Mock, skip
  3. from celery.concurrency.gevent import TaskPool, Timer, apply_timeout
  4. gevent_modules = (
  5. 'gevent',
  6. 'gevent.monkey',
  7. 'gevent.greenlet',
  8. 'gevent.pool',
  9. 'greenlet',
  10. )
  11. @skip.if_pypy()
  12. class test_gevent_patch:
  13. def test_is_patched(self):
  14. self.patching.modules(*gevent_modules)
  15. patch_all = self.patching('gevent.monkey.patch_all')
  16. import gevent
  17. gevent.version_info = (1, 0, 0)
  18. from celery import maybe_patch_concurrency
  19. maybe_patch_concurrency(['x', '-P', 'gevent'])
  20. patch_all.assert_called()
  21. @skip.if_pypy()
  22. class test_Timer:
  23. def setup(self):
  24. self.patching.modules(*gevent_modules)
  25. self.greenlet = self.patching('gevent.greenlet')
  26. self.GreenletExit = self.patching('gevent.greenlet.GreenletExit')
  27. def test_sched(self):
  28. self.greenlet.Greenlet = object
  29. x = Timer()
  30. self.greenlet.Greenlet = Mock()
  31. x._Greenlet.spawn_later = Mock()
  32. x._GreenletExit = KeyError
  33. entry = Mock()
  34. g = x._enter(1, 0, entry)
  35. assert x.queue
  36. x._entry_exit(g)
  37. g.kill.assert_called_with()
  38. assert not x._queue
  39. x._queue.add(g)
  40. x.clear()
  41. x._queue.add(g)
  42. g.kill.side_effect = KeyError()
  43. x.clear()
  44. g = x._Greenlet()
  45. g.cancel()
  46. @skip.if_pypy()
  47. class test_TaskPool:
  48. def setup(self):
  49. self.patching.modules(*gevent_modules)
  50. self.spawn_raw = self.patching('gevent.spawn_raw')
  51. self.Pool = self.patching('gevent.pool.Pool')
  52. def test_pool(self):
  53. x = TaskPool()
  54. x.on_start()
  55. x.on_stop()
  56. x.on_apply(Mock())
  57. x._pool = None
  58. x.on_stop()
  59. x._pool = Mock()
  60. x._pool._semaphore.counter = 1
  61. x._pool.size = 1
  62. x.grow()
  63. assert x._pool.size == 2
  64. assert x._pool._semaphore.counter == 2
  65. x.shrink()
  66. assert x._pool.size, 1
  67. assert x._pool._semaphore.counter == 1
  68. x._pool = [4, 5, 6]
  69. assert x.num_processes == 3
  70. @skip.if_pypy()
  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)