Example #1
0
 def test_version(self):
     cache = AgentCache()
     value = "test too"
     version = 200
     cache.open_version(version)
     cache.cache_value("test", value, version=version)
     assert value == cache.find("test", version=version)
Example #2
0
    def __init__(self, process, name: str, uri: str):
        self.process = process
        self.name = name
        self._uri = uri

        # the lock for changing the current ongoing deployment
        self.critical_ratelimiter = locks.Semaphore(1)
        # lock for dryrun tasks
        self.dryrunlock = locks.Semaphore(1)

        # multi threading control
        self.thread_pool = ThreadPoolExecutor(process.poolsize)
        self.ratelimiter = locks.Semaphore(process.poolsize)

        self._env_id = process._env_id

        self.sessionid = process.sessionid

        # init
        self._cache = AgentCache()
        self._nq = ResourceScheduler(self,
                                     self.process.environment,
                                     name,
                                     self._cache,
                                     ratelimiter=self.ratelimiter)
        self._enabled = None

        # do regular deploys
        self._deploy_interval = cfg.agent_interval.get()
        self._splay_interval = cfg.agent_splay.get()
        self._splay_value = random.randint(0, self._splay_interval)

        self._getting_resources = False
        self._get_resource_timeout = 0
Example #3
0
 def test_resource(self):
     cache = AgentCache()
     value = "test too"
     resource = Id("test::Resource", "test", "key", "test",
                   100).get_instance()
     cache.cache_value("test", value, resource=resource)
     assert value == cache.find("test", resource=resource)
Example #4
0
 def test_version_closed(self):
     cache = AgentCache()
     value = "test too"
     version = 200
     with pytest.raises(Exception):
         cache.cache_value("test", value, version=version)
         assert value == cache.find("test", version=version)
Example #5
0
def test_resource_fail(my_resource):
    cache = AgentCache()
    value = "test too"
    resource = Id("test::Resource", "test", "key", "test", 100).get_instance()
    cache.cache_value("test", value, resource=resource)

    with pytest.raises(KeyError):
        assert value == cache.find("test")
Example #6
0
def test_resource_and_version():
    cache = AgentCache()
    value = "test too"
    resource = Id("test::Resource", "test", "key", "test", 100).get_instance()
    version = 200
    cache.open_version(version)
    cache.cache_value("test", value, resource=resource, version=version)
    assert value == cache.find("test", resource=resource, version=version)
Example #7
0
    def test_version_fail(self):
        cache = AgentCache()
        value = "test too"
        version = 200
        cache.open_version(version)
        cache.cache_value("test", value, version=version)

        with pytest.raises(KeyError):
            assert value == cache.find("test")
Example #8
0
    def test_decorator(self):

        xcache = AgentCache()

        class DT(object):
            def __init__(self, cache):
                self.cache = cache
                self.count = 0
                self.c2 = 0

            @cache
            def test_method(self):
                self.count += 1
                return "x"

            @cache
            def test_method_2(self, version):
                self.count += 1
                return "x2"

            @cache(cacheNone=False)
            def test_method_3(self):
                self.c2 += 1
                if self.c2 < 2:
                    return None
                else:
                    return "X"

        test = DT(xcache)
        assert "x" == test.test_method()
        assert "x" == test.test_method()
        assert "x" == test.test_method()
        assert 1 == test.count

        xcache.open_version(1)
        xcache.open_version(2)
        assert "x2" == test.test_method_2(version=1)
        assert "x2" == test.test_method_2(version=1)
        assert 2 == test.count
        assert "x2" == test.test_method_2(version=2)
        assert 3 == test.count
        xcache.close_version(1)
        xcache.open_version(1)
        assert "x2" == test.test_method_2(version=1)
        assert "x2" == test.test_method_2(version=1)
        assert 4 == test.count

        assert None is test.test_method_3()
        assert 1 == test.c2
        assert "X" == test.test_method_3()
        assert 2 == test.c2
        assert "X" == test.test_method_3()
        assert 2 == test.c2
