forked from viney-shih/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
171 lines (146 loc) · 5.26 KB
/
event_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cache
import (
"context"
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/suite"
)
const (
mockEventPfx = "event-pfx"
mockEventKey = "event-key"
mockEventUUID = "event-from-others"
)
var (
mockEventCTX = context.Background()
)
type eventSuite struct {
suite.Suite
factory *factory
rds *rds
lfu *tinyLFU
ring *redis.Ring
mb *messageBroker
}
func (s *eventSuite) SetupSuite() {
s.ring = redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"server1": ":6379",
},
})
}
func (s *eventSuite) TearDownSuite() {}
func (s *eventSuite) SetupTest() {
s.rds = NewRedis(s.ring).(*rds)
s.lfu = NewTinyLFU(10000).(*tinyLFU)
s.mb = newMessageBroker(mockEventUUID, s.rds)
s.factory = NewFactory(s.rds, s.lfu, WithPubSub(s.rds)).(*factory)
}
func (s *eventSuite) TearDownTest() {
// prevent registering twice
ClearPrefix()
// flush redis
_ = s.ring.ForEachShard(mockCacheCTX, func(ctx context.Context, client *redis.Client) error {
return client.FlushDB(ctx).Err()
})
s.mb.close() // this makes sure only one (mb or factory) can trigger Close() once without panic
s.factory.Close()
}
func TestEventSuite(t *testing.T) {
suite.Run(t, new(eventSuite))
}
func (s *eventSuite) TestSubscribedEventsHandlerWithSet() {
c := s.factory.NewCache([]Setting{
{
Prefix: mockEventPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})
// Set() will trigger eviction in other machines
s.Require().NoError(c.Set(mockEventCTX, mockEventPfx, mockEventKey, 100))
time.Sleep(time.Millisecond * 100)
val, err := s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val) // make sure the local value existed without impacted
// trigger invalid event type, ignore it directly
// TODO: handling error messages forwarding in the future
s.Require().NoError(s.mb.send(mockEventCTX, event{Type: EventTypeNone}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val)
// trigger evict event without keys, nothing happened
s.Require().NoError(s.mb.send(mockEventCTX, event{
Type: EventTypeEvict,
Body: eventBody{Keys: []string{}},
}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val)
// simulate eviction from other machines
s.Require().NoError(s.mb.send(mockEventCTX, event{
Type: EventTypeEvict,
Body: eventBody{Keys: []string{getCacheKey(mockEventPfx, mockEventKey)}},
}))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{}}, val) // local value evicted
}
func (s *eventSuite) TestSubscribedEventsHandlerWithDel() {
c := s.factory.NewCache([]Setting{
{
Prefix: mockEventPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})
// Set() will trigger eviction in other machines
s.Require().NoError(c.Set(mockEventCTX, mockEventPfx, mockEventKey, 100))
time.Sleep(time.Millisecond * 100)
val, err := s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val) // make sure the local value existed without impacted
// Del is the same behavior as Set(), but the value is killed by itself.
s.Require().NoError(c.Del(mockEventCTX, mockEventPfx, mockEventKey))
time.Sleep(time.Millisecond * 100)
val, err = s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{}}, val)
}
func (s *eventSuite) TestUnnormalEvent() {
c := s.factory.NewCache([]Setting{
{
Prefix: mockEventPfx,
CacheAttributes: map[Type]Attribute{
SharedCacheType: {time.Hour},
LocalCacheType: {10 * time.Second},
},
},
})
// Set() will trigger eviction in other machines
s.Require().NoError(c.Set(mockEventCTX, mockEventPfx, mockEventKey, 100))
time.Sleep(time.Millisecond * 100)
val, err := s.lfu.MGet(mockEventCTX, []string{getCacheKey(mockEventPfx, mockEventKey)})
s.Require().NoError(err)
s.Require().Equal([]Value{{Valid: true, Bytes: []byte("100")}}, val) // make sure the local value existed without impacted
// nothing happened due to no handling on such event
s.Require().NoError(s.rds.Pub(mockEventCTX, "not-existed", nil))
// TODO: handle this error in the future
// invalid json format.
s.Require().NoError(s.rds.Pub(mockEventCTX, EventTypeEvict.Topic(), []byte("")))
}
// not stable sometimes, skip it now
// func (s *eventSuite) TestListenNoEvents() {
// //s.T().Skip("not stable sometimes, skip it now")
// rds := NewRedis(s.ring).(*rds)
// mb := newMessageBroker(mockEventUUID, rds)
// s.Require().Equal(errNoEventType, mb.listen(mockEventCTX, []eventType{}, func(ctx context.Context, e *event, err error) {}))
// mb.close()
// }