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