Example #9
0
def test_multi_threaded():
    class Spy(object):
        def __init__(self):
            self.created = 0
            self.deleted = 0
            self.lock = Lock()

        def create(self):
            with self.lock:
                self.created += 1
            return self

        def delete(self):
            self.deleted += 1

    cache = AgentCache()
    version = 200

    cache.open_version(version)

    alpha = Spy()
    beta = Spy()
    alpha.lock.acquire()

    t1 = Thread(
        target=lambda: cache.get_or_else(
            "test", lambda version: alpha.create(), version=version, call_on_delete=lambda x: x.delete()
        )
    )
    t2 = Thread(
        target=lambda: cache.get_or_else(
            "test", lambda version: beta.create(), version=version, call_on_delete=lambda x: x.delete()
        )
    )

    t1.start()
    t2.start()

    alpha.lock.release()

    t1.join()
    t2.join()

    assert alpha.created + beta.created == 1
    assert alpha.deleted == 0
    assert beta.deleted == 0

    cache.close_version(version)

    assert alpha.created + beta.created == 1
    assert alpha.deleted == alpha.created
    assert beta.deleted == beta.created
Example #10
0
    def test_timout(self):
        cache = AgentCache()
        value = "test too"
        cache.cache_value("test", value, timeout=0.1)
        cache.cache_value("test2", value)

        assert value == cache.find("test")
        sleep(1)
        try:
            assert value == cache.find("test")
            raise AssertionError("Should get exception")
        except KeyError:
            pass

        assert value == cache.find("test2")
Example #11
0
def test_context_manager():
    cache = AgentCache()
    value = "test too"
    version = 200
    with cache.manager(version):
        cache.cache_value("test", value, version=version)
        cache.cache_value("test0", value, version=version)
        cache.cache_value("test4", value, version=version)
        resource = Id("test::Resource", "test", "key", "test", 100).get_instance()
        cache.cache_value("testx", value, resource=resource)
        assert value == cache.find("test", version=version)
        assert value == cache.find("testx", resource=resource)

    assert value, cache.find("testx", resource=resource)
    with pytest.raises(KeyError):
        assert value == cache.find("test", version=version)
Example #12
0
def test_version_close():
    cache = AgentCache()
    value = "test too"
    version = 200
    cache.open_version(version)
    cache.cache_value("test", value, version=version)
    cache.cache_value("test0", value, version=version)
    cache.cache_value("test4", value, version=version)
    resource = Id("test::Resource", "test", "key", "test", 100).get_instance()
    cache.cache_value("testx", value, resource=resource)
    assert value == cache.find("test", version=version)
    assert value == cache.find("testx", resource=resource)
    cache.close_version(version)
    assert value, cache.find("testx", resource=resource)
    with pytest.raises(KeyError):
        assert value == cache.find("test", version=version)
        raise AssertionError("Should get exception")
Example #13
0
    def test_version_and_timout(self):
        cache = AgentCache()
        version = 200

        cache.open_version(version)
        value = "test too"
        cache.cache_value("test", value, version=version, timeout=0.3)
        cache.cache_value("testx", value)

        assert value == cache.find("test", version=version)
        assert value == cache.find("testx")

        cache.close_version(version)
        assert value == cache.find("testx")

        sleep(1)
        assert value == cache.find("testx")

        with pytest.raises(KeyError):
            cache.find("test", version=version)
Example #14
0
def test_get_or_else_none():
    called = []

    def creator(param, resource, version):
        called.append("x")
        return param

    class Sequencer(object):
        def __init__(self, sequence):
            self.seq = sequence
            self.count = 0

        def __call__(self, **kwargs):
            out = self.seq[self.count]
            self.count += 1
            return out

    cache = AgentCache()
    value = "test too"
    resource = Id("test::Resource", "test", "key", "test", 100).get_instance()
    version = 100
    cache.open_version(version)
    assert None is cache.get_or_else("test", creator, resource=resource, version=version, cache_none=False, param=None)
    assert len(called) == 1
    assert None is cache.get_or_else("test", creator, resource=resource, version=version, cache_none=False, param=None)
    assert len(called) == 2
    assert value == cache.get_or_else("test", creator, resource=resource, version=version, cache_none=False, param=value)
    assert value == cache.get_or_else("test", creator, resource=resource, version=version, cache_none=False, param=value)
    assert len(called) == 3

    seq = Sequencer([None, None, "A"])
    assert None is cache.get_or_else("testx", seq, resource=resource, version=version, cache_none=False)
    assert seq.count == 1
    assert None is cache.get_or_else("testx", seq, resource=resource, version=version, cache_none=False)
    assert seq.count == 2
    assert "A" == cache.get_or_else("testx", seq, resource=resource, version=version, cache_none=False)
    assert seq.count == 3
    assert "A" == cache.get_or_else("testx", seq, resource=resource, version=version, cache_none=False)
    assert seq.count == 3
    assert "A" == cache.get_or_else("testx", seq, resource=resource, version=version, cache_none=False)
    assert seq.count == 3
