root/_test_ftp_stat_cache.py @ 843:d887d0aa8e84

Revision 843:d887d0aa8e84, 4.7 kB (checked in by Stefan Schwarzer <sschwarzer@…>, 7 months ago)
Removed now-unused Subversion keywords (migration to Mercurial).
Line 
1# Copyright (C) 2006, Stefan Schwarzer
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# - Redistributions of source code must retain the above copyright
9#   notice, this list of conditions and the following disclaimer.
10#
11# - Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14#
15# - Neither the name of the above author nor the names of the
16#   contributors to the software may be used to endorse or promote
17#   products derived from this software without specific prior written
18#   permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
24# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32import time
33import unittest
34
35import ftp_stat_cache
36import _test_base
37
38
39class TestStatCache(unittest.TestCase):
40    def setUp(self):
41        self.cache = ftp_stat_cache.StatCache()
42
43    def test_get_set(self):
44        self.assertRaises(ftp_stat_cache.CacheMissError,
45                          self.cache.__getitem__, "path")
46        self.cache["path"] = "test"
47        self.assertEqual(self.cache["path"], "test")
48
49    def test_invalidate(self):
50        # don't raise a `CacheMissError` for missing paths
51        self.cache.invalidate("/path")
52        self.cache["/path"] = "test"
53        self.cache.invalidate("/path")
54        self.assertEqual(len(self.cache), 0)
55
56    def test_clear(self):
57        self.cache["path1"] = "test1"
58        self.cache["path2"] = "test2"
59        self.cache.clear()
60        self.assertEqual(len(self.cache), 0)
61
62    def test_contains(self):
63        self.cache["path1"] = "test1"
64        self.assertEqual("path1" in self.cache, True)
65        self.assertEqual("path2" in self.cache, False)
66
67    def test_len(self):
68        self.assertEqual(len(self.cache), 0)
69        self.cache["path1"] = "test1"
70        self.cache["path2"] = "test2"
71        self.assertEqual(len(self.cache), 2)
72
73    def test_resize(self):
74        self.cache.resize(100)
75        for i in xrange(150):
76            self.cache["/%d" % i] = i
77        self.assertEqual(len(self.cache), 100)
78
79    def test_max_age1(self):
80        """Set expiration after setting a cache item."""
81        self.cache["/path1"] = "test1"
82        # expire after one second
83        self.cache.max_age = 1
84        time.sleep(0.5)
85        # should still be present
86        self.assertEqual(self.cache["/path1"], "test1")
87        time.sleep(0.6)
88        # should have expired (_setting_ the cache counts)
89        self.assertRaises(ftp_stat_cache.CacheMissError,
90                          self.cache.__getitem__, "/path1")
91
92    def test_max_age2(self):
93        """Set expiration before setting a cache item."""
94        # expire after one second
95        self.cache.max_age = 1
96        self.cache["/path1"] = "test1"
97        time.sleep(0.5)
98        # should still be present
99        self.assertEqual(self.cache["/path1"], "test1")
100        time.sleep(0.6)
101        # should have expired (_setting_ the cache counts)
102        self.assertRaises(ftp_stat_cache.CacheMissError,
103                          self.cache.__getitem__, "/path1")
104
105    def test_disabled(self):
106        self.cache["/path1"] = "test1"
107        self.cache.disable()
108        self.cache["/path2"] = "test2"
109        self.assertRaises(ftp_stat_cache.CacheMissError,
110                          self.cache.__getitem__, "/path1")
111        self.assertRaises(ftp_stat_cache.CacheMissError,
112                          self.cache.__getitem__, "/path2")
113        self.assertEqual(len(self.cache), 1)
114        # don't raise a `CacheMissError` for missing paths
115        self.cache.invalidate("/path2")
116
117    def test_cache_size_zero(self):
118        host = _test_base.ftp_host_factory()
119        host.stat_cache.resize(0)
120        # if bug #38 is present, this raises an `IndexError`
121        items = host.listdir(host.curdir)
122        self.assertEqual(items[:3], ['chemeng', 'download', 'image'])
123
124
125if __name__ == '__main__':
126    unittest.main()
Note: See TracBrowser for help on using the browser.