source: test/test_stat_cache.py @ 1713:f146a1ea66aa

Last change on this file since 1713:f146a1ea66aa was 1713:f146a1ea66aa, checked in by Stefan Schwarzer <sschwarzer@…>, 8 months ago
Remove `__future__` imports With the switch to Python 3.x-only, the `__future__` imports are no longer needed. Update copyright years along with the `__future__` import removal.
File size: 3.3 KB
Line 
1# Copyright (C) 2006-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5import time
6
7import pytest
8
9import ftputil.error
10import ftputil.stat_cache
11
12from test import test_base
13
14
15class TestStatCache(object):
16
17    def setup_method(self, method):
18        self.cache = ftputil.stat_cache.StatCache()
19
20    def test_get_set(self):
21        with pytest.raises(ftputil.error.CacheMissError):
22            self.cache.__getitem__("/path")
23        self.cache["/path"] = "test"
24        assert self.cache["/path"] == "test"
25
26    def test_invalidate(self):
27        # Don't raise a `CacheMissError` for missing paths
28        self.cache.invalidate("/path")
29        self.cache["/path"] = "test"
30        self.cache.invalidate("/path")
31        assert len(self.cache) == 0
32
33    def test_clear(self):
34        self.cache["/path1"] = "test1"
35        self.cache["/path2"] = "test2"
36        self.cache.clear()
37        assert len(self.cache) == 0
38
39    def test_contains(self):
40        self.cache["/path1"] = "test1"
41        assert "/path1" in self.cache
42        assert "/path2" not in self.cache
43
44    def test_len(self):
45        assert len(self.cache) == 0
46        self.cache["/path1"] = "test1"
47        self.cache["/path2"] = "test2"
48        assert len(self.cache) == 2
49
50    def test_resize(self):
51        self.cache.resize(100)
52        # Don't grow the cache beyond it's set size.
53        for i in range(150):
54            self.cache["/{0:d}".format(i)] = i
55        assert len(self.cache) == 100
56
57    def test_max_age1(self):
58        """Set expiration after setting a cache item."""
59        self.cache["/path1"] = "test1"
60        # Expire after one second
61        self.cache.max_age = 1
62        time.sleep(0.5)
63        # Should still be present
64        assert self.cache["/path1"] == "test1"
65        time.sleep(0.6)
66        # Should have expired (_setting_ the cache counts)
67        with pytest.raises(ftputil.error.CacheMissError):
68            self.cache.__getitem__("/path1")
69
70    def test_max_age2(self):
71        """Set expiration before setting a cache item."""
72        # Expire after one second
73        self.cache.max_age = 1
74        self.cache["/path1"] = "test1"
75        time.sleep(0.5)
76        # Should still be present
77        assert self.cache["/path1"] == "test1"
78        time.sleep(0.6)
79        # Should have expired (_setting_ the cache counts)
80        with pytest.raises(ftputil.error.CacheMissError):
81            self.cache.__getitem__("/path1")
82
83    def test_disabled(self):
84        self.cache["/path1"] = "test1"
85        self.cache.disable()
86        self.cache["/path2"] = "test2"
87        with pytest.raises(ftputil.error.CacheMissError):
88            self.cache.__getitem__("/path1")
89        with pytest.raises(ftputil.error.CacheMissError):
90            self.cache.__getitem__("/path2")
91        assert len(self.cache) == 1
92        # Don't raise a `CacheMissError` for missing paths.
93        self.cache.invalidate("/path2")
94
95    def test_cache_size_zero(self):
96        host = test_base.ftp_host_factory()
97        with pytest.raises(ValueError):
98            host.stat_cache.resize(0)
99        # If bug #38 was present, this raised an `IndexError`.
100        items = host.listdir(host.curdir)
101        assert items[:3] == ["chemeng", "download", "image"]
Note: See TracBrowser for help on using the repository browser.