@@ -848,7 +848,7 @@ class TestLRUStoreCache(StoreTests, unittest.TestCase):
848
848
def create_store (self ):
849
849
return LRUStoreCache (dict (), max_size = 2 ** 27 )
850
850
851
- def test_cache_values (self ):
851
+ def test_cache_values_no_max_size (self ):
852
852
853
853
# setup store
854
854
store = CountingDict ()
@@ -903,6 +903,102 @@ def test_cache_values(self):
903
903
assert 1 == store .counter ['__setitem__' , 'bar' ]
904
904
905
905
# TODO test max size
906
+ def test_cache_values_with_max_size (self ):
907
+
908
+ # setup store
909
+ store = CountingDict ()
910
+ store ['foo' ] = b'xxx'
911
+ store ['bar' ] = b'yyy'
912
+ assert 0 == store .counter ['__getitem__' , 'foo' ]
913
+ assert 0 == store .counter ['__getitem__' , 'bar' ]
914
+ # setup cache - can only hold one item
915
+ cache = LRUStoreCache (store , max_size = 5 )
916
+ assert 0 == cache .hits
917
+ assert 0 == cache .misses
918
+
919
+ # test first 'foo' __getitem__, cache miss
920
+ assert b'xxx' == cache ['foo' ]
921
+ assert 1 == store .counter ['__getitem__' , 'foo' ]
922
+ assert 0 == cache .hits
923
+ assert 1 == cache .misses
924
+
925
+ # test second 'foo' __getitem__, cache hit
926
+ assert b'xxx' == cache ['foo' ]
927
+ assert 1 == store .counter ['__getitem__' , 'foo' ]
928
+ assert 1 == cache .hits
929
+ assert 1 == cache .misses
930
+
931
+ # test first 'bar' __getitem__, cache miss
932
+ assert b'yyy' == cache ['bar' ]
933
+ assert 1 == store .counter ['__getitem__' , 'bar' ]
934
+ assert 1 == cache .hits
935
+ assert 2 == cache .misses
936
+
937
+ # test second 'bar' __getitem__, cache hit
938
+ assert b'yyy' == cache ['bar' ]
939
+ assert 1 == store .counter ['__getitem__' , 'bar' ]
940
+ assert 2 == cache .hits
941
+ assert 2 == cache .misses
942
+
943
+ # test 'foo' __getitem__, should have been evicted, cache miss
944
+ assert b'xxx' == cache ['foo' ]
945
+ assert 2 == store .counter ['__getitem__' , 'foo' ]
946
+ assert 2 == cache .hits
947
+ assert 3 == cache .misses
948
+
949
+ # test 'bar' __getitem__, should have been evicted, cache miss
950
+ assert b'yyy' == cache ['bar' ]
951
+ assert 2 == store .counter ['__getitem__' , 'bar' ]
952
+ assert 2 == cache .hits
953
+ assert 4 == cache .misses
954
+
955
+ # setup store
956
+ store = CountingDict ()
957
+ store ['foo' ] = b'xxx'
958
+ store ['bar' ] = b'yyy'
959
+ assert 0 == store .counter ['__getitem__' , 'foo' ]
960
+ assert 0 == store .counter ['__getitem__' , 'bar' ]
961
+ # setup cache - can hold two items
962
+ cache = LRUStoreCache (store , max_size = 6 )
963
+ assert 0 == cache .hits
964
+ assert 0 == cache .misses
965
+
966
+ # test first 'foo' __getitem__, cache miss
967
+ assert b'xxx' == cache ['foo' ]
968
+ assert 1 == store .counter ['__getitem__' , 'foo' ]
969
+ assert 0 == cache .hits
970
+ assert 1 == cache .misses
971
+
972
+ # test second 'foo' __getitem__, cache hit
973
+ assert b'xxx' == cache ['foo' ]
974
+ assert 1 == store .counter ['__getitem__' , 'foo' ]
975
+ assert 1 == cache .hits
976
+ assert 1 == cache .misses
977
+
978
+ # test first 'bar' __getitem__, cache miss
979
+ assert b'yyy' == cache ['bar' ]
980
+ assert 1 == store .counter ['__getitem__' , 'bar' ]
981
+ assert 1 == cache .hits
982
+ assert 2 == cache .misses
983
+
984
+ # test second 'bar' __getitem__, cache hit
985
+ assert b'yyy' == cache ['bar' ]
986
+ assert 1 == store .counter ['__getitem__' , 'bar' ]
987
+ assert 2 == cache .hits
988
+ assert 2 == cache .misses
989
+
990
+ # test 'foo' __getitem__, should still be cached
991
+ assert b'xxx' == cache ['foo' ]
992
+ assert 1 == store .counter ['__getitem__' , 'foo' ]
993
+ assert 3 == cache .hits
994
+ assert 2 == cache .misses
995
+
996
+ # test 'bar' __getitem__, should still be cached
997
+ assert b'yyy' == cache ['bar' ]
998
+ assert 1 == store .counter ['__getitem__' , 'bar' ]
999
+ assert 4 == cache .hits
1000
+ assert 2 == cache .misses
1001
+
906
1002
# TODO test key caching
907
1003
908
1004
0 commit comments