/Lib/test/test_wait4.py

http://unladen-swallow.googlecode.com/ · Python · 37 lines · 27 code · 6 blank · 4 comment · 7 complexity · a97de02aaa47696bf6c17288a9c4bb2e MD5 · raw file

  1. """This test checks for correct wait4() behavior.
  2. """
  3. import os
  4. import time
  5. from test.fork_wait import ForkWait
  6. from test.test_support import TestSkipped, run_unittest, reap_children
  7. try:
  8. os.fork
  9. except AttributeError:
  10. raise TestSkipped, "os.fork not defined -- skipping test_wait4"
  11. try:
  12. os.wait4
  13. except AttributeError:
  14. raise TestSkipped, "os.wait4 not defined -- skipping test_wait4"
  15. class Wait4Test(ForkWait):
  16. def wait_impl(self, cpid):
  17. for i in range(10):
  18. # wait4() shouldn't hang, but some of the buildbots seem to hang
  19. # in the forking tests. This is an attempt to fix the problem.
  20. spid, status, rusage = os.wait4(cpid, os.WNOHANG)
  21. if spid == cpid:
  22. break
  23. time.sleep(1.0)
  24. self.assertEqual(spid, cpid)
  25. self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
  26. self.assertTrue(rusage)
  27. def test_main():
  28. run_unittest(Wait4Test)
  29. reap_children()
  30. if __name__ == "__main__":
  31. test_main()