-
Notifications
You must be signed in to change notification settings - Fork 0
/
evicter_lfu_test.go
50 lines (32 loc) · 1.03 KB
/
evicter_lfu_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gocached
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLFUEvicter(t *testing.T) {
ctx := context.Background()
lfu := NewLFUEvicter[string]()
lfu.Promote(ctx, "key3", 1)
lfu.Promote(ctx, "key3", 1)
lfu.Promote(ctx, "key3", 1)
lfu.Promote(ctx, "key2", 1)
lfu.Promote(ctx, "key2", 1)
lfu.Promote(ctx, "key1", 1)
assert.Equal(t, []string{"key1"}, lfu.Evictees(ctx, 1))
lfu.Promote(ctx, "key1", 5)
assert.Equal(t, []string{"key2"}, lfu.Evictees(ctx, 1))
lfu.Demote(ctx, "key1", 1)
lfu.Demote(ctx, "key2", 1)
lfu.Demote(ctx, "key3", 1)
assert.Equal(t, []string{"key2"}, lfu.Evictees(ctx, 1))
lfu.Demote(ctx, "key3", 3)
lfu.Demote(ctx, "key2", 1)
assert.Equal(t, []string{"key3"}, lfu.Evictees(ctx, 1))
assert.ElementsMatch(t, []string{"key3", "key2"}, lfu.Evictees(ctx, 2))
lfu.Evict(ctx, "key3")
assert.ElementsMatch(t, []string{"key2", "key1"}, lfu.Evictees(ctx, 2))
lfu.Evict(ctx, "key1")
lfu.Evict(ctx, "key2")
assert.ElementsMatch(t, []string{}, lfu.Evictees(ctx, 2))
}