test_wait3.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """This test checks for correct wait3() behavior.
  2. """
  3. import os
  4. import subprocess
  5. import sys
  6. import time
  7. import unittest
  8. from test.fork_wait import ForkWait
  9. from test import support
  10. if not support.has_fork_support:
  11. raise unittest.SkipTest("requires working os.fork()")
  12. if not hasattr(os, 'wait3'):
  13. raise unittest.SkipTest("os.wait3 not defined")
  14. class Wait3Test(ForkWait):
  15. def wait_impl(self, cpid, *, exitcode):
  16. # This many iterations can be required, since some previously run
  17. # tests (e.g. test_ctypes) could have spawned a lot of children
  18. # very quickly.
  19. deadline = time.monotonic() + support.SHORT_TIMEOUT
  20. while time.monotonic() <= deadline:
  21. # wait3() shouldn't hang, but some of the buildbots seem to hang
  22. # in the forking tests. This is an attempt to fix the problem.
  23. spid, status, rusage = os.wait3(os.WNOHANG)
  24. if spid == cpid:
  25. break
  26. time.sleep(0.1)
  27. self.assertEqual(spid, cpid)
  28. self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
  29. self.assertTrue(rusage)
  30. def test_wait3_rusage_initialized(self):
  31. # Ensure a successful wait3() call where no child was ready to report
  32. # its exit status does not return uninitialized memory in the rusage
  33. # structure. See bpo-36279.
  34. args = [sys.executable, '-c', 'import sys; sys.stdin.read()']
  35. proc = subprocess.Popen(args, stdin=subprocess.PIPE)
  36. try:
  37. pid, status, rusage = os.wait3(os.WNOHANG)
  38. self.assertEqual(0, pid)
  39. self.assertEqual(0, status)
  40. self.assertEqual(0, sum(rusage))
  41. finally:
  42. proc.stdin.close()
  43. proc.wait()
  44. def tearDownModule():
  45. support.reap_children()
  46. if __name__ == "__main__":
  47. unittest.main()