Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: convert the example in issue #300 into a test case #302

Merged
merged 9 commits into from
Dec 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,3 +1150,40 @@ func TestCache_RepeatedSetWithBiggerEntry(t *testing.T) {
}

}

// TestBigCache_allocateAdditionalMemoryLeadPanic
// The new commit 16df11e change the encoding method,it can fix issue #300
func TestBigCache_allocateAdditionalMemoryLeadPanic(t *testing.T) {
t.Parallel()
clock := mockedClock{value: 0}
cache, _ := newBigCache(Config{
Shards: 1,
LifeWindow: 3 * time.Second,
MaxEntrySize: 52,
}, &clock)
ts := time.Now().Unix()
clock.set(ts)
cache.Set("a", blob(0xff, 235))
ts += 2
clock.set(ts)
cache.Set("b", blob(0xff, 235))
// expire the key "a"
ts += 2
clock.set(ts)
// move tail to leftMargin,insert before head
cache.Set("c", blob(0xff, 108))
// reallocate memory,fill the tail to head with zero byte,move head to leftMargin
cache.Set("d", blob(0xff, 1024))
ts += 4
clock.set(ts)
// expire the key "c"
cache.Set("e", blob(0xff, 3))
// expire the zero bytes
cache.Set("f", blob(0xff, 3))
// expire the key "b"
cache.Set("g", blob(0xff, 3))
_, err := cache.Get("b")
assertEqual(t, err, ErrEntryNotFound)
data, _ := cache.Get("g")
assertEqual(t, []byte{0xff, 0xff, 0xff}, data)
}