Пример #1
0
    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()
Пример #2
0
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")
Пример #3
0
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")
Пример #4
0
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")
Пример #5
0
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")
Пример #6
0
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")
Пример #7
0
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")
Пример #8
0
 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")
Пример #9
0
 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")
Пример #10
0
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")
Пример #11
0
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")
Пример #12
0
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 == {}
Пример #13
0
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 == {}
Пример #14
0
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 == {}
Пример #15
0
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 == {}
Пример #16
0
    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()
Пример #17
0
    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()
Пример #18
0
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
Пример #19
0
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
Пример #20
0
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
Пример #21
0
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
Пример #22
0
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'
Пример #23
0
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"
Пример #24
0
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'
Пример #25
0
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")