| 1 |
import os |
| 2 |
import pytest |
| 3 |
import signal |
| 4 |
|
| 5 |
from contextlib import contextmanager |
| 6 |
from time import time |
| 7 |
from unittest.mock import patch, Mock, call |
| 8 |
|
| 9 |
from t import skip |
| 10 |
|
| 11 |
from billiard.common import ( |
| 12 |
_shutdown_cleanup, |
| 13 |
reset_signals, |
| 14 |
restart_state, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def signo(name): |
| 19 |
return getattr(signal, name) |
| 20 |
|
| 21 |
|
| 22 |
@contextmanager |
| 23 |
def termsigs(default, full): |
| 24 |
from billiard import common |
| 25 |
prev_def, common.TERMSIGS_DEFAULT = common.TERMSIGS_DEFAULT, default |
| 26 |
prev_full, common.TERMSIGS_FULL = common.TERMSIGS_FULL, full |
| 27 |
try: |
| 28 |
yield |
| 29 |
finally: |
| 30 |
common.TERMSIGS_DEFAULT, common.TERMSIGS_FULL = prev_def, prev_full |
| 31 |
|
| 32 |
|
| 33 |
@skip.if_win32() |
| 34 |
class test_reset_signals: |
| 35 |
|
| 36 |
def test_shutdown_handler(self): |
| 37 |
with patch('sys.exit') as exit: |
| 38 |
_shutdown_cleanup(15, Mock()) |
| 39 |
exit.assert_called() |
| 40 |
assert os.WTERMSIG(exit.call_args[0][0]) == 15 |
| 41 |
|
| 42 |
def test_does_not_reset_ignored_signal(self, sigs=['SIGTERM']): |
| 43 |
with self.assert_context(sigs, [], signal.SIG_IGN) as (_, SET): |
| 44 |
SET.assert_not_called() |
| 45 |
|
| 46 |
def test_does_not_reset_if_current_is_None(self, sigs=['SIGTERM']): |
| 47 |
with self.assert_context(sigs, [], None) as (_, SET): |
| 48 |
SET.assert_not_called() |
| 49 |
|
| 50 |
def test_resets_for_SIG_DFL(self, sigs=['SIGTERM', 'SIGINT', 'SIGUSR1']): |
| 51 |
with self.assert_context(sigs, [], signal.SIG_DFL) as (_, SET): |
| 52 |
SET.assert_has_calls([ |
| 53 |
call(signo(sig), _shutdown_cleanup) for sig in sigs |
| 54 |
]) |
| 55 |
|
| 56 |
def test_resets_for_obj(self, sigs=['SIGTERM', 'SIGINT', 'SIGUSR1']): |
| 57 |
with self.assert_context(sigs, [], object()) as (_, SET): |
| 58 |
SET.assert_has_calls([ |
| 59 |
call(signo(sig), _shutdown_cleanup) for sig in sigs |
| 60 |
]) |
| 61 |
|
| 62 |
def test_handles_errors(self, sigs=['SIGTERM']): |
| 63 |
for exc in (OSError(), AttributeError(), |
| 64 |
ValueError(), RuntimeError()): |
| 65 |
with self.assert_context(sigs, [], signal.SIG_DFL, exc) as (_, S): |
| 66 |
S.assert_called() |
| 67 |
|
| 68 |
@contextmanager |
| 69 |
def assert_context(self, default, full, get_returns=None, set_effect=None): |
| 70 |
with termsigs(default, full): |
| 71 |
with patch('signal.getsignal') as GET: |
| 72 |
with patch('signal.signal') as SET: |
| 73 |
GET.return_value = get_returns |
| 74 |
SET.side_effect = set_effect |
| 75 |
reset_signals() |
| 76 |
GET.assert_has_calls([ |
| 77 |
call(signo(sig)) for sig in default |
| 78 |
]) |
| 79 |
yield GET, SET |
| 80 |
|
| 81 |
|
| 82 |
class test_restart_state: |
| 83 |
|
| 84 |
def test_raises(self): |
| 85 |
s = restart_state(100, 1) # max 100 restarts in 1 second. |
| 86 |
s.R = 99 |
| 87 |
s.step() |
| 88 |
with pytest.raises(s.RestartFreqExceeded): |
| 89 |
s.step() |
| 90 |
|
| 91 |
def test_time_passed_resets_counter(self): |
| 92 |
s = restart_state(100, 10) |
| 93 |
s.R, s.T = 100, time() |
| 94 |
with pytest.raises(s.RestartFreqExceeded): |
| 95 |
s.step() |
| 96 |
s.R, s.T = 100, time() |
| 97 |
s.step(time() + 20) |
| 98 |
assert s.R == 1 |