Beispiel #1
0
class TestFileCache(object):
    @pytest.fixture(autouse=True)
    def setUp(self, request):
        def fin():
            os.system("rm -fr test_cache")

        request.addfinalizer(fin)
        self.cache = FileCache("test_cache")

    def testTimeouts(self):
        assert self.cache.get(
            "date/test/1") is None, "Key already exists in cache"
        self.cache.set("date/test/1", "1", timeout=3)
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        time.sleep(4)
        assert self.cache.get("date/test/1") is None, "Timeout failed"
        # if _default_timeout is 0, timeouts are simply not checked
        self.cache.wipe()
        self.cache._default_timeout = 0
        assert self.cache.get(
            "date/test/1") is None, "Key already exists in cache"
        self.cache.set("date/test/1", "1", timeout=3)
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        time.sleep(4)
        assert self.cache.has_key("date/test/1")  # noqa
        assert self.cache.get(
            "date/test/1") == "1", "Key got timedout and should not"

    def testMaxSize(self):
        empty_size, cache_block = _testCacheFSBlockSize(self.cache)
        self.cache._max_size = empty_size + 4 * cache_block + 1
        # There is an overhead (8 bytes in my system) for timestamp per file,
        # and the limit is only enforced after we cross over it
        for i in range(6):
            self.cache.set("date/test/%d" % i, "abcdefgh" * 127 * cache_block)
        for i in range(4):
            assert (self.cache.get("date/test/%d" % i) == "abcdefgh" * 127 *
                    cache_block), ("Key %d not properly cached" % i)
        assert self.cache.get("date/test/5") is None, "Size limit failed"
        self.cache._max_size = 0
        self.cache.wipe()
        for i in range(6):
            self.cache.set("date/test/%d" % i, "abcdefgh" * 127 * cache_block)
        for i in range(6):
            assert (self.cache.get("date/test/%d" % i) == "abcdefgh" * 127 *
                    cache_block), ("Key %d not properly cached" % i)

    def testMaxEntries(self):
        self.cache._max_entries = 2
        self.cache.set("date/test/1", "1")
        self.cache.set("date/test/2", "2")
        self.cache.set("date/test/3", "3")
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        assert self.cache.get("date/test/2") == "2", "Key not properly cached"
        assert self.cache.get(
            "date/test/3") is None, "File number limit failed"
        self.cache.wipe()
        self.cache._max_entries = 0
        self.cache.set("date/test/1", "1")
        self.cache.set("date/test/2", "2")
        self.cache.set("date/test/3", "3")
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        assert self.cache.get("date/test/2") == "2", "Key not properly cached"
        assert self.cache.get("date/test/3") == "3", "Key not properly cached"

    def testPurge(self):
        self.cache._max_entries = 2
        self.cache._default_timeout = 3
        self.cache.set("date/test/1", "1")
        self.cache.set("date/test/2", "2")
        self.cache.set("date/test/3", "3")
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        assert self.cache.get("date/test/2") == "2", "Key not properly cached"
        assert self.cache.get(
            "date/test/3") is None, "File number limit failed"
        time.sleep(4)
        self.cache.set("date/test/3", "3")
        assert self.cache.get("date/test/3") == "3", "Purge not working"

    def testOther(self):
        # set should only accept strings as values
        pytest.raises(ValueError, self.cache.set, "date/test/1", 123)
        # keys can't have .. or start with /
        pytest.raises(ValueError, self.cache.set, "/date/test/1", "1")
        pytest.raises(ValueError, self.cache.set, "date/test/../1", "1")
        # get some test data in
        self.cache.set("date/test/1", "1")
        self.cache.set("date/test/2", "2")
        self.cache.set("date/test/3", "3")
        assert self.cache.get("date/test/1") == "1", "Key not properly cached"
        assert self.cache.get("date/test/2") == "2", "Key not properly cached"
        assert self.cache.get("date/test/3") == "3", "Key not properly cached"
        # check has_key
        assert self.cache.has_key("date/test/1")  # noqa
        assert not self.cache.has_key("date/test/bogus")  # noqa
        # assert wipe() nukes the whole thing
        assert self.cache._num_entries == 3
        self.cache.wipe()
        assert self.cache._num_entries == 0
Beispiel #2
0
    def setUp(self, request):
        def fin():
            os.system("rm -fr test_cache")

        request.addfinalizer(fin)
        self.cache = FileCache("test_cache")
