test_rdb.py 3.1 KB

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