summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-10-01 22:11:38 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-10-01 22:11:38 +0200
commit405ec083d7cbb057f5eb45f026af7939e397c8ee (patch)
treee82bf7a834c35121ee74798723bd25801869bb6f
parent09fa49d1a270c6d8511946952a17dc454cd87f95 (diff)
downloadpsutil-405ec083d7cbb057f5eb45f026af7939e397c8ee.tar.gz
write more tests
-rw-r--r--psutil/tests/__init__.py15
-rw-r--r--psutil/tests/test_misc.py56
2 files changed, 52 insertions, 19 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index be939555..88590a3f 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -411,20 +411,21 @@ class retry(object):
return wrapper
-@retry(exception=psutil.NoSuchProcess, logfun=None, interval=0.001)
-def wait_for_pid(pid, timeout=GLOBAL_TIMEOUT):
+@retry(exception=psutil.NoSuchProcess, logfun=None, timeout=GLOBAL_TIMEOUT,
+ interval=0.001)
+def wait_for_pid(pid):
"""Wait for pid to show up in the process list then return.
Used in the test suite to give time the sub process to initialize.
"""
psutil.Process(pid)
- # give it some more time to allow better initialization
- time.sleep(0.01)
+ if WINDOWS:
+ # give it some more time to allow better initialization
+ time.sleep(0.01)
@retry(exception=(EnvironmentError, AssertionError), logfun=None,
- interval=0.001)
-def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, delete_file=True,
- empty=False):
+ timeout=GLOBAL_TIMEOUT, interval=0.001)
+def wait_for_file(fname, delete_file=True, empty=False):
"""Wait for a file to be written on disk with some content."""
with open(fname, "rb") as f:
data = f.read()
diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py
index 47908d7e..00f26695 100644
--- a/psutil/tests/test_misc.py
+++ b/psutil/tests/test_misc.py
@@ -23,16 +23,20 @@ from psutil import POSIX
from psutil import WINDOWS
from psutil._common import supports_ipv6
from psutil.tests import APPVEYOR
-from psutil.tests import SCRIPTS_DIR
from psutil.tests import importlib
from psutil.tests import mock
from psutil.tests import retry
from psutil.tests import ROOT_DIR
from psutil.tests import run_test_module_by_name
+from psutil.tests import safe_remove
+from psutil.tests import SCRIPTS_DIR
from psutil.tests import sh
+from psutil.tests import TESTFN
from psutil.tests import TOX
from psutil.tests import TRAVIS
from psutil.tests import unittest
+from psutil.tests import wait_for_file
+from psutil.tests import wait_for_pid
# ===================================================================
@@ -318,19 +322,13 @@ class TestMisc(unittest.TestCase):
psutil.Process()
assert meth.called
- def test_psutil_is_reloadable(self):
- importlib.reload(psutil)
-
def test_sanity_version_check(self):
# see: https://github.com/giampaolo/psutil/issues/564
- try:
- with mock.patch(
- "psutil._psplatform.cext.version", return_value="0.0.0"):
- with self.assertRaises(ImportError) as cm:
- importlib.reload(psutil)
- self.assertIn("version conflict", str(cm.exception).lower())
- finally:
- importlib.reload(psutil)
+ with mock.patch(
+ "psutil._psplatform.cext.version", return_value="0.0.0"):
+ with self.assertRaises(ImportError) as cm:
+ importlib.reload(psutil)
+ self.assertIn("version conflict", str(cm.exception).lower())
# ===================================================================
@@ -518,5 +516,39 @@ class TestRetryDecorator(unittest.TestCase):
self.assertRaises(ValueError, retry, retries=5, timeout=1)
+class TestSyncTestUtils(unittest.TestCase):
+
+ def tearDown(self):
+ safe_remove(TESTFN)
+
+ def test_wait_for_pid(self):
+ wait_for_pid(os.getpid())
+ nopid = max(psutil.pids()) + 99999
+ with mock.patch('psutil.tests.retry.__iter__', return_value=iter([0])):
+ self.assertRaises(psutil.NoSuchProcess, wait_for_pid, nopid)
+
+ def test_wait_for_file(self):
+ with open(TESTFN, 'w') as f:
+ f.write('foo')
+ wait_for_file(TESTFN)
+ assert not os.path.exists(TESTFN)
+
+ def test_wait_for_file_empty(self):
+ with open(TESTFN, 'w'):
+ pass
+ wait_for_file(TESTFN, empty=True)
+ assert not os.path.exists(TESTFN)
+
+ def test_wait_for_file_no_file(self):
+ with mock.patch('psutil.tests.retry.__iter__', return_value=iter([0])):
+ self.assertRaises(IOError, wait_for_file, TESTFN)
+
+ def test_wait_for_file_no_delete(self):
+ with open(TESTFN, 'w') as f:
+ f.write('foo')
+ wait_for_file(TESTFN, delete_file=False)
+ assert os.path.exists(TESTFN)
+
+
if __name__ == '__main__':
run_test_module_by_name(__file__)