blob: 86440b50b2cf1eeee103b6d146c3b30193e9df87 [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()
Christian Heimes26855632008-01-27 23:50:43 +000059 # does eventually. Try flushing several times in
Christian Heimesaf98da12008-01-27 15:18:18 +000060 # 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()
Guido van Rossum806c2462007-08-06 23:33:07 +000065 except IOError:
66 if not limit_set:
67 raise
68 if limit_set:
69 # Close will attempt to flush the byte we wrote
70 # Restore limit first to avoid getting a spurious error
71 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
72 finally:
73 f.close()
Thomas Woutersb2137042007-02-01 18:02:27 +000074 finally:
Guido van Rossum806c2462007-08-06 23:33:07 +000075 if limit_set:
76 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Christian Heimesaf98da12008-01-27 15:18:18 +000077 test_support.unlink(test_support.TESTFN)
Thomas Woutersb2137042007-02-01 18:02:27 +000078
79 def test_fsize_toobig(self):
80 # Be sure that setrlimit is checking for really large values
81 too_big = 10**50
82 try:
83 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
84 except AttributeError:
85 pass
86 else:
87 try:
88 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
89 except (OverflowError, ValueError):
90 pass
91 try:
92 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
93 except (OverflowError, ValueError):
94 pass
95
96 def test_getrusage(self):
97 self.assertRaises(TypeError, resource.getrusage)
98 self.assertRaises(TypeError, resource.getrusage, 42, 42)
99 usageself = resource.getrusage(resource.RUSAGE_SELF)
100 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
101 # May not be available on all systems.
102 try:
103 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
104 except (ValueError, AttributeError):
105 pass
106
107def test_main(verbose=None):
108 test_support.run_unittest(ResourceTest)
109
110if __name__ == "__main__":
111 test_main()