test_rdb.py 3.1 KB

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