blob: 43ff3728c9095896134600ca9a5b37ce079477ef [file] [log] [blame]
Thomas Woutersb2137042007-02-01 18:02:27 +00001import unittest
2from test import test_support
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00003
Christian Heimes412dc9c2008-01-27 18:55:54 +00004import os
5import resource
6import time
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00007
Thomas Woutersb2137042007-02-01 18:02:27 +00008# This test is checking a few specific problem spots with the resource module.
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00009
Thomas Woutersb2137042007-02-01 18:02:27 +000010class ResourceTest(unittest.TestCase):
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000011
Thomas Woutersb2137042007-02-01 18:02:27 +000012 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)
Jason Tishlere4a070a2002-12-12 18:13:36 +000017
Thomas Woutersb2137042007-02-01 18:02:27 +000018 def test_fsize_ismax(self):
Thomas Woutersb2137042007-02-01 18:02:27 +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
Thomas Woutersb2137042007-02-01 18:02:27 +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.
Thomas Wouters9fe394c2007-02-05 01:24:16 +000042
Thomas Woutersb2137042007-02-01 18:02:27 +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")
Thomas Woutersb2137042007-02-01 18:02:27 +000052 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000053 f.write(b"X" * 1024)
Guido van Rossum806c2462007-08-06 23:33:07 +000054 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000055 f.write(b"Y")
Guido van Rossum806c2462007-08-06 23:33:07 +000056 f.flush()
Christian Heimesaf98da12008-01-27 15:18:18 +000057 # On some systems (e.g., Ubuntu on hppa) the flush()
58 # 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):
Christian Heimes412dc9c2008-01-27 18:55:54 +000063 time.sleep(.1)
64 f.flush()
Christian Heimesaf98da12008-01-27 15:18:18 +000065 f.close()
Guido van Rossum806c2462007-08-06 23:33:07 +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()
Thomas Woutersb2137042007-02-01 18:02:27 +000075 finally:
Guido van Rossum806c2462007-08-06 23:33:07 +000076 if limit_set:
77 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Christian Heimesaf98da12008-01-27 15:18:18 +000078 test_support.unlink(test_support.TESTFN)
Thomas Woutersb2137042007-02-01 18:02:27 +000079
80 def test_fsize_toobig(self):
81 # Be sure that setrlimit is checking for really large values
82 too_big = 10**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
96
97 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)
105 except (ValueError, AttributeError):
106 pass
107
108def test_main(verbose=None):
109 test_support.run_unittest(ResourceTest)
110
111if __name__ == "__main__":
112 test_main()