def test_objects_removed_after_keepalive(self): _cache = interfaces.cache._Cache() key = 'the key' test_array = numpy.random.randn(16) obj = builders.fft(test_array) _cache.insert(obj, key) self.assertIs(_cache.lookup(key), obj) keepalive_time = _cache.keepalive_time time.sleep(_cache.keepalive_time*3) self.assertRaises(KeyError, _cache.lookup, key) _cache.insert(obj, key) old_keepalive_time = _cache.keepalive_time _cache.set_keepalive_time(old_keepalive_time * 4) self.assertIs(_cache.lookup(key), obj) time.sleep(old_keepalive_time * 3) self.assertIs(_cache.lookup(key), obj) time.sleep(old_keepalive_time * 8) self.assertRaises(KeyError, _cache.lookup, key)
def test_invalid_lookup(self): _cache = interfaces.cache._Cache() key = 'the key' test_array = numpy.random.randn(16) obj = builders.fft(test_array) _cache.insert(obj, key) self.assertRaises(KeyError, _cache.lookup, 'wrong_key')
def test_insert_and_lookup_item(self): _cache = interfaces.cache._Cache() key = 'the key' test_array = numpy.random.randn(16) obj = builders.fft(test_array) _cache.insert(obj, key) self.assertIs(_cache.lookup(key), obj)
def test_contains(self): _cache = interfaces.cache._Cache() key = 'the key' test_array = numpy.random.randn(16) obj = builders.fft(test_array) _cache.insert(obj, key) self.assertTrue(key in _cache) self.assertFalse('Not a key' in _cache)
def test_objects_removed_after_keepalive(self): _cache = interfaces.cache._Cache() key = 'the key' test_array = numpy.random.randn(16) obj = builders.fft(test_array) _cache.insert(obj, key) self.assertIs(_cache.lookup(key), obj) keepalive_time = _cache.keepalive_time if os.name == 'nt': # A hack to keep appveyor from falling over here. I suspect the # contention is too much to work properly. Either way, let's # assume it's a windows problem for now... time.sleep(keepalive_time * 8) else: # Relax a bit more otherwise time.sleep(keepalive_time * 4) self.assertRaises(KeyError, _cache.lookup, key) _cache.insert(obj, key) old_keepalive_time = _cache.keepalive_time _cache.set_keepalive_time(old_keepalive_time * 4) self.assertIs(_cache.lookup(key), obj) time.sleep(old_keepalive_time * 3.5) # still should be there self.assertIs(_cache.lookup(key), obj) if os.name == 'nt': # As above, but with a bit longer time.sleep(old_keepalive_time * 16) else: time.sleep(old_keepalive_time * 8) self.assertRaises(KeyError, _cache.lookup, key)