Ejemplo n.º 1
0
    def test_2(self):
        """basic resource loading test."""
        pool = ResourcePool(cache_size=None)
        pool.register_resource(ResourceA)
        pool.register_resource(ResourceB)

        # test that resource matches our request, and its data isn't loaded
        variables = dict(foo="hey", bah="ho")
        resource = pool.get_resource("resource.a", variables)
        self.assertTrue(isinstance(resource, ResourceA))
        self.assertEqual(resource.variables, variables)

        # test that a request via a resource's own handle gives the same resource
        resource_ = pool.get_resource_from_handle(resource.handle)
        self.assertTrue(resource_ is resource)

        # test that the same request again gives the cached resource
        resource_ = pool.get_resource("resource.a", variables)
        self.assertTrue(resource_ is resource)

        # clear caches, then test that the same request gives a new resource
        pool.clear_caches()
        resource_ = pool.get_resource("resource.a", variables)
        self.assertEqual(resource_.variables, variables)
        self.assertTrue(resource_ is not resource)
Ejemplo n.º 2
0
    def __init__(self, resource_pool=None):
        """Create a package repo manager.

        Args:
            resource_pool (`ResourcePool`): Provide your own resource pool. If
                None, a default pool is created based on config settings.
        """
        if resource_pool is None:
            cache_size = config.resource_caching_maxsize
            if cache_size < 0:  # -1 == disable caching
                cache_size = None

            resource_pool = ResourcePool(cache_size=cache_size)

        self.pool = resource_pool
        self.repositories = {}
Ejemplo n.º 3
0
    def test_1(self):
        """resource registration test."""
        pool = ResourcePool(cache_size=None)

        with self.assertRaises(ResourceError):
            pool.get_resource("resource.a")

        pool.register_resource(ResourceA)
        pool.register_resource(ResourceB)

        with self.assertRaises(ResourceError):
            pool.register_resource(ResourceBad)

        resource_a = pool.get_resource("resource.a")
        resource_b = pool.get_resource("resource.b")
        self.assertTrue(isinstance(resource_a, ResourceA))
        self.assertTrue(isinstance(resource_b, ResourceB))
Ejemplo n.º 4
0
    def create_repository(cls, repository_data):
        """Create a standalone, in-memory repository.

        Using this function bypasses the `package_repository_manager` singleton.
        This is usually desired however, since in-memory repositories are for
        temporarily storing programmatically created packages, which we do not
        want to cache and that do not persist.

        Args:
            repository_data (dict): Repository data, see class docstring.

        Returns:
            `MemoryPackageRepository` object.
        """
        location = "memory{%s}" % hex(id(repository_data))
        resource_pool = ResourcePool(cache_size=None)
        repo = MemoryPackageRepository(location, resource_pool)
        repo.data = repository_data
        return repo
Ejemplo n.º 5
0
 def __init__(self):
     cache_size = config.resource_caching_maxsize
     if cache_size < 0:
         cache_size = None
     self.cache_size = cache_size
     self.pool = ResourcePool(cache_size=cache_size)
Ejemplo n.º 6
0
 def __init__(self):
     self.pool = ResourcePool(cache_size=None)
     self.repo = PetRepository(self.pool)