Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Oct 21, 2022
1 parent b5551e3 commit 000ec1f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ I: 2150

N: Daniel Widdis
W: https://github.com/dbwiddis
I: 2077
I: 2077, 2160

N: Amir Rossert
W: https://github.com/arossert
Expand Down
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ XXXX-XX-XX
``SPEED_UNKNOWN`` definition. (patch by Amir Rossert)
- 2010_, [macOS]: on MacOS, arm64 ``IFM_1000_TX`` and ``IFM_1000_T`` are the
same value, causing a build failure. (patch by Lawrence D'Anna)
- 2160_, [Windows]: Handle mismatch between committed vs. used physical memory.
(patch by Daniel Widdis)

5.9.3
=====
Expand Down
2 changes: 1 addition & 1 deletion psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def swap_memory():
# pages are accessed, so free_phys is an overestimate of the portion
# free_system contributed to by physical memory, and in some edge cases
# can exceed free system memory.
free = max(0, free_system - free_phys)
free = max(0, min(total, free_system - free_phys))
used = total - free
percent = usage_percent(used, total, round_=1)
return _common.sswap(total, used, free, percent, 0, 0)
Expand Down
25 changes: 25 additions & 0 deletions psutil/tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ def test_total_phymem(self):
self.assertEqual(int(w.TotalPhysicalMemory),
psutil.virtual_memory().total)

def test_total_swapmem(self):
if (psutil.swap_memory().total > 0):
w = wmi.WMI().Win32_PerfRawData_PerfOS_Memory()[0]
self.assertEqual(int(w.CommitLimit)
- psutil.virtual_memory().total,
psutil.swap_memory().total)
else:
self.assertEqual(0, psutil.swap_memory().total)
self.assertEqual(0, psutil.swap_memory().free)
self.assertEqual(0, psutil.swap_memory().used)

def test_percent_swapmem(self):
if (psutil.swap_memory().total > 0):
w = wmi.WMI().Win32_PerfRawData_PerfOS_PagingFile()[0]
# calculate swap usage to integer percent
percentSwap = int(w.PercentUsage) * 100 / int(w.PercentUsage_Base)
# exact percent may change but should be reasonable
# assert within +/- 10% and between 0 and 100
self.assertGreaterEqual(psutil.swap_memory().percent, 0)
self.assertGreaterEqual(psutil.swap_memory().percent,
percentSwap - 10)
self.assertLessEqual(psutil.swap_memory().percent,
percentSwap + 10)
self.assertLessEqual(psutil.swap_memory().percent, 100)

# @unittest.skipIf(wmi is None, "wmi module is not installed")
# def test__UPTIME(self):
# # _UPTIME constant is not public but it is used internally
Expand Down

0 comments on commit 000ec1f

Please sign in to comment.