summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-17 14:17:07 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-17 14:17:07 +0200
commit7399070ccf25e8e6d92b50b22c6635b5fcdcb2e2 (patch)
tree0064316a441ba48fad2eea70c799883c452c4bb6
parent3a8fc3f5b8cf0589dccf519ace25f70f88b914d0 (diff)
parentb4b3e59f0f95ebf7f138763ce259c1d7ea9ffb5f (diff)
downloadpsutil-7399070ccf25e8e6d92b50b22c6635b5fcdcb2e2.tar.gz
Merge branch 'master' of https://github.com/giampaolo/psutil
-rw-r--r--psutil/tests/__init__.py97
-rwxr-xr-xpsutil/tests/test_connections.py2
-rwxr-xr-xpsutil/tests/test_unicode.py25
3 files changed, 82 insertions, 42 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 4aae1980..1206abff 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -271,6 +271,8 @@ def get_test_subprocess(cmd=None, **kwds):
"""
kwds.setdefault("stdin", DEVNULL)
kwds.setdefault("stdout", DEVNULL)
+ kwds.setdefault("cwd", os.getcwd())
+ kwds.setdefault("env", os.environ)
if WINDOWS:
# Prevents the subprocess to open error dialogs.
kwds.setdefault("creationflags", 0x8000000) # CREATE_NO_WINDOW
@@ -357,6 +359,7 @@ def sh(cmd, **kwds):
kwds.setdefault("universal_newlines", True)
kwds.setdefault("creationflags", flags)
p = subprocess.Popen(cmd, **kwds)
+ _subprocesses_started.add(p)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError(stderr)
@@ -375,6 +378,20 @@ def reap_children(recursive=False):
If resursive is True it also tries to terminate and wait()
all grandchildren started by this process.
"""
+ # This is here to make sure wait_procs() behaves properly and
+ # investigate:
+ # https://ci.appveyor.com/project/giampaolo/psutil/build/job/
+ # jiq2cgd6stsbtn60
+ def assert_gone(pid):
+ assert not psutil.pid_exists(pid), pid
+ assert pid not in psutil.pids(), pid
+ try:
+ psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ pass
+ else:
+ assert 0, "pid %s is not gone" % pid
+
# Get the children here, before terminating the children sub
# processes as we don't want to lose the intermediate reference
# in case of grandchildren.
@@ -407,6 +424,7 @@ def reap_children(recursive=False):
except OSError as err:
if err.errno != errno.ECHILD:
raise
+ assert_gone(subp.pid)
# Terminate started pids.
while _pids_started:
@@ -437,53 +455,52 @@ def reap_children(recursive=False):
for p in alive:
warn("process %r survived kill()" % p)
+ for p in children:
+ assert_gone(p.pid)
+
# ===================================================================
# --- OS
# ===================================================================
-if not POSIX:
- def get_kernel_version():
- return ()
-else:
- def get_kernel_version():
- """Return a tuple such as (2, 6, 36)."""
- s = ""
- uname = os.uname()[2]
- for c in uname:
- if c.isdigit() or c == '.':
- s += c
- else:
- break
- if not s:
- raise ValueError("can't parse %r" % uname)
- minor = 0
- micro = 0
- nums = s.split('.')
- major = int(nums[0])
- if len(nums) >= 2:
- minor = int(nums[1])
- if len(nums) >= 3:
- micro = int(nums[2])
- return (major, minor, micro)
-
-
-if not WINDOWS:
- def get_winver():
- raise NotImplementedError("not a Windows OS")
-else:
- def get_winver():
- wv = sys.getwindowsversion()
- if hasattr(wv, 'service_pack_major'): # python >= 2.7
- sp = wv.service_pack_major or 0
+def get_kernel_version():
+ """Return a tuple such as (2, 6, 36)."""
+ if not POSIX:
+ raise NotImplementedError("not POSIX")
+ s = ""
+ uname = os.uname()[2]
+ for c in uname:
+ if c.isdigit() or c == '.':
+ s += c
else:
- r = re.search(r"\s\d$", wv[4])
- if r:
- sp = int(r.group(0))
- else:
- sp = 0
- return (wv[0], wv[1], sp)
+ break
+ if not s:
+ raise ValueError("can't parse %r" % uname)
+ minor = 0
+ micro = 0
+ nums = s.split('.')
+ major = int(nums[0])
+ if len(nums) >= 2:
+ minor = int(nums[1])
+ if len(nums) >= 3:
+ micro = int(nums[2])
+ return (major, minor, micro)
+
+
+def get_winver():
+ if not WINDOWS:
+ raise NotImplementedError("not WINDOWS")
+ wv = sys.getwindowsversion()
+ if hasattr(wv, 'service_pack_major'): # python >= 2.7
+ sp = wv.service_pack_major or 0
+ else:
+ r = re.search(r"\s\d$", wv[4])
+ if r:
+ sp = int(r.group(0))
+ else:
+ sp = 0
+ return (wv[0], wv[1], sp)
# ===================================================================
diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py
index f7dd2ec2..c4d896ee 100755
--- a/psutil/tests/test_connections.py
+++ b/psutil/tests/test_connections.py
@@ -465,7 +465,7 @@ class TestSystemWideConnections(unittest.TestCase):
pids = []
times = 10
for i in range(times):
- fname = TESTFN + str(i)
+ fname = os.path.realpath(TESTFN) + str(i)
src = textwrap.dedent("""\
import time, os
from psutil.tests import create_sockets
diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py
index 4c2181d4..7c87a3f2 100755
--- a/psutil/tests/test_unicode.py
+++ b/psutil/tests/test_unicode.py
@@ -65,6 +65,7 @@ from psutil import POSIX
from psutil import WINDOWS
from psutil._compat import PY3
from psutil._compat import u
+from psutil.tests import APPVEYOR
from psutil.tests import ASCII_FS
from psutil.tests import bind_unix_socket
from psutil.tests import chdir
@@ -77,7 +78,7 @@ from psutil.tests import mock
from psutil.tests import reap_children
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_mkdir
-from psutil.tests import safe_rmpath
+from psutil.tests import safe_rmpath as _safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import TESTFILE_PREFIX
from psutil.tests import TESTFN
@@ -89,6 +90,28 @@ import psutil
import psutil.tests
+def safe_rmpath(path):
+ # XXX
+ return _safe_rmpath(path)
+ if APPVEYOR:
+ # TODO - this is quite random and I'm not sure why it happens,
+ # nor I can reproduce it locally:
+ # https://ci.appveyor.com/project/giampaolo/psutil/build/job/
+ # jiq2cgd6stsbtn60
+ # safe_rmpath() happens after reap_children() so this is weird
+ # Perhaps wait_procs() on Windows is broken? Maybe because
+ # of STILL_ACTIVE?
+ # https://github.com/giampaolo/psutil/blob/
+ # 68c7a70728a31d8b8b58f4be6c4c0baa2f449eda/psutil/arch/
+ # windows/process_info.c#L146
+ try:
+ return _safe_rmpath(path)
+ except WindowsError:
+ traceback.print_exc()
+ else:
+ return _safe_rmpath(path)
+
+
def subprocess_supports_unicode(name):
"""Return True if both the fs and the subprocess module can
deal with a unicode file name.