From 82c0876f1bec1e1b758bdcbbafb88f0c02cb3751 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 24 Oct 2016 21:12:37 +0200 Subject: [PATCH] fix Popen test which is occasionally failing --- psutil/tests/test_process.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 33f8450c3..6e4073d18 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -1435,14 +1435,31 @@ def test_pid_0(self): self.assertTrue(psutil.pid_exists(0)) def test_Popen(self): - with psutil.Popen([PYTHON, "-V"], stdout=subprocess.PIPE, - stderr=subprocess.PIPE) as proc: + # XXX this test causes a ResourceWarning on Python 3 because + # psutil.__subproc instance doesn't get propertly freed. + # Not sure what to do though. + cmd = [PYTHON, "-c", "import time; time.sleep(60);"] + proc = psutil.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + try: proc.name() proc.cpu_times() proc.stdin self.assertTrue(dir(proc)) self.assertRaises(AttributeError, getattr, proc, 'foo') - proc.wait() + finally: + proc.kill() + proc.wait() + + def test_Popen_ctx_manager(self): + with psutil.Popen([PYTHON, "-V"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE) as proc: + pass + assert proc.stdout.closed + assert proc.stderr.closed + assert proc.stdin.closed @unittest.skipUnless(hasattr(psutil.Process, "environ"), "platform not supported")