| 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 extensions_paths import SERVER2 | 11 from extensions_paths import SERVER2 |
| 12 from file_system import StatInfo | 12 from file_system import FileNotFoundError, StatInfo |
| 13 from local_file_system import LocalFileSystem | 13 from local_file_system import LocalFileSystem |
| 14 from mock_file_system import MockFileSystem | 14 from mock_file_system import MockFileSystem |
| 15 from object_store_creator import ObjectStoreCreator | 15 from object_store_creator import ObjectStoreCreator |
| 16 from test_file_system import TestFileSystem | 16 from test_file_system import TestFileSystem |
| 17 from test_object_store import TestObjectStore | 17 from test_object_store import TestObjectStore |
| 18 | 18 |
| 19 | 19 |
| 20 def _CreateLocalFs(): | 20 def _CreateLocalFs(): |
| 21 return LocalFileSystem.Create(SERVER2, 'test_data', 'file_system/') | 21 return LocalFileSystem.Create(SERVER2, 'test_data', 'file_system/') |
| 22 | 22 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 file_system = create_empty_caching_fs() | 152 file_system = create_empty_caching_fs() |
| 153 file_system._stat_object_store.Del('bob/bob0') | 153 file_system._stat_object_store.Del('bob/bob0') |
| 154 future = file_system.ReadSingle('bob/bob0') | 154 future = file_system.ReadSingle('bob/bob0') |
| 155 self.assertTrue(*mock_fs.CheckAndReset(read_count=1)) | 155 self.assertTrue(*mock_fs.CheckAndReset(read_count=1)) |
| 156 self.assertEqual('bob/bob0 contents', future.Get()) | 156 self.assertEqual('bob/bob0 contents', future.Get()) |
| 157 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1, stat_count=1)) | 157 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1, stat_count=1)) |
| 158 self.assertEqual('bob/bob0 contents', | 158 self.assertEqual('bob/bob0 contents', |
| 159 file_system.ReadSingle('bob/bob0').Get()) | 159 file_system.ReadSingle('bob/bob0').Get()) |
| 160 self.assertTrue(*mock_fs.CheckAndReset()) | 160 self.assertTrue(*mock_fs.CheckAndReset()) |
| 161 | 161 |
| 162 # Test skip_not_found caching behavior. |
| 163 file_system = create_empty_caching_fs() |
| 164 future = file_system.ReadSingle('bob/no_file', skip_not_found=True) |
| 165 self.assertTrue(*mock_fs.CheckAndReset(read_count=1)) |
| 166 self.assertEqual(None, future.Get()) |
| 167 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1, stat_count=1)) |
| 168 future = file_system.ReadSingle('bob/no_file', skip_not_found=True) |
| 169 # There shouldn't be another read/stat from the file system; |
| 170 # we know the file is not there. |
| 171 self.assertTrue(*mock_fs.CheckAndReset()) |
| 172 future = file_system.ReadSingle('bob/no_file') |
| 173 self.assertTrue(*mock_fs.CheckAndReset(read_count=1)) |
| 174 # Even though we cached information about non-existent files, |
| 175 # trying to read one without specifiying skip_not_found should |
| 176 # still raise an error. |
| 177 self.assertRaises(FileNotFoundError, future.Get) |
| 178 |
| 162 def testCachedStat(self): | 179 def testCachedStat(self): |
| 163 test_fs = TestFileSystem({ | 180 test_fs = TestFileSystem({ |
| 164 'bob': { | 181 'bob': { |
| 165 'bob0': 'bob/bob0 contents', | 182 'bob0': 'bob/bob0 contents', |
| 166 'bob1': 'bob/bob1 contents' | 183 'bob1': 'bob/bob1 contents' |
| 167 } | 184 } |
| 168 }) | 185 }) |
| 169 mock_fs = MockFileSystem(test_fs) | 186 mock_fs = MockFileSystem(test_fs) |
| 170 | 187 |
| 171 file_system = self._CreateCachingFileSystem(mock_fs, start_empty=False) | 188 file_system = self._CreateCachingFileSystem(mock_fs, start_empty=False) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) | 229 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) |
| 213 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) | 230 self.assertEqual(StatInfo(stat), file_system.Stat('bob/bob0')) |
| 214 self.assertTrue(*mock_fs.CheckAndReset()) | 231 self.assertTrue(*mock_fs.CheckAndReset()) |
| 215 run() | 232 run() |
| 216 run() | 233 run() |
| 217 | 234 |
| 218 run_expecting_stat('0') | 235 run_expecting_stat('0') |
| 219 test_fs.IncrementStat() | 236 test_fs.IncrementStat() |
| 220 run_expecting_stat('1') | 237 run_expecting_stat('1') |
| 221 | 238 |
| 222 def testSkipNotFound(self): | 239 def testSkipNotFound(self): |
| 223 caching_fs = self._CreateCachingFileSystem(TestFileSystem({ | 240 caching_fs = self._CreateCachingFileSystem(TestFileSystem({ |
| 224 'bob': { | 241 'bob': { |
| 225 'bob0': 'bob/bob0 contents', | 242 'bob0': 'bob/bob0 contents', |
| 226 'bob1': 'bob/bob1 contents' | 243 'bob1': 'bob/bob1 contents' |
| 227 } | 244 } |
| 228 })) | 245 })) |
| 229 def read_skip_not_found(paths): | 246 def read_skip_not_found(paths): |
| 230 return caching_fs.Read(paths, skip_not_found=True).Get() | 247 return caching_fs.Read(paths, skip_not_found=True).Get() |
| 231 self.assertEqual({}, read_skip_not_found(('grub',))) | 248 self.assertEqual({}, read_skip_not_found(('grub',))) |
| 232 self.assertEqual({}, read_skip_not_found(('bob/bob2',))) | 249 self.assertEqual({}, read_skip_not_found(('bob/bob2',))) |
| 233 self.assertEqual({ | 250 self.assertEqual({ |
| 234 'bob/bob0': 'bob/bob0 contents', | 251 'bob/bob0': 'bob/bob0 contents', |
| 235 }, read_skip_not_found(('bob/bob0', 'bob/bob2'))) | 252 }, read_skip_not_found(('bob/bob0', 'bob/bob2'))) |
| 236 | 253 |
| 237 | 254 |
| 238 if __name__ == '__main__': | 255 if __name__ == '__main__': |
| 239 unittest.main() | 256 unittest.main() |
| OLD | NEW |