Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(830)

Side by Side Diff: chrome/common/extensions/docs/server2/compiled_file_system_test.py

Issue 521693003: Docserver: Add @Cache annotation to CompiledFileSystem (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small clarification + rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Copyright 2013 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 functools 6 import functools
7 import os 7 import os
8 8
9 from compiled_file_system import CompiledFileSystem 9 from compiled_file_system import Cache, CompiledFileSystem
10 from copy import deepcopy 10 from copy import deepcopy
11 from environment import GetAppVersion 11 from environment import GetAppVersion
12 from file_system import FileNotFoundError 12 from file_system import FileNotFoundError
13 from mock_file_system import MockFileSystem 13 from mock_file_system import MockFileSystem
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 import unittest 17 import unittest
18 18
19 _TEST_DATA = { 19 _TEST_DATA = {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 return '%s%s' % ('Z' * len(key), 'z' * len(val)) 80 return '%s%s' % ('Z' * len(key), 'z' * len(val))
81 compiled_fs = _GetTestCompiledFsCreator()(Sleepy, CompiledFileSystemTest) 81 compiled_fs = _GetTestCompiledFsCreator()(Sleepy, CompiledFileSystemTest)
82 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', 82 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz',
83 compiled_fs.GetFromFile('404.html').Get()) 83 compiled_fs.GetFromFile('404.html').Get())
84 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', 84 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz',
85 compiled_fs.GetFromFile('apps/a11y.html').Get()) 85 compiled_fs.GetFromFile('apps/a11y.html').Get())
86 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', 86 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz',
87 compiled_fs.GetFromFile('apps/fakedir/file.html').Get()) 87 compiled_fs.GetFromFile('apps/fakedir/file.html').Get())
88 88
89 def testPopulateFromFileListing(self): 89 def testPopulateFromFileListing(self):
90 def strip_ext(path, files): 90 def strip_ext(_, files):
91 return [os.path.splitext(f)[0] for f in files] 91 return [os.path.splitext(f)[0] for f in files]
92 compiled_fs = _GetTestCompiledFsCreator()(strip_ext, CompiledFileSystemTest) 92 compiled_fs = _GetTestCompiledFsCreator()(strip_ext, CompiledFileSystemTest)
93 expected_top_listing = [ 93 expected_top_listing = [
94 '404', 94 '404',
95 'apps/a11y', 95 'apps/a11y',
96 'apps/about_apps', 96 'apps/about_apps',
97 'apps/deepdir/deeper/deepest', 97 'apps/deepdir/deeper/deepest',
98 'apps/deepdir/deepfile', 98 'apps/deepdir/deepfile',
99 'apps/fakedir/file', 99 'apps/fakedir/file',
100 'extensions/activeTab', 100 'extensions/activeTab',
(...skipping 13 matching lines...) Expand all
114 self.assertEqual(['file',], 114 self.assertEqual(['file',],
115 compiled_fs.GetFromFileListing('apps/fakedir/').Get()) 115 compiled_fs.GetFromFileListing('apps/fakedir/').Get())
116 self.assertEqual(['deeper/deepest', 'deepfile'], 116 self.assertEqual(['deeper/deepest', 'deepfile'],
117 sorted(compiled_fs.GetFromFileListing( 117 sorted(compiled_fs.GetFromFileListing(
118 'apps/deepdir/').Get())) 118 'apps/deepdir/').Get()))
119 self.assertEqual(['deepest'], 119 self.assertEqual(['deepest'],
120 compiled_fs.GetFromFileListing( 120 compiled_fs.GetFromFileListing(
121 'apps/deepdir/deeper/').Get()) 121 'apps/deepdir/deeper/').Get())
122 122
123 def testCaching(self): 123 def testCaching(self):
124 compiled_fs = _GetTestCompiledFsCreator()(identity, CompiledFileSystemTest) 124 compiled_fs = _GetTestCompiledFsCreator()(Cache(identity),
125 CompiledFileSystemTest)
125 self.assertEqual('404.html contents', 126 self.assertEqual('404.html contents',
126 compiled_fs.GetFromFile('404.html').Get()) 127 compiled_fs.GetFromFile('404.html').Get())
127 self.assertEqual(set(('file.html',)), 128 self.assertEqual(set(('file.html',)),
128 set(compiled_fs.GetFromFileListing('apps/fakedir/').Get())) 129 set(compiled_fs.GetFromFileListing('apps/fakedir/').Get()))
129 130
130 compiled_fs._file_system._path_values['404.html'] = 'boom' 131 compiled_fs._file_system._path_values['404.html'] = 'boom'
131 compiled_fs._file_system._path_values['apps/fakedir/'] = [ 132 compiled_fs._file_system._path_values['apps/fakedir/'] = [
132 'file.html', 'boom.html'] 133 'file.html', 'boom.html']
133 self.assertEqual('404.html contents', 134 self.assertEqual('404.html contents',
134 compiled_fs.GetFromFile('404.html').Get()) 135 compiled_fs.GetFromFile('404.html').Get())
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, 198 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1,
198 read_count=2, 199 read_count=2,
199 read_resolve_count=1)) 200 read_resolve_count=1))
200 future.Get() 201 future.Get()
201 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3)) 202 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3))
202 203
203 def testSkipNotFound(self): 204 def testSkipNotFound(self):
204 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA)) 205 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA))
205 compiled_fs = CompiledFileSystem.Factory( 206 compiled_fs = CompiledFileSystem.Factory(
206 ObjectStoreCreator.ForTest()).Create( 207 ObjectStoreCreator.ForTest()).Create(
207 mock_fs, lambda path, contents: contents, type(self)) 208 mock_fs, Cache(lambda path, contents: contents), type(self))
208 209
209 future = compiled_fs.GetFromFile('no_file', skip_not_found=True) 210 future = compiled_fs.GetFromFile('no_file', skip_not_found=True)
210 # If the file doesn't exist, then the file system is not read. 211 # If the file doesn't exist, then the file system is not read.
211 self.assertTrue(*mock_fs.CheckAndReset(read_count=1, stat_count=1)) 212 self.assertTrue(*mock_fs.CheckAndReset(read_count=1, stat_count=1))
212 self.assertEqual(None, future.Get()) 213 self.assertEqual(None, future.Get())
213 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1)) 214 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1))
214 future = compiled_fs.GetFromFile('no_file', skip_not_found=True) 215 future = compiled_fs.GetFromFile('no_file', skip_not_found=True)
215 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1)) 216 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1))
216 self.assertEqual(None, future.Get()) 217 self.assertEqual(None, future.Get())
217 # The result for a non-existent file should still be cached. 218 # The result for a non-existent file should still be cached.
218 self.assertTrue(*mock_fs.CheckAndReset()) 219 self.assertTrue(*mock_fs.CheckAndReset())
219 future = compiled_fs.GetFromFile('no_file') 220 future = compiled_fs.GetFromFile('no_file')
220 self.assertRaises(FileNotFoundError, future.Get) 221 self.assertRaises(FileNotFoundError, future.Get)
221 222
222 223
223 if __name__ == '__main__': 224 if __name__ == '__main__':
224 unittest.main() 225 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698