Debian Codegraph @

pypy/module/test_lib_pypy/test_resource.py

raw symbols SCIP
1 from __future__ import absolute_import
2 import sys
3 import pytest
4
5 import os
6 if os.name != 'posix':
7 pytest.skip('resource.h only available on unix')
8
9 try:
10 from lib_pypy import resource
11 except (ImportError, SyntaxError) as e:
12 pytest.skip(str(e))
13
14
15 def test_getrusage():
16 x = resource.getrusage(resource.RUSAGE_SELF)
17 assert len(x) == 16
18 assert x[0] == x[-16] == x.ru_utime
19 assert x[1] == x[-15] == x.ru_stime
20 assert x[2] == x[-14] == x.ru_maxrss
21 assert x[3] == x[-13] == x.ru_ixrss
22 assert x[4] == x[-12] == x.ru_idrss
23 assert x[5] == x[-11] == x.ru_isrss
24 assert x[6] == x[-10] == x.ru_minflt
25 assert x[7] == x[-9] == x.ru_majflt
26 assert x[8] == x[-8] == x.ru_nswap
27 assert x[9] == x[-7] == x.ru_inblock
28 assert x[10] == x[-6] == x.ru_oublock
29 assert x[11] == x[-5] == x.ru_msgsnd
30 assert x[12] == x[-4] == x.ru_msgrcv
31 assert x[13] == x[-3] == x.ru_nsignals
32 assert x[14] == x[-2] == x.ru_nvcsw
33 assert x[15] == x[-1] == x.ru_nivcsw
34 for i in range(16):
35 if i < 2:
36 expected_type = float
37 else:
38 expected_type = int
39 assert isinstance(x[i], expected_type)
40
41 def test_getrlimit():
42 x = resource.getrlimit(resource.RLIMIT_CPU)
43 assert isinstance(x, tuple)
44 assert len(x) == 2
45 assert isinstance(x[0], int)
46 assert isinstance(x[1], int)
47
48 def test_setrlimit():
49 # minimal "does not crash" test
50 x, y = resource.getrlimit(resource.RLIMIT_CPU)
51 resource.setrlimit(resource.RLIMIT_CPU, (x, y))
52 # sometimes, x and y are very large (more than 53 bits).
53 # for these huge values, int(float(x)) > x...
54 xf = x + (0.2 if x >= 0 else -0.2)
55 yf = y + (0.3 if y >= 0 else -0.3)
56 if int(xf) == x and int(yf) == y:
57 resource.setrlimit(resource.RLIMIT_CPU, (x, y)) # truncated to ints
58
59 if sys.platform.startswith("linux") and hasattr(resource, 'prlimit'):
60 def test_prlimit():
61 old_limits = resource.getrlimit(resource.RLIMIT_STACK)
62 assert resource.prlimit(0, resource.RLIMIT_STACK) == old_limits
63 assert resource.prlimit(os.getpid(), resource.RLIMIT_STACK) == old_limits
64
65 # Unchanged
66 assert resource.getrlimit(resource.RLIMIT_STACK) == old_limits
67
68 # Raise the soft limit to match the hard limit
69 new_limits = (old_limits[1], old_limits[1])
70 assert resource.prlimit(os.getpid(), resource.RLIMIT_STACK, new_limits) == old_limits
71 # And make sure it was raised
72 assert resource.prlimit(0, resource.RLIMIT_STACK, new_limits) == new_limits
73 assert resource.getrlimit(resource.RLIMIT_STACK) == new_limits
74
75 # Change it back
76 assert resource.prlimit(0, resource.RLIMIT_STACK, old_limits) == new_limits
77 # And make sure it was changed back
78 assert resource.prlimit(os.getpid(), resource.RLIMIT_STACK, old_limits) == old_limits
79 assert resource.getrlimit(resource.RLIMIT_STACK) == old_limits