Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 3 | import time |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 4 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 5 | resource = support.import_module('resource') |
| 6 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +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 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 9 | class ResourceTest(unittest.TestCase): |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 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) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 16 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 17 | def test_fsize_ismax(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 18 | try: |
| 19 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 20 | except AttributeError: |
| 21 | pass |
| 22 | else: |
| 23 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big |
| 24 | # number on a platform with large file support. On these platforms, |
| 25 | # we need to test that the get/setrlimit functions properly convert |
| 26 | # the number to a C long long and that the conversion doesn't raise |
| 27 | # an error. |
| 28 | self.assertEqual(resource.RLIM_INFINITY, max) |
| 29 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 30 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 31 | def test_fsize_enforced(self): |
| 32 | try: |
| 33 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 34 | except AttributeError: |
| 35 | pass |
| 36 | else: |
| 37 | # Check to see what happens when the RLIMIT_FSIZE is small. Some |
| 38 | # versions of Python were terminated by an uncaught SIGXFSZ, but |
| 39 | # pythonrun.c has been fixed to ignore that exception. If so, the |
| 40 | # write() should return EFBIG when the limit is exceeded. |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 42 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts |
| 43 | # to change it raise ValueError instead. |
| 44 | try: |
| 45 | try: |
| 46 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 47 | limit_set = True |
| 48 | except ValueError: |
| 49 | limit_set = False |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 50 | f = open(support.TESTFN, "wb") |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 51 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 52 | f.write(b"X" * 1024) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 53 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 54 | f.write(b"Y") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 55 | f.flush() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 56 | # On some systems (e.g., Ubuntu on hppa) the flush() |
| 57 | # doesn't always cause the exception, but the close() |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 58 | # does eventually. Try flushing several times in |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 59 | # an attempt to ensure the file is really synced and |
| 60 | # the exception raised. |
| 61 | for i in range(5): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 62 | time.sleep(.1) |
| 63 | f.flush() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 64 | except IOError: |
| 65 | if not limit_set: |
| 66 | raise |
| 67 | if limit_set: |
| 68 | # Close will attempt to flush the byte we wrote |
| 69 | # Restore limit first to avoid getting a spurious error |
| 70 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
| 71 | finally: |
| 72 | f.close() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 73 | finally: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 74 | if limit_set: |
| 75 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 76 | support.unlink(support.TESTFN) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 77 | |
| 78 | def test_fsize_toobig(self): |
| 79 | # Be sure that setrlimit is checking for really large values |
| 80 | too_big = 10**50 |
| 81 | try: |
| 82 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 83 | except AttributeError: |
| 84 | pass |
| 85 | else: |
| 86 | try: |
| 87 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 88 | except (OverflowError, ValueError): |
| 89 | pass |
| 90 | try: |
| 91 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 92 | except (OverflowError, ValueError): |
| 93 | pass |
| 94 | |
| 95 | def test_getrusage(self): |
| 96 | self.assertRaises(TypeError, resource.getrusage) |
| 97 | self.assertRaises(TypeError, resource.getrusage, 42, 42) |
| 98 | usageself = resource.getrusage(resource.RUSAGE_SELF) |
| 99 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 100 | # May not be available on all systems. |
| 101 | try: |
| 102 | usageboth = resource.getrusage(resource.RUSAGE_BOTH) |
| 103 | except (ValueError, AttributeError): |
| 104 | pass |
| 105 | |
| 106 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 107 | support.run_unittest(ResourceTest) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 108 | |
| 109 | if __name__ == "__main__": |
| 110 | test_main() |