(t *testing.T)
| 1050 | } |
| 1051 | |
| 1052 | func TestImmediateRevCacheItemBasedEviction(t *testing.T) { |
| 1053 | testCases := []struct { |
| 1054 | name string |
| 1055 | UseCVCache bool |
| 1056 | }{ |
| 1057 | { |
| 1058 | name: "Rev cache pathway", |
| 1059 | UseCVCache: false, |
| 1060 | }, |
| 1061 | { |
| 1062 | name: "CV cache pathway", |
| 1063 | UseCVCache: true, |
| 1064 | }, |
| 1065 | } |
| 1066 | for _, testCase := range testCases { |
| 1067 | t.Run(testCase.name, func(t *testing.T) { |
| 1068 | cacheHitCounter, cacheMissCounter, getDocumentCounter, getRevisionCounter, cacheNumItems, memoryBytesCounted := base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{} |
| 1069 | backingStoreMap := CreateTestSingleBackingStoreMap(&testBackingStore{nil, &getDocumentCounter, &getRevisionCounter}, testCollectionID) |
| 1070 | cacheOptions := &RevisionCacheOptions{ |
| 1071 | MaxItemCount: 1, |
| 1072 | MaxBytes: 0, // turn off memory based eviction |
| 1073 | } |
| 1074 | ctx := base.TestCtx(t) |
| 1075 | var err error |
| 1076 | |
| 1077 | cache := NewLRURevisionCache(cacheOptions, backingStoreMap, &cacheHitCounter, &cacheMissCounter, &cacheNumItems, &memoryBytesCounted) |
| 1078 | // load up item to hit max capacity |
| 1079 | cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{"some":"test"}`), DocID: "doc1", RevID: "1-abc", CV: &Version{Value: 123, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID) |
| 1080 | |
| 1081 | // eviction starts from here in test |
| 1082 | cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{"some":"test"}`), DocID: "newDoc", RevID: "1-abc", CV: &Version{Value: 123, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID) |
| 1083 | |
| 1084 | assert.Equal(t, int64(15), memoryBytesCounted.Value()) |
| 1085 | assert.Equal(t, int64(1), cacheNumItems.Value()) |
| 1086 | |
| 1087 | cache.Upsert(ctx, DocumentRevision{BodyBytes: []byte(`{"some":"test"}`), DocID: "doc2", RevID: "1-abc", CV: &Version{Value: 123, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID) |
| 1088 | |
| 1089 | assert.Equal(t, int64(15), memoryBytesCounted.Value()) |
| 1090 | assert.Equal(t, int64(1), cacheNumItems.Value()) |
| 1091 | |
| 1092 | var docRev DocumentRevision |
| 1093 | if testCase.UseCVCache { |
| 1094 | docRev, err = cache.GetWithCV(ctx, "doc3", &Version{Value: 123, SourceID: "test"}, testCollectionID, RevCacheOmitDelta, false) |
| 1095 | require.NoError(t, err) |
| 1096 | } else { |
| 1097 | docRev, err = cache.GetWithRev(ctx, "doc3", "1-abc", testCollectionID, RevCacheOmitDelta) |
| 1098 | require.NoError(t, err) |
| 1099 | } |
| 1100 | assert.NotNil(t, docRev.BodyBytes) |
| 1101 | |
| 1102 | assert.Equal(t, int64(118), memoryBytesCounted.Value()) |
| 1103 | assert.Equal(t, int64(1), cacheNumItems.Value()) |
| 1104 | |
| 1105 | docRev, err = cache.GetActive(ctx, "doc4", testCollectionID) |
| 1106 | require.NoError(t, err) |
| 1107 | assert.NotNil(t, docRev.BodyBytes) |
| 1108 | |
| 1109 | assert.Equal(t, int64(118), memoryBytesCounted.Value()) |
nothing calls this directly
no test coverage detected