/Lib/test/test_resource.py

http://unladen-swallow.googlecode.com/ · Python · 110 lines · 78 code · 11 blank · 21 comment · 28 complexity · 25fc542294859b25340d5a790c507b4f MD5 · raw file

  1. import unittest
  2. from test import test_support
  3. import resource
  4. import time
  5. # This test is checking a few specific problem spots with the resource module.
  6. class ResourceTest(unittest.TestCase):
  7. def test_args(self):
  8. self.assertRaises(TypeError, resource.getrlimit)
  9. self.assertRaises(TypeError, resource.getrlimit, 42, 42)
  10. self.assertRaises(TypeError, resource.setrlimit)
  11. self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
  12. def test_fsize_ismax(self):
  13. try:
  14. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  15. except AttributeError:
  16. pass
  17. else:
  18. # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
  19. # number on a platform with large file support. On these platforms,
  20. # we need to test that the get/setrlimit functions properly convert
  21. # the number to a C long long and that the conversion doesn't raise
  22. # an error.
  23. self.assertEqual(resource.RLIM_INFINITY, max)
  24. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  25. def test_fsize_enforced(self):
  26. try:
  27. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  28. except AttributeError:
  29. pass
  30. else:
  31. # Check to see what happens when the RLIMIT_FSIZE is small. Some
  32. # versions of Python were terminated by an uncaught SIGXFSZ, but
  33. # pythonrun.c has been fixed to ignore that exception. If so, the
  34. # write() should return EFBIG when the limit is exceeded.
  35. # At least one platform has an unlimited RLIMIT_FSIZE and attempts
  36. # to change it raise ValueError instead.
  37. try:
  38. try:
  39. resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
  40. limit_set = True
  41. except ValueError:
  42. limit_set = False
  43. f = open(test_support.TESTFN, "wb")
  44. try:
  45. f.write("X" * 1024)
  46. try:
  47. f.write("Y")
  48. f.flush()
  49. # On some systems (e.g., Ubuntu on hppa) the flush()
  50. # doesn't always cause the exception, but the close()
  51. # does eventually. Try flushing several times in
  52. # an attempt to ensure the file is really synced and
  53. # the exception raised.
  54. for i in range(5):
  55. time.sleep(.1)
  56. f.flush()
  57. except IOError:
  58. if not limit_set:
  59. raise
  60. if limit_set:
  61. # Close will attempt to flush the byte we wrote
  62. # Restore limit first to avoid getting a spurious error
  63. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  64. finally:
  65. f.close()
  66. finally:
  67. if limit_set:
  68. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  69. test_support.unlink(test_support.TESTFN)
  70. def test_fsize_toobig(self):
  71. # Be sure that setrlimit is checking for really large values
  72. too_big = 10L**50
  73. try:
  74. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  75. except AttributeError:
  76. pass
  77. else:
  78. try:
  79. resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
  80. except (OverflowError, ValueError):
  81. pass
  82. try:
  83. resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
  84. except (OverflowError, ValueError):
  85. pass
  86. def test_getrusage(self):
  87. self.assertRaises(TypeError, resource.getrusage)
  88. self.assertRaises(TypeError, resource.getrusage, 42, 42)
  89. usageself = resource.getrusage(resource.RUSAGE_SELF)
  90. usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
  91. # May not be available on all systems.
  92. try:
  93. usageboth = resource.getrusage(resource.RUSAGE_BOTH)
  94. except (ValueError, AttributeError):
  95. pass
  96. def test_main(verbose=None):
  97. test_support.run_unittest(ResourceTest)
  98. if __name__ == "__main__":
  99. test_main()