| 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 | import time |
|---|
| 33 | import unittest |
|---|
| 34 | |
|---|
| 35 | import ftp_stat_cache |
|---|
| 36 | import _test_base |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class 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 | |
|---|
| 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 | |
|---|
| 83 | self.cache.max_age = 1 |
|---|
| 84 | time.sleep(0.5) |
|---|
| 85 | |
|---|
| 86 | self.assertEqual(self.cache["/path1"], "test1") |
|---|
| 87 | time.sleep(0.6) |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 95 | self.cache.max_age = 1 |
|---|
| 96 | self.cache["/path1"] = "test1" |
|---|
| 97 | time.sleep(0.5) |
|---|
| 98 | |
|---|
| 99 | self.assertEqual(self.cache["/path1"], "test1") |
|---|
| 100 | time.sleep(0.6) |
|---|
| 101 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 121 | items = host.listdir(host.curdir) |
|---|
| 122 | self.assertEqual(items[:3], ['chemeng', 'download', 'image']) |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | if __name__ == '__main__': |
|---|
| 126 | unittest.main() |
|---|