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_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 test_increase_but_no_wrap(self): input = {'disk1': nt(5, 5, 5)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) input = {'disk1': nt(10, 15, 20)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) input = {'disk1': nt(20, 25, 30)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) input = {'disk1': nt(20, 25, 30)} self.assertEqual(wrap_numbers(input, 'disk_io'), input)
def test_changing_keys(self): # Emulate a case where the second call to disk_io() # (or whatever) provides a new disk, then the new disk # disappears on the third call. input = {'disk1': nt(5, 5, 5)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) input = {'disk1': nt(5, 5, 5), 'disk2': nt(7, 7, 7)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) input = {'disk1': nt(8, 8, 8)} self.assertEqual(wrap_numbers(input, 'disk_io'), input)
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': {}})
def test_real_data(self): d = {'nvme0n1': (300, 508, 640, 1571, 5970, 1987, 2049, 451751, 47048), 'nvme0n1p1': (1171, 2, 5600256, 1024, 516, 0, 0, 0, 8), 'nvme0n1p2': (54, 54, 2396160, 5165056, 4, 24, 30, 1207, 28), 'nvme0n1p3': (2389, 4539, 5154, 150, 4828, 1844, 2019, 398, 348)} self.assertEqual(wrap_numbers(d, 'disk_io'), d) self.assertEqual(wrap_numbers(d, 'disk_io'), d) # decrease this ↓ d = {'nvme0n1': (100, 508, 640, 1571, 5970, 1987, 2049, 451751, 47048), 'nvme0n1p1': (1171, 2, 5600256, 1024, 516, 0, 0, 0, 8), 'nvme0n1p2': (54, 54, 2396160, 5165056, 4, 24, 30, 1207, 28), 'nvme0n1p3': (2389, 4539, 5154, 150, 4828, 1844, 2019, 398, 348)} out = wrap_numbers(d, 'disk_io') self.assertEqual(out['nvme0n1'][0], 400)
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_changing_keys_w_wrap(self): input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) # disk 2 wraps input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 10)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 110)}) # disk 2 disappears input = {'disk1': nt(50, 50, 50)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) # then it appears again; the old wrap is supposed to be # gone. input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) # remains the same input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 100)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) # and then wraps again input = {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 10)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(50, 50, 50), 'disk2': nt(100, 100, 110)})
def test_wrap(self): # let's say 100 is the threshold input = {'disk1': nt(100, 100, 100)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) # first wrap restarts from 10 input = {'disk1': nt(100, 100, 10)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(100, 100, 110)}) # then it remains the same input = {'disk1': nt(100, 100, 10)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(100, 100, 110)}) # then it goes up input = {'disk1': nt(100, 100, 90)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(100, 100, 190)}) # then it wraps again 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'), {'disk1': nt(150, 100, 210)}) # and again input = {'disk1': nt(40, 100, 20)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(190, 100, 210)}) # keep it the same input = {'disk1': nt(40, 100, 20)} self.assertEqual(wrap_numbers(input, 'disk_io'), {'disk1': nt(190, 100, 210)})
def test_first_call(self): input = {'disk1': nt(5, 5, 5)} self.assertEqual(wrap_numbers(input, 'disk_io'), input)
def test_input_hasnt_changed(self): input = {'disk1': nt(5, 5, 5)} self.assertEqual(wrap_numbers(input, 'disk_io'), input) self.assertEqual(wrap_numbers(input, 'disk_io'), input)