| OLD | NEW |
| 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 1288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1299 | 1299 |
| 1300 def test_policies_active_trimming(self): | 1300 def test_policies_active_trimming(self): |
| 1301 # Start with a larger cache, add many object. | 1301 # Start with a larger cache, add many object. |
| 1302 # Reload the cache with smaller policies, the cache should be trimmed on | 1302 # Reload the cache with smaller policies, the cache should be trimmed on |
| 1303 # load. | 1303 # load. |
| 1304 h_a = self.to_hash('a')[0] | 1304 h_a = self.to_hash('a')[0] |
| 1305 h_b = self.to_hash('b')[0] | 1305 h_b = self.to_hash('b')[0] |
| 1306 h_c = self.to_hash('c')[0] | 1306 h_c = self.to_hash('c')[0] |
| 1307 h_large, large = self.to_hash('b' * 99) | 1307 h_large, large = self.to_hash('b' * 99) |
| 1308 | 1308 |
| 1309 def assertItems(expected): |
| 1310 actual = [ |
| 1311 (digest, size) for digest, (size, _) in cache._lru._items.iteritems()] |
| 1312 self.assertEqual(expected, actual) |
| 1313 |
| 1309 # Max policies is 100 bytes, 2 items, 1000 bytes free space. | 1314 # Max policies is 100 bytes, 2 items, 1000 bytes free space. |
| 1310 self._free_disk = 1101 | 1315 self._free_disk = 1101 |
| 1311 with self.get_cache() as cache: | 1316 with self.get_cache() as cache: |
| 1312 cache.write(h_a, 'a') | 1317 cache.write(h_a, 'a') |
| 1313 cache.write(h_large, large) | 1318 cache.write(h_large, large) |
| 1314 # Cache (size and # items) is not enforced while adding items. The | 1319 # 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 | 1320 # 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. | 1321 # cache policies. As long as there is free space, this is fine. |
| 1317 cache.write(h_b, 'b') | 1322 cache.write(h_b, 'b') |
| 1318 expected = sorted(((h_a, 1), (h_large, len(large)), (h_b, 1))) | 1323 assertItems([(h_a, 1), (h_large, len(large)), (h_b, 1)]) |
| 1319 self.assertEqual(expected, sorted(cache._lru._items.iteritems())) | |
| 1320 self.assertEqual(h_a, cache._protected) | 1324 self.assertEqual(h_a, cache._protected) |
| 1321 self.assertEqual(1000, cache._free_disk) | 1325 self.assertEqual(1000, cache._free_disk) |
| 1322 self.assertEqual(0, cache.initial_number_items) | 1326 self.assertEqual(0, cache.initial_number_items) |
| 1323 self.assertEqual(0, cache.initial_size) | 1327 self.assertEqual(0, cache.initial_size) |
| 1324 # Free disk is enforced, because otherwise we assume the task wouldn't | 1328 # 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 | 1329 # 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. | 1330 # are protected. The item is added since it's detected after the fact. |
| 1327 with self.assertRaises(isolateserver.Error): | 1331 with self.assertRaises(isolateserver.Error): |
| 1328 cache.write(h_c, 'c') | 1332 cache.write(h_c, 'c') |
| 1329 | 1333 |
| 1330 # At this point, after the implicit trim in __exit__(), h_a and h_large were | 1334 # At this point, after the implicit trim in __exit__(), h_a and h_large were |
| 1331 # evicted. | 1335 # evicted. |
| 1332 self.assertEqual( | 1336 self.assertEqual( |
| 1333 sorted([h_b, h_c, u'state.json']), sorted(os.listdir(self.tempdir))) | 1337 sorted([h_b, h_c, u'state.json']), sorted(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 sorted([h_b, h_c, h_large, u'state.json']), |
| 1344 sorted(os.listdir(self.tempdir))) | 1348 sorted(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 assertItems([(h_c, 1), (h_large, len(large))]) |
| 1350 self.assertEqual(expected, cache._lru._items) | |
| 1351 self.assertEqual(None, cache._protected) | 1354 self.assertEqual(None, cache._protected) |
| 1352 self.assertEqual(1101, cache._free_disk) | 1355 self.assertEqual(1101, cache._free_disk) |
| 1353 self.assertEqual(2, cache.initial_number_items) | 1356 self.assertEqual(2, cache.initial_number_items) |
| 1354 self.assertEqual(100, cache.initial_size) | 1357 self.assertEqual(100, cache.initial_size) |
| 1355 | 1358 |
| 1356 | 1359 |
| 1357 def clear_env_vars(): | 1360 def clear_env_vars(): |
| 1358 for e in ('ISOLATE_DEBUG', 'ISOLATE_SERVER'): | 1361 for e in ('ISOLATE_DEBUG', 'ISOLATE_SERVER'): |
| 1359 os.environ.pop(e, None) | 1362 os.environ.pop(e, None) |
| 1360 | 1363 |
| 1361 | 1364 |
| 1362 if __name__ == '__main__': | 1365 if __name__ == '__main__': |
| 1363 fix_encoding.fix_encoding() | 1366 fix_encoding.fix_encoding() |
| 1364 if '-v' in sys.argv: | 1367 if '-v' in sys.argv: |
| 1365 unittest.TestCase.maxDiff = None | 1368 unittest.TestCase.maxDiff = None |
| 1366 logging.basicConfig( | 1369 logging.basicConfig( |
| 1367 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) | 1370 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) |
| 1368 clear_env_vars() | 1371 clear_env_vars() |
| 1369 unittest.main() | 1372 unittest.main() |
| OLD | NEW |