summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-09-20 16:19:04 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-09-20 16:19:04 +0200
commit840cbd7bf63669055b8e230ac3bbcab8fbf187b6 (patch)
tree6a8778b9aad86a6b4a8603c9f597aa1c0454cad5
parent043752fe55a95973f78e73bdbd7a80ab6c2cbd79 (diff)
downloadpsutil-840cbd7bf63669055b8e230ac3bbcab8fbf187b6.tar.gz
#887: add another test for avail mem
-rw-r--r--psutil/tests/test_linux.py34
1 files changed, 31 insertions, 3 deletions
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 6b5f9706..068c9195 100644
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -246,7 +246,7 @@ class TestSystemVirtualMemory(unittest.TestCase):
side_effect=open_mock) as m:
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
- ret = psutil._pslinux.virtual_memory()
+ ret = psutil.virtual_memory()
assert m.called
self.assertEqual(len(ws), 1)
w = ws[0]
@@ -257,7 +257,9 @@ class TestSystemVirtualMemory(unittest.TestCase):
self.assertEqual(ret.active, 0)
self.assertEqual(ret.inactive, 0)
- def test_calculate_avail(self):
+ def test_calculate_avail_old_kernels(self):
+ # Make sure that our calculation of avail mem for old kernels
+ # is off by max 2%.
from psutil._pslinux import calculate_avail_vmem
from psutil._pslinux import open_binary
@@ -273,6 +275,32 @@ class TestSystemVirtualMemory(unittest.TestCase):
diff_percent = abs(a - b) / a * 100
self.assertLess(diff_percent, 2)
+ def test_avail_comes_from_kernel(self):
+ # Make sure "MemAvailable:" coluimn is used instead of relying
+ # on our internal algorithm to calculate avail mem.
+ def open_mock(*args, **kwargs):
+ return io.BytesIO(textwrap.dedent("""\
+ Active: 9444728 kB
+ Active(anon): 6145416 kB
+ Active(file): 2950064 kB
+ Buffers: 287952 kB
+ Cached: 4818144 kB
+ Inactive(file): 1578132 kB
+ Inactive(anon): 574764 kB
+ Inactive(file): 1567648 kB
+ MemAvailable: 6574984 kB
+ MemFree: 2057400 kB
+ MemTotal: 16325648 kB
+ Shmem: 577588 kB
+ SReclaimable: 346648 kB
+ """).encode())
+
+ with mock.patch('psutil._pslinux.open', create=True,
+ side_effect=open_mock) as m:
+ ret = psutil.virtual_memory()
+ assert m.called
+ self.assertEqual(ret.available, 6574984 * 1024)
+
# =====================================================================
# system swap memory
@@ -306,7 +334,7 @@ class TestSystemSwapMemory(unittest.TestCase):
with mock.patch('psutil._pslinux.open', create=True) as m:
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
- ret = psutil._pslinux.swap_memory()
+ ret = psutil.swap_memory()
assert m.called
self.assertEqual(len(ws), 1)
w = ws[0]