def testGrabAndRelease(self): """Tests the GrabObject and ReleaseObject methods.""" cache_object = cache.ObjectsCache(1) self.assertIsNotNone(cache_object) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) _, cache_value = cache_object.GetCacheValueByObject(self._vfs_object) # pylint: disable=protected-access self.assertEqual(cache_value._reference_count, 0) cache_object.GrabObject(self._path_spec.comparable) _, cache_value = cache_object.GetCacheValueByObject(self._vfs_object) self.assertEqual(cache_value._reference_count, 1) cache_object.GrabObject(self._path_spec.comparable) _, cache_value = cache_object.GetCacheValueByObject(self._vfs_object) self.assertEqual(cache_value._reference_count, 2) cache_object.ReleaseObject(self._path_spec.comparable) _, cache_value = cache_object.GetCacheValueByObject(self._vfs_object) self.assertEqual(cache_value._reference_count, 1) cache_object.ReleaseObject(self._path_spec.comparable) _, cache_value = cache_object.GetCacheValueByObject(self._vfs_object) self.assertEqual(cache_value._reference_count, 0)
def __init__( self, maximum_number_of_file_objects=128, maximum_number_of_file_systems=16): """Initializes the resolver context object. Args: maximum_number_of_file_objects (Optional[int]): maximum number of file-like objects cached in the context. maximum_number_of_file_systems (Optional[int]): maximum number of file system objects cached in the context. """ super(Context, self).__init__() self._file_object_cache = cache.ObjectsCache( maximum_number_of_file_objects) self._file_system_cache = cache.ObjectsCache( maximum_number_of_file_systems)
def testGetObject(self): """Tests the GetObject method.""" cache_object = cache.ObjectsCache(1) self.assertIsNotNone(cache_object) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) cached_object = cache_object.GetObject(self._path_spec.comparable) self.assertEqual(cached_object, self._vfs_object)
def testGetCacheValueByObjectGetCacheValueByObject(self): """Tests the GetCacheValueByObject method.""" cache_object = cache.ObjectsCache(1) self.assertIsNotNone(cache_object) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) identifier, cache_value = cache_object.GetCacheValueByObject( self._vfs_object) self.assertEqual(identifier, self._path_spec.comparable) self.assertEqual(cache_value.vfs_object, self._vfs_object)
def testCacheFull(self): """Tests if the CacheFullError is raised.""" cache_object = cache.ObjectsCache(1) self.assertIsNotNone(cache_object) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) path_spec = fake_path_spec.FakePathSpec(location='2') vfs_object = TestVFSObject() with self.assertRaises(errors.CacheFullError): cache_object.CacheObject(path_spec.comparable, vfs_object)
def testCache(self): """Tests the cache functionality.""" cache_object = cache.ObjectsCache(1) self.assertIsNotNone(cache_object) # pylint: disable=protected-access self.assertEqual(len(cache_object._values), 0) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) self.assertEqual(len(cache_object._values), 1) with self.assertRaises(KeyError): cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) self.assertEqual(len(cache_object._values), 1) cache_object.RemoveObject(self._path_spec.comparable) self.assertEqual(len(cache_object._values), 0)
def testEmpty(self): """Tests the Empty method.""" cache_object = cache.ObjectsCache(5) self.assertIsNotNone(cache_object) cache_object.CacheObject(self._path_spec.comparable, self._vfs_object) path_spec = fake_path_spec.FakePathSpec(location='2') vfs_object = TestVFSObject() cache_object.CacheObject(path_spec.comparable, vfs_object) path_spec = fake_path_spec.FakePathSpec(location='3') vfs_object = TestVFSObject() cache_object.CacheObject(path_spec.comparable, vfs_object) # pylint: disable=protected-access self.assertEqual(len(cache_object._values), 3) cache_object.Empty() self.assertEqual(len(cache_object._values), 0)