Beispiel #3
0
class TestFileCache(object):
    @pytest.fixture(autouse=True)
    def setUp(self, request):
        def fin():
            os.system('rm -fr test_cache')

        request.addfinalizer(fin)
        self.cache = FileCache('test_cache')

    def testTimeouts(self):
        assert self.cache.get('date/test/1') is None, (
            'Key already exists in cache')
        self.cache.set('date/test/1', '1', timeout=3)
        assert self.cache.get('date/test/1') == '1', 'Key not properly cached'
        time.sleep(4)
        assert self.cache.get('date/test/1') is None, 'Timeout failed'
        # if _default_timeout is 0, timeouts are simply not checked
        self.cache.wipe()
        self.cache._default_timeout = 0
        assert self.cache.get('date/test/1') is None, (
            'Key already exists in cache')
        self.cache.set('date/test/1', '1', timeout=3)
        assert self.cache.get('date/test/1') == '1', (
            'Key not properly cached')
        time.sleep(4)
        assert self.cache.has_key('date/test/1')  # noqa
        assert self.cache.get('date/test/1') == '1', (
            'Key got timedout and should not')

    def testMaxSize(self):
        empty_size, cache_block = _testCacheFSBlockSize(self.cache)
        self.cache._max_size = empty_size + 4 * cache_block + 1
        # There is an overhead (8 bytes in my system) for timestamp per file,
        # and the limit is only enforced after we cross over it
        for i in range(6):
            self.cache.set('date/test/%d' % i, 'abcdefgh' * 127 * cache_block)
        for i in range(4):
            assert (self.cache.get('date/test/%d' % i) == 'abcdefgh' * 127 *
                    cache_block), ('Key %d not properly cached' % i)
        assert self.cache.get('date/test/5') is None, 'Size limit failed'
        self.cache._max_size = 0
        self.cache.wipe()
        for i in range(6):
            self.cache.set('date/test/%d' % i, 'abcdefgh' * 127 * cache_block)
        for i in range(6):
            assert (self.cache.get('date/test/%d' % i) == 'abcdefgh' * 127 *
                    cache_block), ('Key %d not properly cached' % i)

    def testMaxEntries(self):
        self.cache._max_entries = 2
        self.cache.set('date/test/1', '1')
        self.cache.set('date/test/2', '2')
        self.cache.set('date/test/3', '3')
        assert self.cache.get('date/test/1') == '1', 'Key not properly cached'
        assert self.cache.get('date/test/2') == '2', 'Key not properly cached'
        assert self.cache.get('date/test/3') is None, (
            'File number limit failed')
        self.cache.wipe()
        self.cache._max_entries = 0
        self.cache.set('date/test/1', '1')
        self.cache.set('date/test/2', '2')
        self.cache.set('date/test/3', '3')
        assert self.cache.get('date/test/1') == '1', 'Key not properly cached'
        assert self.cache.get('date/test/2') == '2', 'Key not properly cached'
        assert self.cache.get('date/test/3') == '3', 'Key not properly cached'

    def testPurge(self):
        self.cache._max_entries = 2
        self.cache._default_timeout = 3
        self.cache.set('date/test/1', '1')
        self.cache.set('date/test/2', '2')
        self.cache.set('date/test/3', '3')
        assert self.cache.get('date/test/1') == '1', 'Key not properly cached'
        assert self.cache.get('date/test/2') == '2', 'Key not properly cached'
        assert self.cache.get('date/test/3') is None, (
            'File number limit failed')
        time.sleep(4)
        self.cache.set('date/test/3', '3')
        assert self.cache.get('date/test/3') == '3', 'Purge not working'

    def testOther(self):
        # set should only accept strings as values
        pytest.raises(ValueError, self.cache.set, 'date/test/1', 123)
        # keys can't have .. or start with /
        pytest.raises(ValueError, self.cache.set, '/date/test/1', '1')
        pytest.raises(ValueError, self.cache.set, 'date/test/../1', '1')
        # get some test data in
        self.cache.set('date/test/1', '1')
        self.cache.set('date/test/2', '2')
        self.cache.set('date/test/3', '3')
        assert self.cache.get('date/test/1') == '1', 'Key not properly cached'
        assert self.cache.get('date/test/2') == '2', 'Key not properly cached'
        assert self.cache.get('date/test/3') == '3', 'Key not properly cached'
        # check has_key
        assert self.cache.has_key('date/test/1')  # noqa
        assert not self.cache.has_key('date/test/bogus')  # noqa
        # assert wipe() nukes the whole thing
        assert self.cache._num_entries == 3
        self.cache.wipe()
        assert self.cache._num_entries == 0