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_unicode_keys(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() cache.set_value(u_('hi ŏ'), o) assert u_('hi ŏ') in cache assert u_('hŏa') not in cache cache.remove_value(u_('hi ŏ')) assert u_('hi ŏ') not in cache
def test_spaces_in_unicode_keys(): cache = Cache("test", data_dir="./cache", url=mc_url, type="ext:memcached") o = object() cache.set_value(u"hi ŏ", o) assert u"hi ŏ" in cache assert u"hŏa" not in cache cache.remove_value(u"hi ŏ") assert u"hi ŏ" not in cache
def test_unicode_keys(): cache = Cache('test', data_dir='./cache', type='dbm') o = object() cache.set_value(u'hiŏ', o) assert u'hiŏ' in cache assert u'hŏa' not in cache cache.remove_value(u'hiŏ') assert u'hiŏ' not in cache
def test_has_key_multicache(): cache = Cache('test', data_dir='./cache', url=db_url, type='ext:database') o = object() cache.set_value("test", o) assert "test" in cache cache = Cache('test', data_dir='./cache', url=db_url, type='ext:database') assert "test" in cache cache.remove_value('test')
def test_spaces_in_unicode_keys(): cache = Cache("test", data_dir="./cache", url=mc_url, type="ext:memcached") o = object() cache.set_value(u_("hi ŏ"), o) assert u_("hi ŏ") in cache assert u_("hŏa") not in cache cache.remove_value(u_("hi ŏ")) assert u_("hi ŏ") not in cache
def test_unicode_keys(): cache = Cache("test", data_dir="./cache", type="dbm") o = object() cache.set_value(u_("hiŏ"), o) assert u_("hiŏ") in cache assert u_("hŏa") not in cache cache.remove_value(u_("hiŏ")) assert u_("hiŏ") not in cache
def test_unicode_keys(): cache = Cache('test', data_dir='./cache', url=db_url, type='ext:database') o = object() cache.set_value(u_('hiŏ'), o) assert u_('hiŏ') in cache assert u_('hŏa') not in cache cache.remove_value(u_('hiŏ')) assert u_('hiŏ') not in cache
def test_long_unicode_keys(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() long_str = u_('Очень длинная строка, которая не влезает в сто двадцать восемь байт и поэтому не проходит ограничение в check_key, что очень прискорбно, не правда ли, друзья? Давайте же скорее исправим это досадное недоразумение!') cache.set_value(long_str, o) assert long_str in cache cache.remove_value(long_str) assert long_str not in cache
def test_spaces_in_unicode_keys(self): cache = Cache("test", **self.CACHE_ARGS) o = object() cache.set_value(u_("hi ŏ"), o) assert u_("hi ŏ") in cache assert u_("hŏa") not in cache cache.remove_value(u_("hi ŏ")) assert u_("hi ŏ") not in cache
def test_spaces_in_unicode_keys(self): cache = Cache('test', **self.CACHE_ARGS) o = object() cache.set_value(u_('hi ŏ'), o) assert u_('hi ŏ') in cache assert u_('hŏa') not in cache cache.remove_value(u_('hi ŏ')) assert u_('hi ŏ') not in cache
def test_unicode_keys(): cache = Cache("test", data_dir="./cache", url=db_url, type="ext:database") o = object() cache.set_value(u"hiŏ", o) assert u"hiŏ" in cache assert u"hŏa" not in cache cache.remove_value(u"hiŏ") assert u"hiŏ" not in cache
def test_has_key(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() cache.set_value("test", o) assert "test" in cache assert "foo" not in cache cache.remove_value("test") assert "test" not in cache
def test_unicode_keys(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() cache.set_value(u'hiŏ', o) assert u'hiŏ' in cache assert u'hŏa' not in cache cache.remove_value(u'hiŏ') assert u'hiŏ' not in cache
def test_unicode_keys(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb', sparse_collection=True) o = object() cache.set_value(u'hiŏ', o) assert u'hiŏ' in cache assert u'hŏa' not in cache cache.remove_value(u'hiŏ') assert u'hiŏ' not in cache
def test_unicode_keys(): cache = Cache('test', url=db_url, type=db_type, database=db_name) o = object() cache.set_value('hiŏ', o) assert 'hiŏ' in cache assert 'hŏa' not in cache cache.remove_value('hiŏ') assert 'hiŏ' not in cache
def test_spaces_in_unicode_keys(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb') o = object() cache.set_value(u'hi ŏ', o) assert u'hi ŏ' in cache assert u'hŏa' not in cache cache.remove_value(u'hi ŏ') assert u'hi ŏ' not in cache
def test_unicode_keys(): cache = Cache('test', url=db_url, type=db_type, database=db_name) o = object() cache.set_value(u'hiŏ', o) assert u'hiŏ' in cache assert u'hŏa' not in cache cache.remove_value(u'hiŏ') assert u'hiŏ' not in cache
def test_client(self): cache = Cache('test', data_dir='./cache', url=mc_url, type="ext:memcached", protocol='binary') o = object() cache.set_value("test", o) assert "test" in cache assert "foo" not in cache cache.remove_value("test") assert "test" not in cache
def test_has_key_multicache(): cache = Cache('test', data_dir='./cache', url=db_url, type='ext:database') o = object() cache.set_value("test", o) assert "test" in cache assert "test" in cache cache = Cache('test', data_dir='./cache', url=db_url, type='ext:database') assert "test" in cache cache.remove_value('test')
def test_long_unicode_keys(): cache = Cache("test", data_dir="./cache", url=mc_url, type="ext:memcached") o = object() for i in range(0, 260): long_str = u"Ы" * i cache.set_value(long_str, o) assert long_str in cache cache.remove_value(long_str) assert long_str not in cache
def test_has_key_multicache(): cache = Cache('test', url=db_url, type=db_type, database=db_name) o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache cache = Cache('test', url=db_url, type=db_type, database=db_name) assert cache.has_key("test") cache.remove_value('test')
def test_has_key_multicache(): cache = Cache('test', url=db_url, type=db_type, database=db_name) o = object() cache.set_value("test", o) assert "test" in cache assert "test" in cache cache = Cache('test', url=db_url, type=db_type, database=db_name) assert "test" in cache cache.remove_value('test')
def test_has_key_multicache(): cache = Cache("test", data_dir="./cache", url=db_url, type="ext:database") o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache cache = Cache("test", data_dir="./cache", url=db_url, type="ext:database") assert cache.has_key("test") cache.remove_value("test")
def test_has_key(): cache = Cache("test", data_dir="./cache", type="dbm") o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_client(self): cache = Cache("test", data_dir="./cache", url=mc_url, type="ext:memcached", protocol="binary") o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_client(self): cache = Cache('test', data_dir='./cache', url=uri, type="mongodb", sparse_collection=True, skip_pickle=True) o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_has_key(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb', sparse_collection=True) o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_has_key(): cache = Cache('test', url=db_url, type=db_type, database=db_name) o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_has_key(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb') o = object() cache.set_value("test", o) assert "test" in cache assert "test" in cache assert "foo" not in cache assert "foo" not in cache cache.remove_value("test") assert "test" not in cache
def test_has_key(self): cache = Cache('test', **self.CACHE_ARGS) o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_long_unicode_keys(self): cache = Cache('test', **self.CACHE_ARGS) o = object() long_str = u_( 'Очень длинная строка, которая не влезает в сто двадцать восемь байт и поэтому не проходит ограничение в check_key, что очень прискорбно, не правда ли, друзья? Давайте же скорее исправим это досадное недоразумение!' ) cache.set_value(long_str, o) assert long_str in cache cache.remove_value(long_str) assert long_str not in cache
def test_has_key(): cache = Cache('test', data_dir='./cache', url=uri, type='mongodb_gridfs') o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_has_key(): cache = Cache("test", data_dir="./cache", url=db_url, type="ext:database") o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_client(self): cache = Cache('test', data_dir='./cache', url=uri, type="mongodb") o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_has_key(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_long_unicode_keys(): cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached') o = object() long_str = u_( 'Очень длинная строка, которая не влезает в сто двадцать восемь байт и поэтому не проходит ограничение в check_key, что очень прискорбно, не правда ли, друзья? Давайте же скорее исправим это досадное недоразумение!' ) cache.set_value(long_str, o) assert long_str in cache cache.remove_value(long_str) assert long_str not in cache
def test_has_key(): cache = Cache('test', data_dir='./cache', type='dbm') o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_client(self): cache = Cache('test', data_dir='./cache', url=mc_url, type="ext:memcached", protocol='binary') o = object() cache.set_value("test", o) assert cache.has_key("test") assert "test" in cache assert not cache.has_key("foo") assert "foo" not in cache cache.remove_value("test") assert not cache.has_key("test")
def test_remove_stale(): """test that remove_value() removes even if the value is expired.""" cache = Cache('test', type='memory') o = object() cache.namespace[b'key'] = (time.time() - 60, 5, o) container = cache._get_value('key') assert not container.has_current_value() assert b'key' in container.namespace cache.remove_value('key') assert b'key' not in container.namespace # safe to call again cache.remove_value('key')
def test_remove_stale(): """test that remove_value() removes even if the value is expired.""" cache = Cache("test", type="memory") o = object() cache.namespace[b"key"] = (time.time() - 60, 5, o) container = cache._get_value("key") assert not container.has_current_value() assert b"key" in container.namespace cache.remove_value("key") assert b"key" not in container.namespace # safe to call again cache.remove_value("key")
def test_remove_stale(): """test that remove_value() removes even if the value is expired.""" cache = Cache("test", type="memory") o = object() cache.namespace["key"] = (time.time() - 30, 10, o) container = cache._get_value("key") assert not container.has_current_value() assert "key" in container.namespace cache.remove_value("key") assert "key" not in container.namespace # safe to call again cache.remove_value("key")
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_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_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_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'