Ejemplo n.º 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()
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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")
Ejemplo n.º 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 == {}
Ejemplo n.º 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 == {}
Ejemplo n.º 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 == {}
Ejemplo n.º 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 == {}
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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'
Ejemplo n.º 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"
Ejemplo n.º 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'
Ejemplo n.º 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")