Thomas Wouters | b213704 | 2007-02-01 18:02:27 +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 | |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 4 | import os |
| 5 | import resource |
| 6 | import time |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 8 | # 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] | 9 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 10 | class ResourceTest(unittest.TestCase): |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 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) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 17 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 18 | def test_fsize_ismax(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +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. |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 42 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 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") |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 52 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 53 | f.write(b"X" * 1024) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 54 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 55 | f.write(b"Y") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 56 | f.flush() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 57 | # On some systems (e.g., Ubuntu on hppa) the flush() |
| 58 | # doesn't always cause the exception, but the close() |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 59 | # does eventually. Try flushing several times in |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 60 | # an attempt to ensure the file is really synced and |
| 61 | # the exception raised. |
| 62 | for i in range(5): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 63 | time.sleep(.1) |
| 64 | f.flush() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 65 | 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 Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 74 | finally: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 75 | if limit_set: |
| 76 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 77 | test_support.unlink(test_support.TESTFN) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 78 | |
| 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 | |
| 107 | def test_main(verbose=None): |
| 108 | test_support.run_unittest(ResourceTest) |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | test_main() |