test_snapshot.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import Mock, mock, patch
  4. from celery.app.events import Events
  5. from celery.events.snapshot import Polaroid, evcam
  6. class MockTimer(object):
  7. installed = []
  8. def call_repeatedly(self, secs, fun, *args, **kwargs):
  9. self.installed.append(fun)
  10. return Mock(name='TRef')
  11. timer = MockTimer()
  12. class test_Polaroid:
  13. def setup(self):
  14. self.state = self.app.events.State()
  15. def test_constructor(self):
  16. x = Polaroid(self.state, app=self.app)
  17. assert x.app is self.app
  18. assert x.state is self.state
  19. assert x.freq
  20. assert x.cleanup_freq
  21. assert x.logger
  22. assert not x.maxrate
  23. def test_install_timers(self):
  24. x = Polaroid(self.state, app=self.app)
  25. x.timer = timer
  26. x.__exit__()
  27. x.__enter__()
  28. assert x.capture in MockTimer.installed
  29. assert x.cleanup in MockTimer.installed
  30. x._tref.cancel.assert_not_called()
  31. x._ctref.cancel.assert_not_called()
  32. x.__exit__()
  33. x._tref.cancel.assert_called()
  34. x._ctref.cancel.assert_called()
  35. x._tref.assert_called()
  36. x._ctref.assert_not_called()
  37. def test_cleanup(self):
  38. x = Polaroid(self.state, app=self.app)
  39. cleanup_signal_sent = [False]
  40. def handler(**kwargs):
  41. cleanup_signal_sent[0] = True
  42. x.cleanup_signal.connect(handler)
  43. x.cleanup()
  44. assert cleanup_signal_sent[0]
  45. def test_shutter__capture(self):
  46. x = Polaroid(self.state, app=self.app)
  47. shutter_signal_sent = [False]
  48. def handler(**kwargs):
  49. shutter_signal_sent[0] = True
  50. x.shutter_signal.connect(handler)
  51. x.shutter()
  52. assert shutter_signal_sent[0]
  53. shutter_signal_sent[0] = False
  54. x.capture()
  55. assert shutter_signal_sent[0]
  56. def test_shutter_maxrate(self):
  57. x = Polaroid(self.state, app=self.app, maxrate='1/h')
  58. shutter_signal_sent = [0]
  59. def handler(**kwargs):
  60. shutter_signal_sent[0] += 1
  61. x.shutter_signal.connect(handler)
  62. for i in range(30):
  63. x.shutter()
  64. x.shutter()
  65. x.shutter()
  66. assert shutter_signal_sent[0] == 1
  67. class test_evcam:
  68. class MockReceiver(object):
  69. raise_keyboard_interrupt = False
  70. def capture(self, **kwargs):
  71. if self.__class__.raise_keyboard_interrupt:
  72. raise KeyboardInterrupt()
  73. class MockEvents(Events):
  74. def Receiver(self, *args, **kwargs):
  75. return test_evcam.MockReceiver()
  76. def setup(self):
  77. self.app.events = self.MockEvents()
  78. self.app.events.app = self.app
  79. @mock.restore_logging()
  80. def test_evcam(self):
  81. evcam(Polaroid, timer=timer, app=self.app)
  82. evcam(Polaroid, timer=timer, loglevel='CRITICAL', app=self.app)
  83. self.MockReceiver.raise_keyboard_interrupt = True
  84. try:
  85. with pytest.raises(SystemExit):
  86. evcam(Polaroid, timer=timer, app=self.app)
  87. finally:
  88. self.MockReceiver.raise_keyboard_interrupt = False
  89. @patch('celery.platforms.create_pidlock')
  90. def test_evcam_pidfile(self, create_pidlock):
  91. evcam(Polaroid, timer=timer, pidfile='/var/pid', app=self.app)
  92. create_pidlock.assert_called_with('/var/pid')