test_components.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import pytest
  2. from case import Mock, patch, skip
  3. from celery.exceptions import ImproperlyConfigured
  4. from celery.worker.components import Beat, Hub, Pool, Timer
  5. # some of these are tested in test_worker, so I've only written tests
  6. # here to complete coverage. Should move everyting to this module at some
  7. # point [-ask]
  8. class test_Timer:
  9. def test_create__eventloop(self):
  10. w = Mock(name='w')
  11. w.use_eventloop = True
  12. Timer(w).create(w)
  13. assert not w.timer.queue
  14. class test_Hub:
  15. def setup(self):
  16. self.w = Mock(name='w')
  17. self.hub = Hub(self.w)
  18. self.w.hub = Mock(name='w.hub')
  19. @patch('celery.worker.components.set_event_loop')
  20. @patch('celery.worker.components.get_event_loop')
  21. def test_create(self, get_event_loop, set_event_loop):
  22. self.hub._patch_thread_primitives = Mock(name='ptp')
  23. assert self.hub.create(self.w) is self.hub
  24. self.hub._patch_thread_primitives.assert_called_with(self.w)
  25. def test_start(self):
  26. self.hub.start(self.w)
  27. def test_stop(self):
  28. self.hub.stop(self.w)
  29. self.w.hub.close.assert_called_with()
  30. def test_terminate(self):
  31. self.hub.terminate(self.w)
  32. self.w.hub.close.assert_called_with()
  33. class test_Pool:
  34. def test_close_terminate(self):
  35. w = Mock()
  36. comp = Pool(w)
  37. pool = w.pool = Mock()
  38. comp.close(w)
  39. pool.close.assert_called_with()
  40. comp.terminate(w)
  41. pool.terminate.assert_called_with()
  42. w.pool = None
  43. comp.close(w)
  44. comp.terminate(w)
  45. @skip.if_win32()
  46. def test_create_when_eventloop(self):
  47. w = Mock()
  48. w.use_eventloop = w.pool_putlocks = w.pool_cls.uses_semaphore = True
  49. comp = Pool(w)
  50. w.pool = Mock()
  51. comp.create(w)
  52. assert w.process_task is w._process_task_sem
  53. def test_create_calls_instantiate_with_max_memory(self):
  54. w = Mock()
  55. w.use_eventloop = w.pool_putlocks = w.pool_cls.uses_semaphore = True
  56. comp = Pool(w)
  57. comp.instantiate = Mock()
  58. w.max_memory_per_child = 32
  59. comp.create(w)
  60. assert comp.instantiate.call_args[1]['max_memory_per_child'] == 32
  61. class test_Beat:
  62. def test_create__green(self):
  63. w = Mock(name='w')
  64. w.pool_cls.__module__ = 'foo_gevent'
  65. with pytest.raises(ImproperlyConfigured):
  66. Beat(w).create(w)