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
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)
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