def test_createfunc(self): cache = Cache('test', **self.CACHE_ARGS) def createfunc(): createfunc.count += 1 return createfunc.count createfunc.count = 0 def keepitlocked(): lock = cache.namespace.get_creation_lock('test') lock.acquire() time.sleep(1.0) lock.release() v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 1) v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 1) cache.remove_value('test') begin = datetime.datetime.utcnow() t = threading.Thread(target=keepitlocked) t.start() v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 2) # Ensure that the `get_value` was blocked by the concurrent thread. assert datetime.datetime.utcnow() - begin > datetime.timedelta(seconds=1) t.join()
def test_spaces_in_keys(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') cache.set_value("has space", 24) assert cache.has_key("has space") assert 24 == cache.get_value("has space") cache.set_value("hasspace", 42) assert cache.has_key("hasspace") assert 42 == cache.get_value("hasspace")
def test_spaces_in_keys(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb', sparse_collection=True, skip_pickle=True) cache.set_value("has space", 24) assert cache.has_key("has space") assert 24 == cache.get_value("has space") cache.set_value("hasspace", 42) assert cache.has_key("hasspace") assert 42 == cache.get_value("hasspace")
def test_spaces_in_keys(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb') cache.set_value("has space", 24) assert cache.has_key("has space") assert 24 == cache.get_value("has space") cache.set_value("hasspace", 42) assert cache.has_key("hasspace") assert 42 == cache.get_value("hasspace")
def test_spaces_in_keys(): cache = Cache("test", data_dir="./cache", url=mc_url, type="ext:memcached") cache.set_value("has space", 24) assert cache.has_key("has space") assert 24 == cache.get_value("has space") cache.set_value("hasspace", 42) assert cache.has_key("hasspace") assert 42 == cache.get_value("hasspace")
def test_spaces_in_keys(self): cache = Cache('test', **self.CACHE_ARGS) cache.set_value("has space", 24) assert cache.has_key("has space") assert 24 == cache.get_value("has space") cache.set_value("hasspace", 42) assert cache.has_key("hasspace") assert 42 == cache.get_value("hasspace")
def test_multi_keys(): cache = Cache("newtests", data_dir="./cache", type="dbm") cache.clear() called = {} def create_func(): called["here"] = True return "howdy" try: cache.get_value("key1") except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") assert "howdy" == cache.get_value("key2", createfunc=create_func) assert called["here"] == True del called["here"] try: cache.get_value("key3") except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") try: cache.get_value("key1") except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") assert "howdy" == cache.get_value("key2", createfunc=create_func) assert called == {}
def test_multi_keys(): cache = Cache('newtests', data_dir='./cache', type='dbm') cache.clear() called = {} def create_func(): called['here'] = True return 'howdy' try: cache.get_value('key1') except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") assert 'howdy' == cache.get_value('key2', createfunc=create_func) assert called['here'] == True del called['here'] try: cache.get_value('key3') except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") try: cache.get_value('key1') except KeyError: pass else: raise Exception("Failed to keyerror on nonexistent key") assert 'howdy' == cache.get_value('key2', createfunc=create_func) assert called == {}
def test_createfunc(self): cache = Cache("test", **self.CACHE_ARGS) def createfunc(): createfunc.count += 1 return createfunc.count createfunc.count = 0 def keepitlocked(): lock = cache.namespace.get_creation_lock("test") lock.acquire() keepitlocked.acquired = True time.sleep(1.0) lock.release() keepitlocked.acquired = False v0 = cache.get_value("test", createfunc=createfunc) self.assertEqual(v0, 1) v0 = cache.get_value("test", createfunc=createfunc) self.assertEqual(v0, 1) cache.remove_value("test") begin = datetime.datetime.utcnow() t = threading.Thread(target=keepitlocked) t.start() while not keepitlocked.acquired: # Wait for the thread that should lock the cache to start. time.sleep(0.001) v0 = cache.get_value("test", createfunc=createfunc) self.assertEqual(v0, 2) # Ensure that the `get_value` was blocked by the concurrent thread. assert datetime.datetime.utcnow() - begin > datetime.timedelta(seconds=1) t.join()
def test_createfunc(self): cache = Cache('test', **self.CACHE_ARGS) def createfunc(): createfunc.count += 1 return createfunc.count createfunc.count = 0 def keepitlocked(): lock = cache.namespace.get_creation_lock('test') lock.acquire() keepitlocked.acquired = True time.sleep(1.0) lock.release() keepitlocked.acquired = False v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 1) v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 1) cache.remove_value('test') begin = datetime.datetime.utcnow() t = threading.Thread(target=keepitlocked) t.start() while not keepitlocked.acquired: # Wait for the thread that should lock the cache to start. time.sleep(0.001) v0 = cache.get_value('test', createfunc=createfunc) self.assertEqual(v0, 2) # Ensure that the `get_value` was blocked by the concurrent thread. assert datetime.datetime.utcnow() - begin > datetime.timedelta(seconds=1) t.join()
def test_fresh_createfunc(): cache = Cache("test_foo", data_dir="./cache", type="dbm") x = cache.get_value("test", createfunc=lambda: 10, expiretime=2) assert x == 10 x = cache.get_value("test", createfunc=lambda: 12, expiretime=2) assert x == 10 x = cache.get_value("test", createfunc=lambda: 14, expiretime=2) assert x == 10 time.sleep(2) x = cache.get_value("test", createfunc=lambda: 16, expiretime=2) assert x == 16 x = cache.get_value("test", createfunc=lambda: 18, expiretime=2) assert x == 16 cache.remove_value("test") assert not cache.has_key("test") x = cache.get_value("test", createfunc=lambda: 20, expiretime=2) assert x == 20
def test_fresh_createfunc(): cache = Cache('test', data_dir='./cache', type='dbm') x = cache.get_value('test', createfunc=lambda: 10, expiretime=2) assert x == 10 x = cache.get_value('test', createfunc=lambda: 12, expiretime=2) assert x == 10 x = cache.get_value('test', createfunc=lambda: 14, expiretime=2) assert x == 10 time.sleep(2) x = cache.get_value('test', createfunc=lambda: 16, expiretime=2) assert x == 16 x = cache.get_value('test', createfunc=lambda: 18, expiretime=2) assert x == 16 cache.remove_value('test') assert not cache.has_key('test') x = cache.get_value('test', createfunc=lambda: 20, expiretime=2) assert x == 20
def test_fresh_createfunc(): cache = Cache('test_foo', data_dir='./cache', type='dbm') x = cache.get_value('test', createfunc=lambda: 10, expiretime=2) assert x == 10 x = cache.get_value('test', createfunc=lambda: 12, expiretime=2) assert x == 10 x = cache.get_value('test', createfunc=lambda: 14, expiretime=2) assert x == 10 time.sleep(2) x = cache.get_value('test', createfunc=lambda: 16, expiretime=2) assert x == 16 x = cache.get_value('test', createfunc=lambda: 18, expiretime=2) assert x == 16 cache.remove_value('test') assert not cache.has_key('test') x = cache.get_value('test', createfunc=lambda: 20, expiretime=2) assert x == 20
def test_legacy_cache(): cache = Cache('newtests', data_dir='./cache', type='dbm') cache.set_value('x', '1') assert cache.get_value('x') == '1' cache.set_value('x', '2', type='file', data_dir='./cache') assert cache.get_value('x') == '1' assert cache.get_value('x', type='file', data_dir='./cache') == '2' cache.remove_value('x') cache.remove_value('x', type='file', data_dir='./cache') assert cache.get_value('x', expiretime=1, createfunc=lambda: '5') == '5' assert cache.get_value('x', expiretime=1, createfunc=lambda: '6', type='file', data_dir='./cache') == '6' assert cache.get_value('x', expiretime=1, createfunc=lambda: '7') == '5' assert cache.get_value('x', expiretime=1, createfunc=lambda: '8', type='file', data_dir='./cache') == '6' time.sleep(1) assert cache.get_value('x', expiretime=1, createfunc=lambda: '9') == '9' assert cache.get_value('x', expiretime=1, createfunc=lambda: '10', type='file', data_dir='./cache') == '10' assert cache.get_value('x', expiretime=1, createfunc=lambda: '11') == '9' assert cache.get_value('x', expiretime=1, createfunc=lambda: '12', type='file', data_dir='./cache') == '10'
def test_legacy_cache(): cache = Cache("newtests", data_dir="./cache", type="dbm") cache.set_value("x", "1") assert cache.get_value("x") == "1" cache.set_value("x", "2", type="file", data_dir="./cache") assert cache.get_value("x") == "1" assert cache.get_value("x", type="file", data_dir="./cache") == "2" cache.remove_value("x") cache.remove_value("x", type="file", data_dir="./cache") assert cache.get_value("x", expiretime=1, createfunc=lambda: "5") == "5" assert cache.get_value("x", expiretime=1, createfunc=lambda: "6", type="file", data_dir="./cache") == "6" assert cache.get_value("x", expiretime=1, createfunc=lambda: "7") == "5" assert cache.get_value("x", expiretime=1, createfunc=lambda: "8", type="file", data_dir="./cache") == "6" time.sleep(1) assert cache.get_value("x", expiretime=1, createfunc=lambda: "9") == "9" assert cache.get_value("x", expiretime=1, createfunc=lambda: "10", type="file", data_dir="./cache") == "10" assert cache.get_value("x", expiretime=1, createfunc=lambda: "11") == "9" assert cache.get_value("x", expiretime=1, createfunc=lambda: "12", type="file", data_dir="./cache") == "10"
def test_legacy_cache(): cache = Cache("newtests", data_dir="./cache", type="dbm") cache.set_value("x", "1") assert cache.get_value("x") == "1" cache.set_value("x", "2", type="file", data_dir="./cache") assert cache.get_value("x") == "1" assert cache.get_value("x", type="file", data_dir="./cache") == "2" cache.remove_value("x") cache.remove_value("x", type="file", data_dir="./cache") assert cache.get_value("x", expiretime=1, createfunc=lambda: "5") == "5" assert (cache.get_value("x", expiretime=1, createfunc=lambda: "6", type="file", data_dir="./cache") == "6") assert cache.get_value("x", expiretime=1, createfunc=lambda: "7") == "5" assert (cache.get_value("x", expiretime=1, createfunc=lambda: "8", type="file", data_dir="./cache") == "6") time.sleep(1) assert cache.get_value("x", expiretime=1, createfunc=lambda: "9") == "9" assert (cache.get_value("x", expiretime=1, createfunc=lambda: "10", type="file", data_dir="./cache") == "10") assert cache.get_value("x", expiretime=1, createfunc=lambda: "11") == "9" assert (cache.get_value("x", expiretime=1, createfunc=lambda: "12", type="file", data_dir="./cache") == "10")