blob: 496259f6320013e1427d824379eded67f70fc6ac [file] [log] [blame]
Walter Dörwald9fab9a72007-01-20 17:28:31 +00001import unittest
2from test import test_support
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00003
Neal Norwitz041ee5d2008-01-27 17:10:50 +00004import os
5import resource
6import time
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00007
Walter Dörwald9fab9a72007-01-20 17:28:31 +00008# This test is checking a few specific problem spots with the resource module.
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00009
Walter Dörwald9fab9a72007-01-20 17:28:31 +000010class ResourceTest(unittest.TestCase):
Walter Dörwald66262ab2007-01-20 18:19:33 +000011
12 def test_args(self):
13 self.assertRaises(TypeError, resource.getrlimit)
14 self.assertRaises(TypeError, resource.getrlimit, 42, 42)
15 self.assertRaises(TypeError, resource.setrlimit)
16 self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
17
Walter Dörwald9fab9a72007-01-20 17:28:31 +000018 def test_fsize_ismax(self):
Walter Dörwald9fab9a72007-01-20 17:28:31 +000019 try:
20 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
21 except AttributeError:
22 pass
23 else:
24 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
25 # number on a platform with large file support. On these platforms,
26 # we need to test that the get/setrlimit functions properly convert
27 # the number to a C long long and that the conversion doesn't raise
28 # an error.
29 self.assertEqual(resource.RLIM_INFINITY, max)
30 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000031
Walter Dörwald9fab9a72007-01-20 17:28:31 +000032 def test_fsize_enforced(self):
33 try:
34 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
35 except AttributeError:
36 pass
37 else:
38 # Check to see what happens when the RLIMIT_FSIZE is small. Some
39 # versions of Python were terminated by an uncaught SIGXFSZ, but
40 # pythonrun.c has been fixed to ignore that exception. If so, the
41 # write() should return EFBIG when the limit is exceeded.
Tim Petersf733abb2007-01-30 03:03:46 +000042
Walter Dörwald9fab9a72007-01-20 17:28:31 +000043 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
44 # to change it raise ValueError instead.
45 try:
46 try:
47 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
48 limit_set = True
49 except ValueError:
50 limit_set = False
51 f = open(test_support.TESTFN, "wb")
Walter Dörwald9fab9a72007-01-20 17:28:31 +000052 try:
Nick Coghlan12adef92007-07-24 14:39:23 +000053 f.write("X" * 1024)
54 try:
55 f.write("Y")
56 f.flush()
Neal Norwitz7f47d932008-01-26 23:13:46 +000057 # On some systems (e.g., Ubuntu on hppa) the flush()
Neal Norwitz46c61b22008-01-27 05:02:34 +000058 # doesn't always cause the exception, but the close()
59 # does eventually. Try closing several times in
60 # an attempt to ensure the file is really synced and
61 # the exception raised.
62 for i in range(5):
Neal Norwitz041ee5d2008-01-27 17:10:50 +000063 time.sleep(.1)
64 f.flush()
Neal Norwitz46c61b22008-01-27 05:02:34 +000065 f.close()
Nick Coghlan12adef92007-07-24 14:39:23 +000066 except IOError:
67 if not limit_set:
68 raise
69 if limit_set:
70 # Close will attempt to flush the byte we wrote
71 # Restore limit first to avoid getting a spurious error
72 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
73 finally:
74 f.close()
Walter Dörwald9fab9a72007-01-20 17:28:31 +000075 finally:
Nick Coghlan12adef92007-07-24 14:39:23 +000076 if limit_set:
77 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Neal Norwitza1215082008-01-26 21:02:45 +000078 test_support.unlink(test_support.TESTFN)
Jason Tishlere4a070a2002-12-12 18:13:36 +000079
Walter Dörwald9fab9a72007-01-20 17:28:31 +000080 def test_fsize_toobig(self):
81 # Be sure that setrlimit is checking for really large values
82 too_big = 10L**50
83 try:
84 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
85 except AttributeError:
86 pass
87 else:
88 try:
89 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
90 except (OverflowError, ValueError):
91 pass
92 try:
93 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
94 except (OverflowError, ValueError):
95 pass
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000096
Walter Dörwald66262ab2007-01-20 18:19:33 +000097 def test_getrusage(self):
98 self.assertRaises(TypeError, resource.getrusage)
99 self.assertRaises(TypeError, resource.getrusage, 42, 42)
100 usageself = resource.getrusage(resource.RUSAGE_SELF)
101 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
102 # May not be available on all systems.
103 try:
104 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
Walter Dörwaldd4143022007-01-20 19:03:17 +0000105 except (ValueError, AttributeError):
Walter Dörwald66262ab2007-01-20 18:19:33 +0000106 pass
107
Walter Dörwald9fab9a72007-01-20 17:28:31 +0000108def test_main(verbose=None):
109 test_support.run_unittest(ResourceTest)
110
111if __name__ == "__main__":
112 test_main()