test_gevent.py 3.2 KB

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