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)])}})
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_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)])}})
def test_cache_clear_public_apis(self): psutil.disk_io_counters() psutil.net_io_counters() caches = wrap_numbers.cache_info() for cache in caches: self.assertIn('psutil.disk_io_counters', cache) self.assertIn('psutil.net_io_counters', cache) psutil.disk_io_counters.cache_clear() caches = wrap_numbers.cache_info() for cache in caches: self.assertIn('psutil.net_io_counters', cache) self.assertNotIn('psutil.disk_io_counters', cache) psutil.net_io_counters.cache_clear() caches = wrap_numbers.cache_info() self.assertEqual(caches, ({}, {}, {}))
def test_cache_clear(self): input = {'disk1': nt(5, 5, 5)} wrap_numbers(input, 'disk_io') wrap_numbers(input, 'disk_io') wrap_numbers.cache_clear('disk_io') self.assertEqual(wrap_numbers.cache_info(), ({}, {}, {})) wrap_numbers.cache_clear('disk_io') wrap_numbers.cache_clear('?!?')
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)])}})
def test_cache_clear_public_apis(self): if not psutil.disk_io_counters() or not psutil.net_io_counters(): return self.skipTest("no disks or NICs available") psutil.disk_io_counters() psutil.net_io_counters() caches = wrap_numbers.cache_info() for cache in caches: self.assertIn('psutil.disk_io_counters', cache) self.assertIn('psutil.net_io_counters', cache) psutil.disk_io_counters.cache_clear() caches = wrap_numbers.cache_info() for cache in caches: self.assertIn('psutil.net_io_counters', cache) self.assertNotIn('psutil.disk_io_counters', cache) psutil.net_io_counters.cache_clear() caches = wrap_numbers.cache_info() self.assertEqual(caches, ({}, {}, {}))
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_changing_keys(self): input = {'disk1': nt(5, 5, 5)} wrap_numbers(input, 'disk_io') input = {'disk1': nt(5, 5, 5), 'disk2': nt(7, 7, 7)} 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': {}})