summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-09-23 04:13:24 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-09-23 04:13:24 +0200
commiteec3c620a9e9fc3d2ba551cf1379d61aebcdfa48 (patch)
tree2251480be08b5a0b9ab5c968be89ef9f5aea3560
parent44d37aec3dff113960af5c83ded3f271e9ee57f3 (diff)
downloadpsutil-eec3c620a9e9fc3d2ba551cf1379d61aebcdfa48.tar.gz
better synchronize get_test_subprocess() so that it has more time to initialize properly
-rw-r--r--IDEAS5
-rw-r--r--psutil/tests/__init__.py58
-rw-r--r--psutil/tests/test_process.py24
-rw-r--r--psutil/tests/test_system.py2
4 files changed, 42 insertions, 47 deletions
diff --git a/IDEAS b/IDEAS
index 9eb7d762..143b686e 100644
--- a/IDEAS
+++ b/IDEAS
@@ -17,10 +17,7 @@ PLATFORMS
FEATURES
========
-- (Linux): from /proc/pid/stat we can also retrieve process and children guest
- times (time spent running a virtual CPU for a guest OS).
-
-- #809: (Linux) per-process resource limits.
+- #809: (BSD) per-process resource limits (rlimit()).
- (UNIX) process root (different from cwd)
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index edecc9e3..28f796fd 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -186,39 +186,34 @@ class ThreadTask(threading.Thread):
_subprocesses_started = set()
-def get_test_subprocess(cmd=None, wait=False, **kwds):
+def get_test_subprocess(cmd=None, **kwds):
"""Return a subprocess.Popen object to use in tests.
By default stdout and stderr are redirected to /dev/null and the
python interpreter is used as test process.
- If 'wait' is True attemps to make sure the process is in a
- reasonably initialized state.
+ It also attemps to make sure the process is in a reasonably
+ initialized state.
"""
- if cmd is None:
- pyline = ""
- if wait:
- pyline += "open(r'%s', 'w'); " % TESTFN
- # A process living for 30 secs. We sleep N times (as opposed to
- # once) in order to be nicer towards Windows which doesn't handle
- # interrupt signals properly.
- pyline += "import time; [time.sleep(0.01) for x in range(3000)];"
- cmd_ = [PYTHON, "-c", pyline]
- else:
- cmd_ = cmd
kwds.setdefault("stdin", DEVNULL)
kwds.setdefault("stdout", DEVNULL)
kwds.setdefault("stderr", DEVNULL)
- sproc = subprocess.Popen(cmd_, **kwds)
- if wait:
- if cmd is None:
- stop_at = time.time() + 3
- while stop_at > time.time():
- if os.path.exists(TESTFN):
- break
+ if cmd is None:
+ pyline = "from time import sleep;"
+ pyline += "open(r'%s', 'w').close();" % TESTFN
+ pyline += "sleep(10)"
+ cmd = [PYTHON, "-c", pyline]
+ sproc = subprocess.Popen(cmd, **kwds)
+ stop_at = time.time() + 3
+ while stop_at > time.time():
+ if os.path.exists(TESTFN):
+ os.remove(TESTFN)
time.sleep(0.001)
- else:
- warn("couldn't make sure test file was actually created")
+ break
+ time.sleep(0.001)
else:
- wait_for_pid(sproc.pid)
+ warn("couldn't make sure test file was actually created")
+ else:
+ sproc = subprocess.Popen(cmd, **kwds)
+ wait_for_pid(sproc.pid)
_subprocesses_started.add(sproc)
return sproc
@@ -372,17 +367,20 @@ def wait_for_pid(pid, timeout=GLOBAL_TIMEOUT):
"""
raise_at = time.time() + timeout
while True:
- if pid in psutil.pids():
- # give it one more iteration to allow full initialization
+ try:
+ psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ time.sleep(0.001)
+ if time.time() >= raise_at:
+ raise RuntimeError("Timed out")
+ else:
+ # give it some more time to allow better initialization
time.sleep(0.01)
return
- time.sleep(0.0001)
- if time.time() >= raise_at:
- raise RuntimeError("Timed out")
def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, delete_file=True):
- """Wait for a file to be written on disk."""
+ """Wait for a file to be written on disk with some content."""
stop_at = time.time() + timeout
while time.time() < stop_at:
try:
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 5ade9df2..f5a24da2 100644
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -102,7 +102,7 @@ class TestProcess(unittest.TestCase):
self.assertEqual(psutil.Process(sproc.pid).pid, sproc.pid)
def test_kill(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
test_pid = sproc.pid
p = psutil.Process(test_pid)
p.kill()
@@ -112,7 +112,7 @@ class TestProcess(unittest.TestCase):
self.assertEqual(sig, signal.SIGKILL)
def test_terminate(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
test_pid = sproc.pid
p = psutil.Process(test_pid)
p.terminate()
@@ -289,7 +289,7 @@ class TestProcess(unittest.TestCase):
self.fail("expected: %s, found: %s" % (ktime, kernel_time))
def test_create_time(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
now = time.time()
p = psutil.Process(sproc.pid)
create_time = p.create_time()
@@ -557,7 +557,7 @@ class TestProcess(unittest.TestCase):
# see: https://travis-ci.org/giampaolo/psutil/jobs/111842553
@unittest.skipIf(OSX and TRAVIS, "")
def test_threads_2(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
p = psutil.Process(sproc.pid)
if OPENBSD:
try:
@@ -667,7 +667,7 @@ class TestProcess(unittest.TestCase):
assert 0 <= ret <= 100, ret
def test_is_running(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
p = psutil.Process(sproc.pid)
assert p.is_running()
assert p.is_running()
@@ -677,7 +677,7 @@ class TestProcess(unittest.TestCase):
assert not p.is_running()
def test_exe(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
exe = psutil.Process(sproc.pid).exe()
try:
self.assertEqual(exe, PYTHON)
@@ -698,7 +698,7 @@ class TestProcess(unittest.TestCase):
def test_cmdline(self):
cmdline = [PYTHON, "-c", "import time; time.sleep(60)"]
- sproc = get_test_subprocess(cmdline, wait=True)
+ sproc = get_test_subprocess(cmdline)
try:
self.assertEqual(' '.join(psutil.Process(sproc.pid).cmdline()),
' '.join(cmdline))
@@ -714,7 +714,7 @@ class TestProcess(unittest.TestCase):
raise
def test_name(self):
- sproc = get_test_subprocess(PYTHON, wait=True)
+ sproc = get_test_subprocess(PYTHON)
name = psutil.Process(sproc.pid).name().lower()
pyexe = os.path.basename(os.path.realpath(sys.executable)).lower()
assert pyexe.startswith(name), (pyexe, name)
@@ -825,13 +825,13 @@ class TestProcess(unittest.TestCase):
p.username()
def test_cwd(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
p = psutil.Process(sproc.pid)
self.assertEqual(p.cwd(), os.getcwd())
def test_cwd_2(self):
cmd = [PYTHON, "-c", "import os, time; os.chdir('..'); time.sleep(60)"]
- sproc = get_test_subprocess(cmd, wait=True)
+ sproc = get_test_subprocess(cmd)
p = psutil.Process(sproc.pid)
call_until(p.cwd, "ret == os.path.dirname(os.getcwd())")
@@ -898,7 +898,7 @@ class TestProcess(unittest.TestCase):
# another process
cmdline = "import time; f = open(r'%s', 'r'); time.sleep(60);" % TESTFN
- sproc = get_test_subprocess([PYTHON, "-c", cmdline], wait=True)
+ sproc = get_test_subprocess([PYTHON, "-c", cmdline])
p = psutil.Process(sproc.pid)
for x in range(100):
@@ -1194,7 +1194,7 @@ class TestProcess(unittest.TestCase):
self.assertEqual(len(c), len(set(c)))
def test_suspend_resume(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
p = psutil.Process(sproc.pid)
p.suspend()
for x in range(100):
diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py
index 4a48a52b..53f10fd4 100644
--- a/psutil/tests/test_system.py
+++ b/psutil/tests/test_system.py
@@ -189,7 +189,7 @@ class TestSystemAPIs(unittest.TestCase):
assert mem.sout >= 0, mem
def test_pid_exists(self):
- sproc = get_test_subprocess(wait=True)
+ sproc = get_test_subprocess()
self.assertTrue(psutil.pid_exists(sproc.pid))
p = psutil.Process(sproc.pid)
p.kill()