blob: 225f88278deac9ab5de814c05653ea10cc520c21 [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()
Neal Norwitzc5198092008-01-27 18:09:48 +000059 # does eventually. Try flushing several times in
Neal Norwitz46c61b22008-01-27 05:02:34 +000060 # 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()
Nick Coghlan12adef92007-07-24 14:39:23 +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()
Walter Dörwald9fab9a72007-01-20 17:28:31 +000074 finally:
Nick Coghlan12adef92007-07-24 14:39:23 +000075 if limit_set:
76 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Neal Norwitza1215082008-01-26 21:02:45 +000077 test_support.unlink(test_support.TESTFN)
Jason Tishlere4a070a2002-12-12 18:13:36 +000078
Walter Dörwald9fab9a72007-01-20 17:28:31 +000079 def test_fsize_toobig(self):
80 # Be sure that setrlimit is checking for really large values
81 too_big = 10L**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
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000095
Walter Dörwald66262ab2007-01-20 18:19:33 +000096 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)
Walter Dörwaldd4143022007-01-20 19:03:17 +0000104 except (ValueError, AttributeError):
Walter Dörwald66262ab2007-01-20 18:19:33 +0000105 pass
106
Walter Dörwald9fab9a72007-01-20 17:28:31 +0000107def test_main(verbose=None):
108 test_support.run_unittest(ResourceTest)
109
110if __name__ == "__main__":
111 test_main()