-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlocal_cache_test.go
59 lines (51 loc) · 1.89 KB
/
local_cache_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
53
54
55
56
57
58
59
package beeorm
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLocalCache(t *testing.T) {
registry := NewRegistry()
registry.RegisterLocalCache(DefaultPoolCode, 0)
registry.RegisterLocalCache("with_limit", 3)
validatedRegistry, err := registry.Validate()
assert.Nil(t, err)
orm := validatedRegistry.NewORM(context.Background())
testLogger := &MockLogHandler{}
orm.RegisterQueryLogger(testLogger, false, false, true)
testQueryLog := &MockLogHandler{}
orm.RegisterQueryLogger(testQueryLog, false, false, true)
lc := orm.Engine().LocalCache(DefaultPoolCode)
assert.Equal(t, DefaultPoolCode, lc.GetConfig().GetCode())
assert.Equal(t, 0, lc.GetConfig().GetLimit())
val, has := lc.Get(orm, "test_get")
assert.False(t, has)
assert.Nil(t, val)
lc.Set(orm, "test_get", "hello")
val, has = lc.Get(orm, "test_get")
assert.True(t, has)
assert.Equal(t, "hello", val)
lcLimit := orm.Engine().LocalCache("with_limit")
assert.Equal(t, "with_limit", lcLimit.GetConfig().GetCode())
assert.Equal(t, 3, lcLimit.GetConfig().GetLimit())
lcLimit.Set(orm, "1", "1")
lcLimit.Set(orm, "2", "2")
lcLimit.Set(orm, "3", "3")
assert.Equal(t, uint64(3), lcLimit.GetUsage()[0].Used)
assert.Equal(t, uint64(0), lcLimit.GetUsage()[0].Evictions)
lcLimit.Set(orm, "1", "1")
assert.Equal(t, uint64(3), lcLimit.GetUsage()[0].Used)
assert.Equal(t, uint64(0), lcLimit.GetUsage()[0].Evictions)
lcLimit.Set(orm, "4", "4")
assert.Equal(t, uint64(3), lcLimit.GetUsage()[0].Used)
assert.Equal(t, uint64(1), lcLimit.GetUsage()[0].Evictions)
_, found := lcLimit.Get(orm, "4")
assert.True(t, found)
orm = validatedRegistry.NewORM(context.Background())
orm.RegisterQueryLogger(testLogger, false, false, true)
orm.RegisterQueryLogger(testQueryLog, false, false, true)
lc = orm.Engine().LocalCache(DefaultPoolCode)
val, has = lc.Get(orm, "test_get")
assert.True(t, has)
assert.Equal(t, "hello", val)
}