| 1 |
import sys |
| 2 |
import time |
| 3 |
from unittest.mock import Mock, call, patch |
| 4 |
|
| 5 |
from celery.utils import timer2 as timer2 |
| 6 |
|
| 7 |
|
| 8 |
class test_Timer: |
| 9 |
|
| 10 |
def test_enter_after(self): |
| 11 |
t = timer2.Timer() |
| 12 |
try: |
| 13 |
done = [False] |
| 14 |
|
| 15 |
def set_done(): |
| 16 |
done[0] = True |
| 17 |
|
| 18 |
t.call_after(0.3, set_done) |
| 19 |
mss = 0 |
| 20 |
while not done[0]: |
| 21 |
if mss >= 2.0: |
| 22 |
raise Exception('test timed out') |
| 23 |
time.sleep(0.1) |
| 24 |
mss += 0.1 |
| 25 |
finally: |
| 26 |
t.stop() |
| 27 |
|
| 28 |
def test_exit_after(self): |
| 29 |
t = timer2.Timer() |
| 30 |
t.call_after = Mock() |
| 31 |
t.exit_after(0.3, priority=10) |
| 32 |
t.call_after.assert_called_with(0.3, sys.exit, 10) |
| 33 |
|
| 34 |
def test_ensure_started_not_started(self): |
| 35 |
t = timer2.Timer() |
| 36 |
t.running = True |
| 37 |
t.start = Mock() |
| 38 |
t.ensure_started() |
| 39 |
t.start.assert_not_called() |
| 40 |
t.running = False |
| 41 |
t.on_start = Mock() |
| 42 |
t.ensure_started() |
| 43 |
t.on_start.assert_called_with(t) |
| 44 |
t.start.assert_called_with() |
| 45 |
|
| 46 |
@patch('celery.utils.timer2.sleep') |
| 47 |
@patch('os._exit') # To ensure the test fails gracefully |
| 48 |
def test_on_tick(self, _exit, sleep): |
| 49 |
def next_entry_side_effect(): |
| 50 |
# side effect simulating following scenario: |
| 51 |
# 3.33, 3.33, 3.33, <shutdown event set> |
| 52 |
for _ in range(3): |
| 53 |
yield 3.33 |
| 54 |
while True: |
| 55 |
yield getattr(t, "_Timer__is_shutdown").set() |
| 56 |
|
| 57 |
on_tick = Mock(name='on_tick') |
| 58 |
t = timer2.Timer(on_tick=on_tick) |
| 59 |
t._next_entry = Mock( |
| 60 |
name='_next_entry', side_effect=next_entry_side_effect() |
| 61 |
) |
| 62 |
t.run() |
| 63 |
sleep.assert_called_with(3.33) |
| 64 |
on_tick.assert_has_calls([call(3.33), call(3.33), call(3.33)]) |
| 65 |
_exit.assert_not_called() |
| 66 |
|
| 67 |
@patch('os._exit') |
| 68 |
def test_thread_crash(self, _exit): |
| 69 |
t = timer2.Timer() |
| 70 |
t._next_entry = Mock() |
| 71 |
t._next_entry.side_effect = OSError(131) |
| 72 |
t.run() |
| 73 |
_exit.assert_called_with(1) |
| 74 |
|
| 75 |
def test_gc_race_lost(self): |
| 76 |
t = timer2.Timer() |
| 77 |
with patch.object(t, "_Timer__is_stopped") as mock_stop_event: |
| 78 |
# Mark the timer as shutting down so we escape the run loop, |
| 79 |
# mocking the running state so we don't block! |
| 80 |
with patch.object(t, "running", new=False): |
| 81 |
t.stop() |
| 82 |
# Pretend like the interpreter has shutdown and GCed built-in |
| 83 |
# modules, causing an exception |
| 84 |
mock_stop_event.set.side_effect = TypeError() |
| 85 |
t.run() |
| 86 |
mock_stop_event.set.assert_called_with() |
| 87 |
|
| 88 |
def test_test_enter(self): |
| 89 |
t = timer2.Timer() |
| 90 |
t._do_enter = Mock() |
| 91 |
e = Mock() |
| 92 |
t.enter(e, 13, 0) |
| 93 |
t._do_enter.assert_called_with('enter_at', e, 13, priority=0) |
| 94 |
|
| 95 |
def test_test_enter_after(self): |
| 96 |
t = timer2.Timer() |
| 97 |
t._do_enter = Mock() |
| 98 |
t.enter_after() |
| 99 |
t._do_enter.assert_called_with('enter_after') |
| 100 |
|
| 101 |
def test_cancel(self): |
| 102 |
t = timer2.Timer() |
| 103 |
tref = Mock() |
| 104 |
t.cancel(tref) |
| 105 |
tref.cancel.assert_called_with() |