| 1 |
from unittest.mock import Mock |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from celery.utils import debug |
| 6 |
|
| 7 |
|
| 8 |
def test_on_blocking(patching): |
| 9 |
getframeinfo = patching('inspect.getframeinfo') |
| 10 |
frame = Mock(name='frame') |
| 11 |
with pytest.raises(RuntimeError): |
| 12 |
debug._on_blocking(1, frame) |
| 13 |
getframeinfo.assert_called_with(frame) |
| 14 |
|
| 15 |
|
| 16 |
def test_blockdetection(patching): |
| 17 |
signals = patching('celery.utils.debug.signals') |
| 18 |
with debug.blockdetection(10): |
| 19 |
signals.arm_alarm.assert_called_with(10) |
| 20 |
signals.__setitem__.assert_called_with('ALRM', debug._on_blocking) |
| 21 |
signals.__setitem__.assert_called_with('ALRM', signals['ALRM']) |
| 22 |
signals.reset_alarm.assert_called_with() |
| 23 |
|
| 24 |
|
| 25 |
def test_sample_mem(patching): |
| 26 |
mem_rss = patching('celery.utils.debug.mem_rss') |
| 27 |
prev, debug._mem_sample = debug._mem_sample, [] |
| 28 |
try: |
| 29 |
debug.sample_mem() |
| 30 |
assert debug._mem_sample[0] is mem_rss() |
| 31 |
finally: |
| 32 |
debug._mem_sample = prev |
| 33 |
|
| 34 |
|
| 35 |
def test_sample(): |
| 36 |
x = list(range(100)) |
| 37 |
assert list(debug.sample(x, 10)) == [ |
| 38 |
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, |
| 39 |
] |
| 40 |
x = list(range(91)) |
| 41 |
assert list(debug.sample(x, 10)) == [ |
| 42 |
0, 9, 18, 27, 36, 45, 54, 63, 72, 81, |
| 43 |
] |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize('f,precision,expected', [ |
| 47 |
(10, 5, '10'), |
| 48 |
(10.45645234234, 5, '10.456'), |
| 49 |
]) |
| 50 |
def test_hfloat(f, precision, expected): |
| 51 |
assert str(debug.hfloat(f, precision)) == expected |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize('byt,expected', [ |
| 55 |
(2 ** 20, '1MB'), |
| 56 |
(4 * 2 ** 20, '4MB'), |
| 57 |
(2 ** 16, '64KB'), |
| 58 |
(2 ** 16, '64KB'), |
| 59 |
(2 ** 8, '256b'), |
| 60 |
]) |
| 61 |
def test_humanbytes(byt, expected): |
| 62 |
assert debug.humanbytes(byt) == expected |
| 63 |
|
| 64 |
|
| 65 |
def test_mem_rss(patching): |
| 66 |
humanbytes = patching('celery.utils.debug.humanbytes') |
| 67 |
ps = patching('celery.utils.debug.ps') |
| 68 |
ret = debug.mem_rss() |
| 69 |
ps.assert_called_with() |
| 70 |
ps().memory_info.assert_called_with() |
| 71 |
humanbytes.assert_called_with(ps().memory_info().rss) |
| 72 |
assert ret is humanbytes() |
| 73 |
ps.return_value = None |
| 74 |
assert debug.mem_rss() is None |
| 75 |
|
| 76 |
|
| 77 |
def test_ps(patching): |
| 78 |
Process = patching('celery.utils.debug.Process') |
| 79 |
getpid = patching('os.getpid') |
| 80 |
prev, debug._process = debug._process, None |
| 81 |
try: |
| 82 |
debug.ps() |
| 83 |
Process.assert_called_with(getpid()) |
| 84 |
assert debug._process is Process() |
| 85 |
finally: |
| 86 |
debug._process = prev |