test_multiprocessing.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/python
  2. from base_test import DakTestCase
  3. from daklib.dakmultiprocessing import DakProcessPool, \
  4. PROC_STATUS_SUCCESS, PROC_STATUS_MISCFAILURE, \
  5. PROC_STATUS_EXCEPTION, PROC_STATUS_SIGNALRAISED
  6. import signal
  7. def test_function(num, num2):
  8. from os import kill, getpid
  9. if num == 1:
  10. sigs = [signal.SIGTERM, signal.SIGPIPE, signal.SIGALRM, signal.SIGHUP]
  11. kill(getpid(), sigs[num2])
  12. if num2 == 3:
  13. raise Exception('Test uncaught exception handling')
  14. if num == 0 and num2 == 1:
  15. return (PROC_STATUS_MISCFAILURE, 'Test custom error return')
  16. return (PROC_STATUS_SUCCESS, 'blah, %d, %d' % (num, num2))
  17. class DakProcessPoolTestCase(DakTestCase):
  18. def testPool(self):
  19. def alarm_handler(signum, frame):
  20. raise AssertionError('Timed out')
  21. # Shouldn't take us more than 15 seconds to run this test
  22. signal.signal(signal.SIGALRM, alarm_handler)
  23. signal.alarm(15)
  24. p = DakProcessPool()
  25. for s in range(3):
  26. for j in range(4):
  27. p.apply_async(test_function, [s, j])
  28. p.close()
  29. p.join()
  30. signal.alarm(0)
  31. signal.signal(signal.SIGALRM, signal.SIG_DFL)
  32. expected = [(PROC_STATUS_SUCCESS, 'blah, 0, 0'),
  33. (PROC_STATUS_MISCFAILURE, 'Test custom error return'),
  34. (PROC_STATUS_SUCCESS, 'blah, 0, 2'),
  35. (PROC_STATUS_EXCEPTION, 'Test uncaught exception handling'),
  36. (PROC_STATUS_SIGNALRAISED, 15),
  37. (PROC_STATUS_SIGNALRAISED, 13),
  38. (PROC_STATUS_SIGNALRAISED, 14),
  39. (PROC_STATUS_SIGNALRAISED, 1),
  40. (PROC_STATUS_SUCCESS, 'blah, 2, 0'),
  41. (PROC_STATUS_SUCCESS, 'blah, 2, 1'),
  42. (PROC_STATUS_SUCCESS, 'blah, 2, 2'),
  43. (PROC_STATUS_EXCEPTION, 'Test uncaught exception handling')]
  44. self.assertEqual( len(p.results), len(expected) )
  45. for r in range(len(p.results)):
  46. self.assertEqual(p.results[r], expected[r])