summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-09-30 03:36:57 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-09-30 03:36:57 +0200
commitfe02332040d61951951945300c754aaaa7359026 (patch)
treec7b213fda5f80e6676ae916f6d56435123a046a0
parented4fdb5826c7de868a28ee537e116c41ece32b41 (diff)
downloadpsutil-fe02332040d61951951945300c754aaaa7359026.tar.gz
add tests for the test utils (lol)
-rw-r--r--psutil/tests/__init__.py12
-rw-r--r--psutil/tests/test_testutils.py62
2 files changed, 69 insertions, 5 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index fabe7855..be939555 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -361,12 +361,13 @@ class retry(object):
def __init__(self,
exception=Exception,
- timeout=GLOBAL_TIMEOUT,
+ timeout=None,
retries=None,
interval=0.001,
logfun=lambda s: print(s, file=sys.stderr),
):
- assert not (timeout and retries), "mutually exclusive"
+ if timeout and retries:
+ raise ValueError("timeout and retries args are mutually exclusive")
self.exception = exception
self.timeout = timeout
self.retries = retries
@@ -410,7 +411,7 @@ class retry(object):
return wrapper
-@retry(exception=psutil.NoSuchProcess, logfun=None)
+@retry(exception=psutil.NoSuchProcess, logfun=None, interval=0.001)
def wait_for_pid(pid, timeout=GLOBAL_TIMEOUT):
"""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.
@@ -420,7 +421,8 @@ def wait_for_pid(pid, timeout=GLOBAL_TIMEOUT):
time.sleep(0.01)
-@retry(exception=(EnvironmentError, AssertionError), logfun=None)
+@retry(exception=(EnvironmentError, AssertionError), logfun=None,
+ interval=0.001)
def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, delete_file=True,
empty=False):
"""Wait for a file to be written on disk with some content."""
@@ -433,7 +435,7 @@ def wait_for_file(fname, timeout=GLOBAL_TIMEOUT, delete_file=True,
return data
-@retry(exception=AssertionError, logfun=None)
+@retry(exception=AssertionError, logfun=None, interval=0.001)
def call_until(fun, expr, timeout=GLOBAL_TIMEOUT):
"""Keep calling function for timeout secs and exit if eval()
expression is True.
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
new file mode 100644
index 00000000..eccb7226
--- /dev/null
+++ b/psutil/tests/test_testutils.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Unit tests for the test utilities (oh boy!).
+"""
+
+from psutil.tests import unittest
+from psutil.tests import retry
+from psutil.tests import mock
+from psutil.tests import run_test_module_by_name
+
+
+class TestRetryDecorator(unittest.TestCase):
+
+ @mock.patch('time.sleep')
+ def test_retry_success(self, sleep):
+ # Fail 3 times out of 5; make sure the decorated fun returns.
+
+ @retry(retries=5, logfun=None)
+ def foo():
+ while queue:
+ queue.pop()
+ 1 / 0
+ return 1
+
+ queue = list(range(3))
+ self.assertEqual(foo(), 1)
+
+ @mock.patch('time.sleep')
+ def test_retry_failure(self, sleep):
+ # Fail 6 times out of 5; th function is supposed to raise exc.
+
+ @retry(retries=5, logfun=None)
+ def foo():
+ while queue:
+ queue.pop()
+ 1 / 0
+ return 1
+
+ queue = list(range(6))
+ self.assertRaises(ZeroDivisionError, foo)
+
+ @mock.patch('time.sleep')
+ def test_exception(self, sleep):
+
+ @retry(exception=ValueError)
+ def foo():
+ raise TypeError
+
+ self.assertRaises(TypeError, foo)
+
+ @mock.patch('time.sleep')
+ def test_retries_and_timeout(self, sleep):
+ self.assertRaises(ValueError, retry, retries=5, timeout=1)
+
+
+if __name__ == '__main__':
+ run_test_module_by_name(__file__)