| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import time |
|---|
| 5 | import unittest |
|---|
| 6 | |
|---|
| 7 | import ftp_stat_cache |
|---|
| 8 | import _test_base |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | class TestStatCache(unittest.TestCase): |
|---|
| 12 | def setUp(self): |
|---|
| 13 | self.cache = ftp_stat_cache.StatCache() |
|---|
| 14 | |
|---|
| 15 | def test_get_set(self): |
|---|
| 16 | self.assertRaises(ftp_stat_cache.CacheMissError, |
|---|
| 17 | self.cache.__getitem__, "path") |
|---|
| 18 | self.cache["path"] = "test" |
|---|
| 19 | self.assertEqual(self.cache["path"], "test") |
|---|
| 20 | |
|---|
| 21 | def test_invalidate(self): |
|---|
| 22 | |
|---|
| 23 | self.cache.invalidate("/path") |
|---|
| 24 | self.cache["/path"] = "test" |
|---|
| 25 | self.cache.invalidate("/path") |
|---|
| 26 | self.assertEqual(len(self.cache), 0) |
|---|
| 27 | |
|---|
| 28 | def test_clear(self): |
|---|
| 29 | self.cache["path1"] = "test1" |
|---|
| 30 | self.cache["path2"] = "test2" |
|---|
| 31 | self.cache.clear() |
|---|
| 32 | self.assertEqual(len(self.cache), 0) |
|---|
| 33 | |
|---|
| 34 | def test_contains(self): |
|---|
| 35 | self.cache["path1"] = "test1" |
|---|
| 36 | self.assertEqual("path1" in self.cache, True) |
|---|
| 37 | self.assertEqual("path2" in self.cache, False) |
|---|
| 38 | |
|---|
| 39 | def test_len(self): |
|---|
| 40 | self.assertEqual(len(self.cache), 0) |
|---|
| 41 | self.cache["path1"] = "test1" |
|---|
| 42 | self.cache["path2"] = "test2" |
|---|
| 43 | self.assertEqual(len(self.cache), 2) |
|---|
| 44 | |
|---|
| 45 | def test_resize(self): |
|---|
| 46 | self.cache.resize(100) |
|---|
| 47 | for i in xrange(150): |
|---|
| 48 | self.cache["/%d" % i] = i |
|---|
| 49 | self.assertEqual(len(self.cache), 100) |
|---|
| 50 | |
|---|
| 51 | def test_max_age1(self): |
|---|
| 52 | """Set expiration after setting a cache item.""" |
|---|
| 53 | self.cache["/path1"] = "test1" |
|---|
| 54 | |
|---|
| 55 | self.cache.max_age = 1 |
|---|
| 56 | time.sleep(0.5) |
|---|
| 57 | |
|---|
| 58 | self.assertEqual(self.cache["/path1"], "test1") |
|---|
| 59 | time.sleep(0.6) |
|---|
| 60 | |
|---|
| 61 | self.assertRaises(ftp_stat_cache.CacheMissError, |
|---|
| 62 | self.cache.__getitem__, "/path1") |
|---|
| 63 | |
|---|
| 64 | def test_max_age2(self): |
|---|
| 65 | """Set expiration before setting a cache item.""" |
|---|
| 66 | |
|---|
| 67 | self.cache.max_age = 1 |
|---|
| 68 | self.cache["/path1"] = "test1" |
|---|
| 69 | time.sleep(0.5) |
|---|
| 70 | |
|---|
| 71 | self.assertEqual(self.cache["/path1"], "test1") |
|---|
| 72 | time.sleep(0.6) |
|---|
| 73 | |
|---|
| 74 | self.assertRaises(ftp_stat_cache.CacheMissError, |
|---|
| 75 | self.cache.__getitem__, "/path1") |
|---|
| 76 | |
|---|
| 77 | def test_disabled(self): |
|---|
| 78 | self.cache["/path1"] = "test1" |
|---|
| 79 | self.cache.disable() |
|---|
| 80 | self.cache["/path2"] = "test2" |
|---|
| 81 | self.assertRaises(ftp_stat_cache.CacheMissError, |
|---|
| 82 | self.cache.__getitem__, "/path1") |
|---|
| 83 | self.assertRaises(ftp_stat_cache.CacheMissError, |
|---|
| 84 | self.cache.__getitem__, "/path2") |
|---|
| 85 | self.assertEqual(len(self.cache), 1) |
|---|
| 86 | |
|---|
| 87 | self.cache.invalidate("/path2") |
|---|
| 88 | |
|---|
| 89 | def test_cache_size_zero(self): |
|---|
| 90 | host = _test_base.ftp_host_factory() |
|---|
| 91 | host.stat_cache.resize(0) |
|---|
| 92 | |
|---|
| 93 | items = host.listdir(host.curdir) |
|---|
| 94 | self.assertEqual(items[:3], ['chemeng', 'download', 'image']) |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | if __name__ == '__main__': |
|---|
| 98 | unittest.main() |
|---|