diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-11-05 01:49:03 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-11-05 01:49:03 +0100 |
commit | cd8d3d728e218e1fb56fbafc3d7902e146e969fd (patch) | |
tree | ddd55b23a53701f3943d3c66b25a4410f22665c7 | |
parent | cc81692688d8502d9fc455effe9e00a6ea495d0a (diff) | |
download | psutil-cd8d3d728e218e1fb56fbafc3d7902e146e969fd.tar.gz |
add simple test case for oneshot() ctx manager
-rwxr-xr-x | psutil/tests/test_process.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index d2b700e5..52651b44 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -1249,6 +1249,26 @@ class TestProcess(unittest.TestCase): p.cpu_times() self.assertEqual(m.call_count, 2) + def test_oneshot_twice(self): + # Test the case where the ctx manager is __enter__ed twice. + # The second __enter__ is supposed to resut in a NOOP. + with mock.patch("psutil._psplatform.Process.cpu_times") as m1: + with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2: + p = psutil.Process() + with p.oneshot(): + p.cpu_times() + p.cpu_times() + with p.oneshot(): + p.cpu_times() + p.cpu_times() + self.assertEqual(m1.call_count, 1) + self.assertEqual(m2.call_count, 1) + + with mock.patch("psutil._psplatform.Process.cpu_times") as m: + p.cpu_times() + p.cpu_times() + self.assertEqual(m.call_count, 2) + def test_halfway_terminated_process(self): # Test that NoSuchProcess exception gets raised in case the # process dies after we create the Process object. |