Skip to content

Commit 77bbae4

Browse files
authored
test: convert the example in issue #300 into a test case (#302)
1 parent be11930 commit 77bbae4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

bigcache_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,40 @@ func TestCache_RepeatedSetWithBiggerEntry(t *testing.T) {
11501150
}
11511151

11521152
}
1153+
1154+
// TestBigCache_allocateAdditionalMemoryLeadPanic
1155+
// The new commit 16df11e change the encoding method,it can fix issue #300
1156+
func TestBigCache_allocateAdditionalMemoryLeadPanic(t *testing.T) {
1157+
t.Parallel()
1158+
clock := mockedClock{value: 0}
1159+
cache, _ := newBigCache(Config{
1160+
Shards: 1,
1161+
LifeWindow: 3 * time.Second,
1162+
MaxEntrySize: 52,
1163+
}, &clock)
1164+
ts := time.Now().Unix()
1165+
clock.set(ts)
1166+
cache.Set("a", blob(0xff, 235))
1167+
ts += 2
1168+
clock.set(ts)
1169+
cache.Set("b", blob(0xff, 235))
1170+
// expire the key "a"
1171+
ts += 2
1172+
clock.set(ts)
1173+
// move tail to leftMargin,insert before head
1174+
cache.Set("c", blob(0xff, 108))
1175+
// reallocate memory,fill the tail to head with zero byte,move head to leftMargin
1176+
cache.Set("d", blob(0xff, 1024))
1177+
ts += 4
1178+
clock.set(ts)
1179+
// expire the key "c"
1180+
cache.Set("e", blob(0xff, 3))
1181+
// expire the zero bytes
1182+
cache.Set("f", blob(0xff, 3))
1183+
// expire the key "b"
1184+
cache.Set("g", blob(0xff, 3))
1185+
_, err := cache.Get("b")
1186+
assertEqual(t, err, ErrEntryNotFound)
1187+
data, _ := cache.Get("g")
1188+
assertEqual(t, []byte{0xff, 0xff, 0xff}, data)
1189+
}

0 commit comments

Comments
 (0)