test_snapshot.py 3.3 KB

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