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

Side by Side Diff: client/tests/isolateserver_test.py

Issue 2414543003: isolateserver: DiskCache format v2 (Closed)
Patch Set: docs Created 4 years, 2 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
« client/isolateserver.py ('K') | « client/isolateserver.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2013 The LUCI Authors. All rights reserved. 2 # Copyright 2013 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file. 4 # that can be found in the LICENSE file.
5 5
6 # pylint: disable=W0212,W0223,W0231,W0613 6 # pylint: disable=W0212,W0223,W0231,W0613
7 7
8 import base64 8 import base64
9 import collections 9 import collections
10 import hashlib 10 import hashlib
(...skipping 1230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1241 # Max: 100 bytes, 2 items 1241 # Max: 100 bytes, 2 items
1242 # Min free disk: 1000 bytes. 1242 # Min free disk: 1000 bytes.
1243 self._policies = isolateserver.CachePolicies(100, 1000, 2) 1243 self._policies = isolateserver.CachePolicies(100, 1000, 2)
1244 def get_free_space(p): 1244 def get_free_space(p):
1245 self.assertEqual(p, self.tempdir) 1245 self.assertEqual(p, self.tempdir)
1246 return self._free_disk 1246 return self._free_disk
1247 self.mock(file_path, 'get_free_space', get_free_space) 1247 self.mock(file_path, 'get_free_space', get_free_space)
1248 # TODO(maruel): Test the following. 1248 # TODO(maruel): Test the following.
1249 #cache.touch() 1249 #cache.touch()
1250 1250
1251 self.now = 0.0
1252
1251 def get_cache(self): 1253 def get_cache(self):
1252 return isolateserver.DiskCache(self.tempdir, self._policies, self._algo) 1254 return isolateserver.DiskCache(
1255 self.tempdir, self._policies, self._algo, time_fn=lambda: self.now)
1253 1256
1254 def to_hash(self, content): 1257 def to_hash(self, content):
1255 return self._algo(content).hexdigest(), content 1258 return self._algo(content).hexdigest(), content
1256 1259
1257 def test_read_evict(self): 1260 def test_read_evict(self):
1258 self._free_disk = 1100 1261 self._free_disk = 1100
1259 h_a = self.to_hash('a')[0] 1262 h_a = self.to_hash('a')[0]
1260 with self.get_cache() as cache: 1263 with self.get_cache() as cache:
1261 cache.write(h_a, 'a') 1264 cache.write(h_a, 'a')
1262 with cache.getfileobj(h_a) as f: 1265 with cache.getfileobj(h_a) as f:
(...skipping 23 matching lines...) Expand all
1286 with self.assertRaises(isolateserver.Error): 1289 with self.assertRaises(isolateserver.Error):
1287 cache.write(*self.to_hash('e')) 1290 cache.write(*self.to_hash('e'))
1288 1291
1289 def test_cleanup(self): 1292 def test_cleanup(self):
1290 # Inject an item without a state.json. It will be deleted on cleanup. 1293 # Inject an item without a state.json. It will be deleted on cleanup.
1291 h_a = self.to_hash('a')[0] 1294 h_a = self.to_hash('a')[0]
1292 isolateserver.file_write(os.path.join(self.tempdir, h_a), 'a') 1295 isolateserver.file_write(os.path.join(self.tempdir, h_a), 'a')
1293 cache = self.get_cache() 1296 cache = self.get_cache()
1294 self.assertEqual([], sorted(cache._lru._items.iteritems())) 1297 self.assertEqual([], sorted(cache._lru._items.iteritems()))
1295 self.assertEqual( 1298 self.assertEqual(
1296 sorted([h_a, u'state.json']), sorted(os.listdir(self.tempdir))) 1299 sorted([h_a, u'state.json', u'VERSION']), sorted(os.listdir(self.tempdir )))
1297 cache.cleanup() 1300 cache.cleanup()
1298 self.assertEqual([u'state.json'], os.listdir(self.tempdir)) 1301 self.assertEqual([u'state.json', u'VERSION'], os.listdir(self.tempdir))
1299 1302
1300 def test_policies_active_trimming(self): 1303 def test_policies_active_trimming(self):
1301 # Start with a larger cache, add many object. 1304 # Start with a larger cache, add many object.
1302 # Reload the cache with smaller policies, the cache should be trimmed on 1305 # Reload the cache with smaller policies, the cache should be trimmed on
1303 # load. 1306 # load.
1304 h_a = self.to_hash('a')[0] 1307 h_a = self.to_hash('a')[0]
1305 h_b = self.to_hash('b')[0] 1308 h_b = self.to_hash('b')[0]
1306 h_c = self.to_hash('c')[0] 1309 h_c = self.to_hash('c')[0]
1307 h_large, large = self.to_hash('b' * 99) 1310 h_large, large = self.to_hash('b' * 99)
1308 1311
1309 # Max policies is 100 bytes, 2 items, 1000 bytes free space. 1312 # Max policies is 100 bytes, 2 items, 1000 bytes free space.
1310 self._free_disk = 1101 1313 self._free_disk = 1101
1311 with self.get_cache() as cache: 1314 with self.get_cache() as cache:
1312 cache.write(h_a, 'a') 1315 cache.write(h_a, 'a')
1313 cache.write(h_large, large) 1316 cache.write(h_large, large)
1314 # Cache (size and # items) is not enforced while adding items. The 1317 # Cache (size and # items) is not enforced while adding items. The
1315 # rationale is that a task may request more data than the size of the 1318 # rationale is that a task may request more data than the size of the
1316 # cache policies. As long as there is free space, this is fine. 1319 # cache policies. As long as there is free space, this is fine.
1317 cache.write(h_b, 'b') 1320 cache.write(h_b, 'b')
1318 expected = sorted(((h_a, 1), (h_large, len(large)), (h_b, 1))) 1321 expected = sorted(((h_a, 1), (h_large, len(large)), (h_b, 1)))
1319 self.assertEqual(expected, sorted(cache._lru._items.iteritems())) 1322 self.assertEqual(expected, sorted(cache._sizes()))
1320 self.assertEqual(h_a, cache._protected) 1323 self.assertEqual(h_a, cache._protected)
1321 self.assertEqual(1000, cache._free_disk) 1324 self.assertEqual(1000, cache._free_disk)
1322 self.assertEqual(0, cache.initial_number_items) 1325 self.assertEqual(0, cache.initial_number_items)
1323 self.assertEqual(0, cache.initial_size) 1326 self.assertEqual(0, cache.initial_size)
1324 # Free disk is enforced, because otherwise we assume the task wouldn't 1327 # Free disk is enforced, because otherwise we assume the task wouldn't
1325 # be able to start. In this case, it throws an exception since all items 1328 # be able to start. In this case, it throws an exception since all items
1326 # are protected. The item is added since it's detected after the fact. 1329 # are protected. The item is added since it's detected after the fact.
1327 with self.assertRaises(isolateserver.Error): 1330 with self.assertRaises(isolateserver.Error):
1328 cache.write(h_c, 'c') 1331 cache.write(h_c, 'c')
1329 1332
1330 # At this point, after the implicit trim in __exit__(), h_a and h_large were 1333 # At this point, after the implicit trim in __exit__(), h_a and h_large were
1331 # evicted. 1334 # evicted.
1332 self.assertEqual( 1335 self.assertEqual(
1333 sorted([h_b, h_c, u'state.json']), sorted(os.listdir(self.tempdir))) 1336 set([h_b[:2], h_c[:2], u'state.json', u'VERSION']),
1337 set(os.listdir(self.tempdir)))
1334 1338
1335 # Allow 3 items and 101 bytes so h_large is kept. 1339 # Allow 3 items and 101 bytes so h_large is kept.
1336 self._policies = isolateserver.CachePolicies(101, 1000, 3) 1340 self._policies = isolateserver.CachePolicies(101, 1000, 3)
1337 with self.get_cache() as cache: 1341 with self.get_cache() as cache:
1338 cache.write(h_large, large) 1342 cache.write(h_large, large)
1339 self.assertEqual(2, cache.initial_number_items) 1343 self.assertEqual(2, cache.initial_number_items)
1340 self.assertEqual(2, cache.initial_size) 1344 self.assertEqual(2, cache.initial_size)
1341 1345
1342 self.assertEqual( 1346 self.assertEqual(
1343 sorted([h_b, h_c, h_large, u'state.json']), 1347 set([h_b[:2], h_c[:2], h_large[:2], u'state.json', u'VERSION']),
1344 sorted(os.listdir(self.tempdir))) 1348 set(os.listdir(self.tempdir)))
1345 1349
1346 # Assert that trimming is done in constructor too. 1350 # Assert that trimming is done in constructor too.
1347 self._policies = isolateserver.CachePolicies(100, 1000, 2) 1351 self._policies = isolateserver.CachePolicies(100, 1000, 2)
1348 with self.get_cache() as cache: 1352 with self.get_cache() as cache:
1349 expected = collections.OrderedDict([(h_c, 1), (h_large, len(large))]) 1353 self.assertEqual(
1350 self.assertEqual(expected, cache._lru._items) 1354 [(h_c, 1), (h_large, len(large))],
1355 list(cache._sizes()))
1351 self.assertEqual(None, cache._protected) 1356 self.assertEqual(None, cache._protected)
1352 self.assertEqual(1101, cache._free_disk) 1357 self.assertEqual(1101, cache._free_disk)
1353 self.assertEqual(2, cache.initial_number_items) 1358 self.assertEqual(2, cache.initial_number_items)
1354 self.assertEqual(100, cache.initial_size) 1359 self.assertEqual(100, cache.initial_size)
1355 1360
1361 def test_timestamp(self):
1362 self._free_disk = 1100
1363 cache = self.get_cache()
1364 with cache:
1365 self.now = 1
1366 cache.write('deadbeef', '1')
1367
1368 self.now = 2
1369 cache.write('badcoffee', '2')
1370
1371 self.assertEqual(
1372 [
1373 ('deadbeef', [1, 1]),
1374 ('badcoffee', [1, 2]),
1375 ],
1376 cache._lru._items.items()
1377 )
1378
1379 loaded = self.get_cache()
1380 self.assertEqual(cache._lru._items, loaded._lru._items)
1381
1356 1382
1357 def clear_env_vars(): 1383 def clear_env_vars():
1358 for e in ('ISOLATE_DEBUG', 'ISOLATE_SERVER'): 1384 for e in ('ISOLATE_DEBUG', 'ISOLATE_SERVER'):
1359 os.environ.pop(e, None) 1385 os.environ.pop(e, None)
1360 1386
1361 1387
1362 if __name__ == '__main__': 1388 if __name__ == '__main__':
1363 fix_encoding.fix_encoding() 1389 fix_encoding.fix_encoding()
1364 if '-v' in sys.argv: 1390 if '-v' in sys.argv:
1365 unittest.TestCase.maxDiff = None 1391 unittest.TestCase.maxDiff = None
1366 logging.basicConfig( 1392 logging.basicConfig(
1367 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) 1393 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL))
1368 clear_env_vars() 1394 clear_env_vars()
1369 unittest.main() 1395 unittest.main()
OLDNEW
« client/isolateserver.py ('K') | « client/isolateserver.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698