test_debug.py 2.3 KB

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