test_debug.py 2.8 KB

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