-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_cache_test.go
83 lines (70 loc) · 2.28 KB
/
shard_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gcache
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func defaultShardConfig() ConfigShardCacheInterface {
r := ConfigMessage{
SizeLimit: 20000,
DefaultExpiration: -1,
IsKeepUsefull: true,
CacheType: ConfigMessage_RWL,
ShardCount: 10,
}
return &r
}
func getCacheGorBase() *ShardCache {
gen := func(config ConfigCacheInterface) Cacher {
return NewRwCache(config)
}
c := NewShardCache(defaultShardConfig(), gen, calcSUM)
return c
}
func TestShardCache_Purge(t *testing.T) {
as := assert.New(t)
c := getCacheGorBase()
c.SetOrUpdate("first", []byte(`zaza`), DefaultExpirationMarker)
as.Equal(int64(1), c.Statistic().ItemsCount)
as.Equal(int64(1), c.Statistic().SetOrReplaceCount)
c.Purge()
as.Equal(int64(0), c.Statistic().ItemsCount)
as.Equal(int64(1), c.Statistic().DeleteCount)
c.Dead() //Cleanup
}
func TestShardCache_Get(t *testing.T) {
as := assert.New(t)
c := getCacheGorBase()
c.SetOrUpdate("first", []byte(`zaza`), DefaultExpirationMarker)
c.SetOrUpdate("second", []byte(`azaz`), DefaultExpirationMarker)
as.Nil(c.Get("irst"))
as.Equal(int64(2), c.Statistic().ItemsCount)
as.Equal([]byte(`zaza`), c.Get("first"))
as.Equal([]byte(`azaz`), c.Get("second"))
as.Equal(int64(2), c.Statistic().GetSuccessNumber)
as.Equal(int64(1), c.Statistic().GetErrorNumber)
as.Equal(int64(2), c.Statistic().ItemsCount)
c.Dead() //Cleanup
}
func TestShardCache_SetOrUpdate(t *testing.T) {
as := assert.New(t)
c := getCacheGorBase()
c.SetOrUpdate("first", []byte(`zaza`), (DefaultExpirationMarker))
c.SetOrUpdate("second", []byte(`azaz`), (DefaultExpirationMarker))
as.Nil(c.Get("irst"))
time.Sleep(100)
as.Equal(int64(2), c.Statistic().ItemsCount)
as.Equal(int64(2), c.Statistic().SetOrReplaceCount)
as.Equal(int64(1), c.Statistic().GetErrorNumber)
as.Equal([]byte(`zaza`), c.Get("first"))
as.Equal([]byte(`azaz`), c.Get("second"))
c.SetOrUpdate("second", []byte(`zara`), (DefaultExpirationMarker))
as.Equal(int64(2), c.Statistic().ItemsCount)
as.Equal([]byte(`zara`), c.Get("second"))
time.Sleep(100)
as.Equal(int64(3), c.Statistic().SetOrReplaceCount)
as.Equal(int64(3), c.Statistic().GetSuccessNumber)
as.Equal(int64(1), c.Statistic().GetErrorNumber)
as.Equal(int64(2), c.Statistic().ItemsCount)
c.Dead() //Cleanup
}