diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-06-14 12:21:53 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-06-14 12:21:53 +0200 |
commit | 24c7ff6d9b437be35703e08d7d07d4cd73561ca0 (patch) | |
tree | d367b5acedd4ea0cacfe9b9f75b506a05c978dab | |
parent | 210352d302ae32202b7cdc5dd30d6abce9b842e8 (diff) | |
download | psutil-24c7ff6d9b437be35703e08d7d07d4cd73561ca0.tar.gz |
OSX try to fix swapmem test
-rw-r--r-- | test/_osx.py | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/test/_osx.py b/test/_osx.py index 35a0e600..c8b1f1cd 100644 --- a/test/_osx.py +++ b/test/_osx.py @@ -16,7 +16,8 @@ import psutil from psutil._compat import PY3 from test_psutil import (MEMORY_TOLERANCE, OSX, sh, get_test_subprocess, - reap_children, retry_before_failing, unittest) + reap_children, retry_before_failing, unittest, + TRAVIS) PAGESIZE = os.sysconf("SC_PAGE_SIZE") @@ -47,6 +48,33 @@ def vm_stat(field): return int(re.search('\d+', line).group(0)) * PAGESIZE +# http://code.activestate.com/recipes/578019/ +def human2bytes(s): + SYMBOLS = { + 'customary': ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'), + } + init = s + num = "" + while s and s[0:1].isdigit() or s[0:1] == '.': + num += s[0] + s = s[1:] + num = float(num) + letter = s.strip() + for name, sset in SYMBOLS.items(): + if letter in sset: + break + else: + if letter == 'k': + sset = SYMBOLS['customary'] + letter = letter.upper() + else: + raise ValueError("can't interpret %r" % init) + prefix = {sset[0]: 1} + for i, s in enumerate(sset[1:]): + prefix[s] = 1 << (i+1)*10 + return int(num * prefix[letter]) + + @unittest.skipUnless(OSX, "not an OSX system") class OSXSpecificTestCase(unittest.TestCase): @@ -111,9 +139,9 @@ class OSXSpecificTestCase(unittest.TestCase): sysctl_hwphymem = sysctl('sysctl hw.memsize') self.assertEqual(sysctl_hwphymem, psutil.virtual_memory().total) + unittest.skipIf(TRAVIS, "") @retry_before_failing() def test_vmem_free(self): - print(os.system("vm_stat")) num = vm_stat("free") self.assertAlmostEqual(psutil.virtual_memory().free, num, delta=MEMORY_TOLERANCE) @@ -147,19 +175,13 @@ class OSXSpecificTestCase(unittest.TestCase): self.assertEqual(psutil.swap_memory().sout, num) def test_swapmem_total(self): - psutil_total = psutil.swap_memory().total - sys_total = 0 - # OSX uses multiple cache files: - # http://en.wikipedia.org/wiki/Paging#OS_X - for name in os.listdir("/var/vm/"): - # In here we also have 'sleepimage' file which is the ram - # during hybernation: - # http://apple.stackexchange.com/a/50272 - if name.startswith("swapfile"): - file = os.path.join("/var/vm", name) - if os.path.isfile(file): - sys_total += os.path.getsize(file) - self.assertEqual(psutil_total, sys_total) + out = sh('sysctl vm.swapusage') + out = out.replace('vm.swapusage: ', '') + total, used, free = re.findall('\d+.\d+\w', out) + psutil_smem = psutil.swap_memory() + self.assertEqual(psutil_smem.total, human2bytes(total)) + self.assertEqual(psutil_smem.used, human2bytes(used)) + self.assertEqual(psutil_smem.free, human2bytes(free)) def main(): |