| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import os | 6 import os |
| 7 import sys | 7 import sys |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 from caching_file_system import CachingFileSystem | 10 from caching_file_system import CachingFileSystem |
| 11 from file_system import FileSystem, StatInfo | 11 from file_system import FileSystem, StatInfo |
| 12 from future import Future | 12 from future import Future |
| 13 from local_file_system import LocalFileSystem | 13 from local_file_system import LocalFileSystem |
| 14 from object_store_creator import ObjectStoreCreator | 14 from object_store_creator import ObjectStoreCreator |
| 15 from test_file_system import TestFileSystem | 15 from test_file_system import TestFileSystem |
| 16 from test_object_store import TestObjectStore | 16 from test_object_store import TestObjectStore |
| 17 | 17 |
| 18 def _CreateLocalFs(): | 18 def _CreateLocalFs(): |
| 19 return LocalFileSystem( | 19 return LocalFileSystem( |
| 20 os.path.join(sys.path[0], 'test_data', 'file_system')) | 20 os.path.join(sys.path[0], 'test_data', 'file_system')) |
| 21 | 21 |
| 22 class CachingFileSystemTest(unittest.TestCase): | 22 class CachingFileSystemTest(unittest.TestCase): |
| 23 def setUp(self): | 23 def setUp(self): |
| 24 # Use this to make sure that every time _CreateCachingFileSystem is called | 24 # Use this to make sure that every time _CreateCachingFileSystem is called |
| 25 # the underlying object store data is the same, within each test. | 25 # the underlying object store data is the same, within each test. |
| 26 self._object_store_dbs = {} | 26 self._object_store_dbs = {} |
| 27 | 27 |
| 28 def _CreateCachingFileSystem(self, fs, use_existing_values=False): | 28 def _CreateCachingFileSystem(self, fs, start_configuration): |
| 29 def object_store_constructor(namespace, start_empty=False): | 29 def object_store_constructor(namespace, start_empty=False): |
| 30 if namespace not in self._object_store_dbs: | 30 if namespace not in self._object_store_dbs: |
| 31 self._object_store_dbs[namespace] = {} | 31 self._object_store_dbs[namespace] = {} |
| 32 db = self._object_store_dbs[namespace] | 32 db = self._object_store_dbs[namespace] |
| 33 if start_empty: | 33 if start_empty: |
| 34 db.clear() | 34 db.clear() |
| 35 return TestObjectStore(namespace, init=db) | 35 return TestObjectStore(namespace, init=db) |
| 36 return CachingFileSystem( | 36 return CachingFileSystem( |
| 37 fs, | 37 fs, |
| 38 ObjectStoreCreator.TestFactory(store_type=object_store_constructor), | 38 ObjectStoreCreator.TestFactory( |
| 39 use_existing_values=use_existing_values) | 39 store_type=object_store_constructor, |
| 40 start_configuration=start_configuration)) |
| 40 | 41 |
| 41 def testReadFiles(self): | 42 def testReadFiles(self): |
| 42 file_system = CachingFileSystem(_CreateLocalFs(), | 43 file_system = self._CreateCachingFileSystem( |
| 43 ObjectStoreCreator.TestFactory()) | 44 _CreateLocalFs(), |
| 45 ObjectStoreCreator.START_POPULATED) |
| 44 expected = { | 46 expected = { |
| 45 './test1.txt': 'test1\n', | 47 './test1.txt': 'test1\n', |
| 46 './test2.txt': 'test2\n', | 48 './test2.txt': 'test2\n', |
| 47 './test3.txt': 'test3\n', | 49 './test3.txt': 'test3\n', |
| 48 } | 50 } |
| 49 self.assertEqual( | 51 self.assertEqual( |
| 50 expected, | 52 expected, |
| 51 file_system.Read(['./test1.txt', './test2.txt', './test3.txt']).Get()) | 53 file_system.Read(['./test1.txt', './test2.txt', './test3.txt']).Get()) |
| 52 | 54 |
| 53 def testListDir(self): | 55 def testListDir(self): |
| 54 file_system = CachingFileSystem(_CreateLocalFs(), | 56 file_system = self._CreateCachingFileSystem( |
| 55 ObjectStoreCreator.TestFactory()) | 57 _CreateLocalFs(), |
| 58 ObjectStoreCreator.START_POPULATED) |
| 56 expected = ['dir/'] + ['file%d.html' % i for i in range(7)] | 59 expected = ['dir/'] + ['file%d.html' % i for i in range(7)] |
| 57 file_system._read_object_store.Set( | 60 file_system._read_object_store.Set( |
| 58 'list/', | 61 'list/', |
| 59 (expected, file_system.Stat('list/').version)) | 62 (expected, file_system.Stat('list/').version)) |
| 60 self.assertEqual(expected, sorted(file_system.ReadSingle('list/'))) | 63 self.assertEqual(expected, sorted(file_system.ReadSingle('list/'))) |
| 61 | 64 |
| 62 expected.remove('file0.html') | 65 expected.remove('file0.html') |
| 63 file_system._read_object_store.Set( | 66 file_system._read_object_store.Set( |
| 64 'list/', | 67 'list/', |
| 65 (expected, file_system.Stat('list/').version)) | 68 (expected, file_system.Stat('list/').version)) |
| 66 self.assertEqual(expected, sorted(file_system.ReadSingle('list/'))) | 69 self.assertEqual(expected, sorted(file_system.ReadSingle('list/'))) |
| 67 | 70 |
| 68 def testCaching(self): | 71 def testCaching(self): |
| 69 fake_fs = TestFileSystem({ | 72 fake_fs = TestFileSystem({ |
| 70 'bob': { | 73 'bob': { |
| 71 'bob0': 'bob/bob0 contents', | 74 'bob0': 'bob/bob0 contents', |
| 72 'bob1': 'bob/bob1 contents', | 75 'bob1': 'bob/bob1 contents', |
| 73 'bob2': 'bob/bob2 contents', | 76 'bob2': 'bob/bob2 contents', |
| 74 'bob3': 'bob/bob3 contents', | 77 'bob3': 'bob/bob3 contents', |
| 75 } | 78 } |
| 76 }) | 79 }) |
| 77 file_system = self._CreateCachingFileSystem(fake_fs) | 80 def create_empty_caching_fs(): |
| 81 return self._CreateCachingFileSystem(fake_fs, |
| 82 ObjectStoreCreator.START_EMPTY) |
| 83 file_system = create_empty_caching_fs() |
| 78 | 84 |
| 79 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 85 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 80 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) | 86 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) |
| 81 | 87 |
| 82 # Resource has been cached, so test resource is not re-fetched. | 88 # Resource has been cached, so test resource is not re-fetched. |
| 83 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 89 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 84 self.assertTrue(*fake_fs.CheckAndReset()) | 90 self.assertTrue(*fake_fs.CheckAndReset()) |
| 85 | 91 |
| 86 # Test if the Stat version is the same the resource is not re-fetched. | 92 # Test if the Stat version is the same the resource is not re-fetched. |
| 87 file_system = self._CreateCachingFileSystem(fake_fs) | 93 file_system = create_empty_caching_fs() |
| 88 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 94 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 89 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) | 95 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) |
| 90 | 96 |
| 91 # Test if there is a newer version, the resource is re-fetched. | 97 # Test if there is a newer version, the resource is re-fetched. |
| 92 file_system = self._CreateCachingFileSystem(fake_fs) | 98 file_system = create_empty_caching_fs() |
| 93 fake_fs.IncrementStat(); | 99 fake_fs.IncrementStat(); |
| 94 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 100 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 95 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) | 101 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) |
| 96 | 102 |
| 97 # Test directory and subdirectory stats are cached. | 103 # Test directory and subdirectory stats are cached. |
| 98 file_system = self._CreateCachingFileSystem(fake_fs) | 104 file_system = create_empty_caching_fs() |
| 99 file_system._stat_object_store.Del('bob/bob0') | 105 file_system._stat_object_store.Del('bob/bob0') |
| 100 file_system._read_object_store.Del('bob/bob0') | 106 file_system._read_object_store.Del('bob/bob0') |
| 101 file_system._stat_object_store.Del('bob/bob1') | 107 file_system._stat_object_store.Del('bob/bob1') |
| 102 fake_fs.IncrementStat(); | 108 fake_fs.IncrementStat(); |
| 103 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) | 109 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) |
| 104 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 110 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 105 self.assertTrue(*fake_fs.CheckAndReset(read_count=2, stat_count=1)) | 111 self.assertTrue(*fake_fs.CheckAndReset(read_count=2, stat_count=1)) |
| 106 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) | 112 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) |
| 107 self.assertTrue(*fake_fs.CheckAndReset()) | 113 self.assertTrue(*fake_fs.CheckAndReset()) |
| 108 | 114 |
| 109 # Test a more recent parent directory doesn't force a refetch of children. | 115 # Test a more recent parent directory doesn't force a refetch of children. |
| 110 file_system = self._CreateCachingFileSystem(fake_fs) | 116 file_system = create_empty_caching_fs() |
| 111 file_system._read_object_store.Del('bob/bob0') | 117 file_system._read_object_store.Del('bob/bob0') |
| 112 file_system._read_object_store.Del('bob/bob1') | 118 file_system._read_object_store.Del('bob/bob1') |
| 113 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) | 119 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) |
| 114 self.assertEqual('bob/bob2 contents', file_system.ReadSingle('bob/bob2')) | 120 self.assertEqual('bob/bob2 contents', file_system.ReadSingle('bob/bob2')) |
| 115 self.assertEqual('bob/bob3 contents', file_system.ReadSingle('bob/bob3')) | 121 self.assertEqual('bob/bob3 contents', file_system.ReadSingle('bob/bob3')) |
| 116 self.assertTrue(*fake_fs.CheckAndReset(read_count=3, stat_count=1)) | 122 self.assertTrue(*fake_fs.CheckAndReset(read_count=3, stat_count=1)) |
| 117 | 123 |
| 118 fake_fs.IncrementStat(path='bob/') | 124 fake_fs.IncrementStat(path='bob/') |
| 119 file_system = self._CreateCachingFileSystem(fake_fs) | 125 file_system = create_empty_caching_fs() |
| 120 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) | 126 self.assertEqual('bob/bob1 contents', file_system.ReadSingle('bob/bob1')) |
| 121 self.assertEqual('bob/bob2 contents', file_system.ReadSingle('bob/bob2')) | 127 self.assertEqual('bob/bob2 contents', file_system.ReadSingle('bob/bob2')) |
| 122 self.assertEqual('bob/bob3 contents', file_system.ReadSingle('bob/bob3')) | 128 self.assertEqual('bob/bob3 contents', file_system.ReadSingle('bob/bob3')) |
| 123 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) | 129 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) |
| 124 | 130 |
| 125 file_system = self._CreateCachingFileSystem(fake_fs) | 131 file_system = create_empty_caching_fs() |
| 126 file_system._stat_object_store.Del('bob/bob0') | 132 file_system._stat_object_store.Del('bob/bob0') |
| 127 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 133 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 128 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) | 134 self.assertTrue(*fake_fs.CheckAndReset(read_count=1, stat_count=1)) |
| 129 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) | 135 self.assertEqual('bob/bob0 contents', file_system.ReadSingle('bob/bob0')) |
| 130 self.assertTrue(*fake_fs.CheckAndReset()) | 136 self.assertTrue(*fake_fs.CheckAndReset()) |
| 131 | 137 |
| 132 def testCachedStat(self): | 138 def testCachedStat(self): |
| 133 fake_fs = TestFileSystem({ | 139 fake_fs = TestFileSystem({ |
| 134 'bob': { | 140 'bob': { |
| 135 'bob0': 'bob/bob0 contents', | 141 'bob0': 'bob/bob0 contents', |
| 136 'bob1': 'bob/bob1 contents' | 142 'bob1': 'bob/bob1 contents' |
| 137 } | 143 } |
| 138 }) | 144 }) |
| 139 file_system = self._CreateCachingFileSystem(fake_fs, | 145 file_system = self._CreateCachingFileSystem( |
| 140 use_existing_values=True) | 146 fake_fs, |
| 141 | 147 ObjectStoreCreator.START_POPULATED) |
| 142 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob0')) | 148 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob0')) |
| 143 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) | 149 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) |
| 144 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob0')) | 150 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob0')) |
| 145 self.assertTrue(*fake_fs.CheckAndReset()) | 151 self.assertTrue(*fake_fs.CheckAndReset()) |
| 146 | 152 |
| 147 # Caching happens on a directory basis, so reading other files from that | 153 # Caching happens on a directory basis, so reading other files from that |
| 148 # directory won't result in a stat. | 154 # directory won't result in a stat. |
| 149 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob1')) | 155 self.assertEqual(StatInfo('0'), file_system.Stat('bob/bob1')) |
| 150 self.assertEqual( | 156 self.assertEqual( |
| 151 StatInfo('0', child_versions={'bob0': '0', 'bob1': '0'}), | 157 StatInfo('0', child_versions={'bob0': '0', 'bob1': '0'}), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 165 def testFreshStat(self): | 171 def testFreshStat(self): |
| 166 fake_fs = TestFileSystem({ | 172 fake_fs = TestFileSystem({ |
| 167 'bob': { | 173 'bob': { |
| 168 'bob0': 'bob/bob0 contents', | 174 'bob0': 'bob/bob0 contents', |
| 169 'bob1': 'bob/bob1 contents' | 175 'bob1': 'bob/bob1 contents' |
| 170 } | 176 } |
| 171 }) | 177 }) |
| 172 | 178 |
| 173 def run_expecting_stat(stat): | 179 def run_expecting_stat(stat): |
| 174 def run(): | 180 def run(): |
| 175 file_system = self._CreateCachingFileSystem(fake_fs) | 181 file_system = self._CreateCachingFileSystem( |
| 182 fake_fs, |
| 183 ObjectStoreCreator.START_EMPTY) |
| 176 self.assertEqual( | 184 self.assertEqual( |
| 177 StatInfo(stat, child_versions={'bob0': stat, 'bob1': stat}), | 185 StatInfo(stat, child_versions={'bob0': stat, 'bob1': stat}), |
| 178 file_system.Stat('bob/')) | 186 file_system.Stat('bob/')) |
| 179 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) | 187 self.assertTrue(*fake_fs.CheckAndReset(stat_count=1)) |
| 180 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) | 188 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) |
| 181 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) | 189 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) |
| 182 self.assertTrue(*fake_fs.CheckAndReset()) | 190 self.assertTrue(*fake_fs.CheckAndReset()) |
| 183 run() | 191 run() |
| 184 run() | 192 run() |
| 185 | 193 |
| 186 run_expecting_stat('0') | 194 run_expecting_stat('0') |
| 187 fake_fs.IncrementStat() | 195 fake_fs.IncrementStat() |
| 188 run_expecting_stat('1') | 196 run_expecting_stat('1') |
| 189 | 197 |
| 190 if __name__ == '__main__': | 198 if __name__ == '__main__': |
| 191 unittest.main() | 199 unittest.main() |
| OLD | NEW |