test_debug.py 2.3 KB

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