Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 3 | |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 5 | import os, resource |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 7 | # This test is checking a few specific problem spots with the resource module. |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 9 | class ResourceTest(unittest.TestCase): |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame^] | 10 | |
| 11 | def test_args(self): |
| 12 | self.assertRaises(TypeError, resource.getrlimit) |
| 13 | self.assertRaises(TypeError, resource.getrlimit, 42, 42) |
| 14 | self.assertRaises(TypeError, resource.setrlimit) |
| 15 | self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42) |
| 16 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 17 | def test_fsize_ismax(self): |
| 18 | |
| 19 | 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 Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 32 | 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. |
| 42 | |
| 43 | # 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") |
| 52 | f.write("X" * 1024) |
| 53 | try: |
| 54 | f.write("Y") |
| 55 | f.flush() |
| 56 | except IOError: |
| 57 | if not limit_set: |
| 58 | raise |
| 59 | f.close() |
| 60 | os.unlink(test_support.TESTFN) |
| 61 | finally: |
| 62 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 63 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 64 | def test_fsize_toobig(self): |
| 65 | # Be sure that setrlimit is checking for really large values |
| 66 | too_big = 10L**50 |
| 67 | try: |
| 68 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 69 | except AttributeError: |
| 70 | pass |
| 71 | else: |
| 72 | try: |
| 73 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 74 | except (OverflowError, ValueError): |
| 75 | pass |
| 76 | try: |
| 77 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 78 | except (OverflowError, ValueError): |
| 79 | pass |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 80 | |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame^] | 81 | def test_getrusage(self): |
| 82 | self.assertRaises(TypeError, resource.getrusage) |
| 83 | self.assertRaises(TypeError, resource.getrusage, 42, 42) |
| 84 | usageself = resource.getrusage(resource.RUSAGE_SELF) |
| 85 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 86 | # May not be available on all systems. |
| 87 | try: |
| 88 | usageboth = resource.getrusage(resource.RUSAGE_BOTH) |
| 89 | except ValueError: |
| 90 | pass |
| 91 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 92 | def test_main(verbose=None): |
| 93 | test_support.run_unittest(ResourceTest) |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | test_main() |