File: test_stat_cache.py

package info (click to toggle)
python-ftputil 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 848 kB
  • sloc: python: 3,308; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright (C) 2006-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.

from __future__ import unicode_literals

import time

import pytest

import ftputil.error
import ftputil.stat_cache

from test import test_base


class TestStatCache(object):

    def setup_method(self, method):
        self.cache = ftputil.stat_cache.StatCache()

    def test_get_set(self):
        with pytest.raises(ftputil.error.CacheMissError):
            self.cache.__getitem__("/path")
        self.cache["/path"] = "test"
        assert self.cache["/path"] == "test"

    def test_invalidate(self):
        # Don't raise a `CacheMissError` for missing paths
        self.cache.invalidate("/path")
        self.cache["/path"] = "test"
        self.cache.invalidate("/path")
        assert len(self.cache) == 0

    def test_clear(self):
        self.cache["/path1"] = "test1"
        self.cache["/path2"] = "test2"
        self.cache.clear()
        assert len(self.cache) == 0

    def test_contains(self):
        self.cache["/path1"] = "test1"
        assert "/path1" in self.cache
        assert "/path2" not in self.cache

    def test_len(self):
        assert len(self.cache) == 0
        self.cache["/path1"] = "test1"
        self.cache["/path2"] = "test2"
        assert len(self.cache) == 2

    def test_resize(self):
        self.cache.resize(100)
        # Don't grow the cache beyond it's set size.
        for i in range(150):
            self.cache["/{0:d}".format(i)] = i
        assert len(self.cache) == 100

    def test_max_age1(self):
        """Set expiration after setting a cache item."""
        self.cache["/path1"] = "test1"
        # Expire after one second
        self.cache.max_age = 1
        time.sleep(0.5)
        # Should still be present
        assert self.cache["/path1"] == "test1"
        time.sleep(0.6)
        # Should have expired (_setting_ the cache counts)
        with pytest.raises(ftputil.error.CacheMissError):
            self.cache.__getitem__("/path1")

    def test_max_age2(self):
        """Set expiration before setting a cache item."""
        # Expire after one second
        self.cache.max_age = 1
        self.cache["/path1"] = "test1"
        time.sleep(0.5)
        # Should still be present
        assert self.cache["/path1"] == "test1"
        time.sleep(0.6)
        # Should have expired (_setting_ the cache counts)
        with pytest.raises(ftputil.error.CacheMissError):
            self.cache.__getitem__("/path1")

    def test_disabled(self):
        self.cache["/path1"] = "test1"
        self.cache.disable()
        self.cache["/path2"] = "test2"
        with pytest.raises(ftputil.error.CacheMissError):
            self.cache.__getitem__("/path1")
        with pytest.raises(ftputil.error.CacheMissError):
            self.cache.__getitem__("/path2")
        assert len(self.cache) == 1
        # Don't raise a `CacheMissError` for missing paths.
        self.cache.invalidate("/path2")

    def test_cache_size_zero(self):
        host = test_base.ftp_host_factory()
        with pytest.raises(ValueError):
            host.stat_cache.resize(0)
        # If bug #38 was present, this raised an `IndexError`.
        items = host.listdir(host.curdir)
        assert items[:3] == ["chemeng", "download", "image"]