Example #15
0
    def test_get_or_else(self):
        called = []

        def creator(param, resource, version):

            called.append("x")
            return param

        cache = AgentCache()
        value = "test too"
        value2 = "test too x"
        resource = Id("test::Resource", "test", "key", "test",
                      100).get_instance()
        resourcev2 = Id("test::Resource", "test", "key", "test",
                        200).get_instance()
        assert 200 == resourcev2.id.version
        version = 200
        cache.open_version(version)
        assert value == cache.get_or_else("test",
                                          creator,
                                          resource=resource,
                                          version=version,
                                          param=value)
        assert value == cache.get_or_else("test",
                                          creator,
                                          resource=resource,
                                          version=version,
                                          param=value)
        assert len(called) == 1
        assert value == cache.get_or_else("test",
                                          creator,
                                          resource=resourcev2,
                                          version=version,
                                          param=value)
        assert len(called) == 1
        assert value2 == cache.get_or_else("test",
                                           creator,
                                           resource=resource,
                                           version=version,
                                           param=value2)
Example #16
0
 def test_base_fail(self):
     cache = AgentCache()
     value = "test too"
     with pytest.raises(KeyError):
         assert value == cache.find("test")
Example #17
0
 def test_base(self):
     cache = AgentCache()
     value = "test too"
     cache.cache_value("test", value)
     assert value == cache.find("test")
Example #18
0
def test_decorator():
    class Closeable:
        def __init__(self):
            self.closed = False

        def close(self):
            self.closed = True

    my_closable = Closeable()
    my_closable_2 = Closeable()

    xcache = AgentCache()

    class DT(object):
        def __init__(self, cache):
            self.cache = cache
            self.count = 0
            self.c2 = 0

        @cache()
        def test_method(self):
            self.count += 1
            return "x"

        @cache
        def test_method_2(self, version):
            self.count += 1
            return "x2"

        @cache(cacheNone=False)
        def test_method_3(self):
            self.c2 += 1
            if self.c2 < 2:
                return None
            else:
                return "X"

        @cache(call_on_delete=lambda x: x.close())
        def test_close(self, version):
            self.count += 1
            return my_closable

        @cache(call_on_delete=lambda x: x.close())
        def test_close_2(self):
            self.count += 1
            return my_closable_2

    test = DT(xcache)

    xcache.open_version(3)
    test.test_close(version=3)
    test.test_close_2()
    xcache.close()
    assert my_closable.closed
    assert my_closable_2.closed

    test.count = 0
    my_closable.closed = False

    assert "x" == test.test_method()
    assert "x" == test.test_method()
    assert "x" == test.test_method()
    assert 1 == test.count

    xcache.open_version(1)
    xcache.open_version(2)
    assert "x2" == test.test_method_2(version=1)
    assert "x2" == test.test_method_2(version=1)
    assert 2 == test.count
    assert "x2" == test.test_method_2(version=2)
    assert 3 == test.count
    xcache.close_version(1)
    xcache.open_version(1)
    assert "x2" == test.test_method_2(version=1)
    assert "x2" == test.test_method_2(version=1)
    assert 4 == test.count

    assert None is test.test_method_3()
    assert 1 == test.c2
    assert "X" == test.test_method_3()
    assert 2 == test.c2
    assert "X" == test.test_method_3()
    assert 2 == test.c2

    test.count = 0
    xcache.open_version(3)
    test.test_close(version=3)
    assert test.count == 1
    test.test_close(version=3)
    assert test.count == 1
    assert not my_closable.closed
    xcache.close_version(3)
    assert my_closable.closed