| 1 |
import errno |
| 2 |
import socket |
| 3 |
from unittest.mock import Mock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import t.skip |
| 8 |
from celery.contrib.rdb import Rdb, debugger, set_trace |
| 9 |
from celery.utils.text import WhateverIO |
| 10 |
|
| 11 |
|
| 12 |
class SockErr(socket.error): |
| 13 |
errno = None |
| 14 |
|
| 15 |
|
| 16 |
class test_Rdb: |
| 17 |
|
| 18 |
@patch('celery.contrib.rdb.Rdb') |
| 19 |
def test_debugger(self, Rdb): |
| 20 |
x = debugger() |
| 21 |
assert x |
| 22 |
assert x is debugger() |
| 23 |
|
| 24 |
@patch('celery.contrib.rdb.debugger') |
| 25 |
@patch('celery.contrib.rdb._frame') |
| 26 |
def test_set_trace(self, _frame, debugger): |
| 27 |
assert set_trace(Mock()) |
| 28 |
assert set_trace() |
| 29 |
debugger.return_value.set_trace.assert_called() |
| 30 |
|
| 31 |
@patch('celery.contrib.rdb.Rdb.get_avail_port') |
| 32 |
@t.skip.if_pypy |
| 33 |
def test_rdb(self, get_avail_port): |
| 34 |
sock = Mock() |
| 35 |
get_avail_port.return_value = (sock, 8000) |
| 36 |
sock.accept.return_value = (Mock(), ['helu']) |
| 37 |
out = WhateverIO() |
| 38 |
with Rdb(out=out) as rdb: |
| 39 |
get_avail_port.assert_called() |
| 40 |
assert 'helu' in out.getvalue() |
| 41 |
|
| 42 |
# set_quit |
| 43 |
with patch('sys.settrace') as settrace: |
| 44 |
rdb.set_quit() |
| 45 |
settrace.assert_called_with(None) |
| 46 |
|
| 47 |
# set_trace |
| 48 |
with patch('celery.contrib.rdb.Pdb.set_trace') as pset: |
| 49 |
with patch('celery.contrib.rdb._frame'): |
| 50 |
rdb.set_trace() |
| 51 |
rdb.set_trace(Mock()) |
| 52 |
pset.side_effect = SockErr |
| 53 |
pset.side_effect.errno = errno.ENOENT |
| 54 |
with pytest.raises(SockErr): |
| 55 |
rdb.set_trace() |
| 56 |
|
| 57 |
# _close_session |
| 58 |
rdb._close_session() |
| 59 |
rdb.active = True |
| 60 |
rdb._handle = None |
| 61 |
rdb._client = None |
| 62 |
rdb._sock = None |
| 63 |
rdb._close_session() |
| 64 |
|
| 65 |
# do_continue |
| 66 |
rdb.set_continue = Mock() |
| 67 |
rdb.do_continue(Mock()) |
| 68 |
rdb.set_continue.assert_called_with() |
| 69 |
|
| 70 |
# do_quit |
| 71 |
rdb.set_quit = Mock() |
| 72 |
rdb.do_quit(Mock()) |
| 73 |
rdb.set_quit.assert_called_with() |
| 74 |
|
| 75 |
@patch('socket.socket') |
| 76 |
@t.skip.if_pypy |
| 77 |
def test_get_avail_port(self, sock): |
| 78 |
out = WhateverIO() |
| 79 |
sock.return_value.accept.return_value = (Mock(), ['helu']) |
| 80 |
with Rdb(out=out): |
| 81 |
pass |
| 82 |
|
| 83 |
with patch('celery.contrib.rdb.current_process') as curproc: |
| 84 |
curproc.return_value.name = 'PoolWorker-10' |
| 85 |
with Rdb(out=out): |
| 86 |
pass |
| 87 |
|
| 88 |
err = sock.return_value.bind.side_effect = SockErr() |
| 89 |
err.errno = errno.ENOENT |
| 90 |
with pytest.raises(SockErr): |
| 91 |
with Rdb(out=out): |
| 92 |
pass |
| 93 |
err.errno = errno.EADDRINUSE |
| 94 |
with pytest.raises(Exception): |
| 95 |
with Rdb(out=out): |
| 96 |
pass |
| 97 |
called = [0] |
| 98 |
|
| 99 |
def effect(*a, **kw): |
| 100 |
try: |
| 101 |
if called[0] > 50: |
| 102 |
return True |
| 103 |
raise err |
| 104 |
finally: |
| 105 |
called[0] += 1 |
| 106 |
sock.return_value.bind.side_effect = effect |
| 107 |
with Rdb(out=out): |
| 108 |
pass |