summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-09 04:10:46 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-09 04:10:46 +0200
commit956c1cdc627e9e9f35b56eac13f3ce0c63f953da (patch)
treed8ab118d1dfe5ca517ec1e7cf5cf486a966a8d53
parent247d3a446955ddbd35ffb3db66896d6013894cb3 (diff)
downloadpsutil-956c1cdc627e9e9f35b56eac13f3ce0c63f953da.tar.gz
#802 add tests
-rw-r--r--psutil/_common.py4
-rwxr-xr-xpsutil/tests/test_misc.py74
2 files changed, 76 insertions, 2 deletions
diff --git a/psutil/_common.py b/psutil/_common.py
index 5e11b952..3bca0799 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -483,7 +483,7 @@ class _WrapNumbers:
assert name not in self.reminder_keys
self.cache[name] = input_dict
self.reminders[name] = defaultdict(int)
- self.reminder_keys[name] = defaultdict(list)
+ self.reminder_keys[name] = defaultdict(set)
def _remove_dead_reminders(self, input_dict, name):
"""In case the number of keys changed between calls (e.g. a
@@ -523,7 +523,7 @@ class _WrapNumbers:
remkey = (key, i)
if input_value < old_value:
self.reminders[name][remkey] += old_value
- self.reminder_keys[name][key].append(remkey)
+ self.reminder_keys[name][key].add(remkey)
bits.append(input_value + self.reminders[name][remkey])
new_dict[key] = input_nt._make(bits)
diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py
index 7f22ee54..d59bd159 100755
--- a/psutil/tests/test_misc.py
+++ b/psutil/tests/test_misc.py
@@ -428,6 +428,10 @@ class TestWrapNumbers(unittest.TestCase):
input = {'disk1': nt(100, 100, 20)}
self.assertEqual(wrap_numbers(input, 'disk_io'),
{'disk1': nt(100, 100, 210)})
+ # and remains the same
+ input = {'disk1': nt(100, 100, 20)}
+ self.assertEqual(wrap_numbers(input, 'disk_io'),
+ {'disk1': nt(100, 100, 210)})
# now wrap another num
input = {'disk1': nt(50, 100, 20)}
self.assertEqual(wrap_numbers(input, 'disk_io'),
@@ -483,6 +487,76 @@ class TestWrapNumbers(unittest.TestCase):
{'disk1': nt(50, 50, 50),
'disk2': nt(100, 100, 110)})
+ # --- cache tests
+
+ def test_cache_first_call(self):
+ input = {'disk1': nt(5, 5, 5)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ self.assertEqual(cache[1], {'disk_io': {}})
+ self.assertEqual(cache[2], {'disk_io': {}})
+
+ def test_cache_call_twice(self):
+ input = {'disk1': nt(5, 5, 5)}
+ wrap_numbers(input, 'disk_io')
+ input = {'disk1': nt(10, 10, 10)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ self.assertEqual(
+ cache[1],
+ {'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 0}})
+ self.assertEqual(cache[2], {'disk_io': {}})
+
+ def test_cache_wrap(self):
+ # let's say 100 is the threshold
+ input = {'disk1': nt(100, 100, 100)}
+ wrap_numbers(input, 'disk_io')
+
+ # first wrap restarts from 10
+ input = {'disk1': nt(100, 100, 10)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ self.assertEqual(
+ cache[1],
+ {'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 100}})
+ self.assertEqual(cache[2], {'disk_io': {'disk1': set([('disk1', 2)])}})
+
+ def assert_():
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(
+ cache[1],
+ {'disk_io': {('disk1', 0): 0, ('disk1', 1): 0,
+ ('disk1', 2): 100}})
+ self.assertEqual(cache[2],
+ {'disk_io': {'disk1': set([('disk1', 2)])}})
+
+ # then it remains the same
+ input = {'disk1': nt(100, 100, 10)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ assert_()
+
+ # then it goes up
+ input = {'disk1': nt(100, 100, 90)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ assert_()
+
+ # then it wraps again
+ input = {'disk1': nt(100, 100, 20)}
+ wrap_numbers(input, 'disk_io')
+ cache = wrap_numbers.cache_info()
+ self.assertEqual(cache[0], {'disk_io': input})
+ self.assertEqual(
+ cache[1],
+ {'disk_io': {('disk1', 0): 0, ('disk1', 1): 0, ('disk1', 2): 190}})
+ self.assertEqual(cache[2], {'disk_io': {'disk1': set([('disk1', 2)])}})
+
# ===================================================================
# --- Example script tests