-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_test.go
52 lines (41 loc) · 1.08 KB
/
policy_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
51
52
package mcache
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCache(t *testing.T) {
t.Run("simple cache", runCachePolicy[SimpleCache])
t.Run("lfu cache", runCachePolicy[LfuCache])
t.Run("lru cache", runCachePolicy[LruCache])
t.Run("arc cache", runCachePolicy[ArcCache])
}
func runCachePolicy[T any, P CachePolicy[T]](t *testing.T) {
var (
ctx = context.TODO()
fc = NewFakeClock()
cc = P(new(T))
)
cc.Init(fc, 5)
val, err := cc.Get(ctx, "key")
assert.NotNil(t, err)
cc.Set(ctx, "key", "value", 0)
val, err = cc.Get(ctx, "key")
assert.Equal(t, "value", val)
cc.Set(ctx, "k", "v", 100*time.Millisecond)
val, _ = cc.Get(ctx, "k")
assert.Equal(t, "v", val)
fc.Advance(101 * time.Millisecond)
assert.True(t, cc.Exists(ctx, "key"))
assert.False(t, cc.Exists(ctx, "k"))
val, err = cc.Get(ctx, "k")
assert.NotNil(t, err)
assert.Nil(t, val)
assert.True(t, cc.Remove(ctx, "key"))
assert.False(t, cc.Remove(ctx, "k"))
cc.Init(fc, 1)
assert.Nil(t, cc.Set(ctx, "ak", "av", 0))
cc.Evict(ctx, 1)
assert.False(t, cc.Exists(ctx, "ak"))
}