-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbloom_test.go
154 lines (140 loc) · 4.97 KB
/
bloom_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
package golayeredcache
import (
"context"
"os"
"testing"
"time"
"github.com/agiledragon/gomonkey/v2"
"github.com/begonia-org/go-layered-cache/gobloom"
"github.com/begonia-org/go-layered-cache/source"
"github.com/begonia-org/go-layered-cache/utils"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
c "github.com/smartystreets/goconvey/convey"
)
func mockBloomRedis() *gomonkey.Patches {
patches := gomonkey.ApplyFunc((*redis.Client).Process, func(cli *redis.Client, ctx context.Context, cmd redis.Cmder) error {
return nil
})
iterSeq := []gomonkey.OutputCell{
{Values: gomonkey.Params{true}}, // 第一次调用返回
{Values: gomonkey.Params{false}}, // 第二次调用返回
}
patches.ApplyFuncSeq((*redis.ScanIterator).Next, iterSeq)
patches.ApplyFuncReturn((*redis.Client).Scan, &redis.ScanCmd{})
patches.ApplyFuncReturn((*redis.ScanCmd).Iterator, &redis.ScanIterator{})
patches.ApplyFuncReturn((*redis.ScanIterator).Val, "bf:cache:test")
headers, err := os.ReadFile("testdata/header.bin")
if err != nil {
panic(err)
}
hDump := &redis.ScanDumpCmd{}
hDump.SetVal(redis.ScanDump{Data: utils.BytesToString(headers), Iter: 1})
bdata, err := os.ReadFile("testdata/bloom.bin")
if err != nil {
panic(err)
}
dDump := &redis.ScanDumpCmd{}
dDump.SetVal(redis.ScanDump{Data: utils.BytesToString(bdata), Iter: 1385})
outSeq := []gomonkey.OutputCell{
{Values: gomonkey.Params{hDump}}, // 第一次调用返回
{Values: gomonkey.Params{dDump}}, // 第二次调用返回
{Values: gomonkey.Params{&redis.ScanDumpCmd{}}}, // 第二次调用返回
}
outSeq2 := []gomonkey.OutputCell{
{Values: gomonkey.Params{redis.ScanDump{Data: utils.BytesToString(headers), Iter: 1}, nil}}, // 第一次调用返回
{Values: gomonkey.Params{redis.ScanDump{Data: utils.BytesToString(bdata), Iter: 1385}, nil}}, // 第二次调用返回
{Values: gomonkey.Params{redis.ScanDump{Data: "", Iter: 0}, nil}}, // 第二次调用返回
}
patches.ApplyFuncSeq((*redis.Client).BFScanDump, outSeq)
patches.ApplyFuncSeq((*redis.ScanDumpCmd).Result, outSeq2)
patches.ApplyFuncReturn((*redis.Pipeline).Exec, nil, nil)
return patches
}
func TestBloomLayeredLoad(t *testing.T) {
c.Convey("TestLayeredLoad", t, func() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:16379",
DB: 0,
})
watcher := source.WatchOptions{
Block: 3 * time.Second,
BatchSize: 10,
Channels: []interface{}{"bf:test"},
WatcheAt: "$",
}
patches := mockBloomRedis()
defer patches.Reset()
options := &LayeredBuildOptions{
RDB: rdb,
Watcher: &watcher,
KeyPrefix: "bf:cache",
Channel: "bf:test",
Strategy: LocalOnly,
Log: logrus.New(),
}
layered := NewLayeredBloom(options, gobloom.BloomBuildOptions{
Entries: 1000,
Errors: 0.01,
BloomOptions: gobloom.BLOOM_OPT_NOROUND | gobloom.BLOOM_OPT_FORCE64 | gobloom.BLOOM_OPT_NO_SCALING,
Growth: 2,
})
err := layered.LoadDump(context.Background())
c.So(err, c.ShouldBeNil)
time.Sleep(1 * time.Second)
values, err := layered.Check(context.Background(), "bf:cache:test", []byte("item1"))
c.So(err, c.ShouldBeNil)
c.So(values, c.ShouldEqual, true)
err = layered.Add(context.Background(), "bf:cache:test", []byte("item2"))
c.So(err, c.ShouldBeNil)
// source:=NewBloomSourceImpl(, "test", nil)
})
}
func TestBloomWatch(t *testing.T) {
c.Convey("TestWatch", t, func() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:16379",
DB: 0,
})
watcher := source.WatchOptions{
Block: 3 * time.Second,
BatchSize: 1,
Channels: []interface{}{"bf:test"},
WatcheAt: "$",
}
patches := mockRedis()
defer patches.Reset()
options := &LayeredBuildOptions{
RDB: rdb,
Watcher: &watcher,
KeyPrefix: "bf:cache",
Channel: "bf:test",
Strategy: LocalOnly,
Log: logrus.New(),
}
defaultBuildBloomOptions := gobloom.BloomBuildOptions{
Entries: 1000,
Errors: 0.01,
BloomOptions: gobloom.BLOOM_OPT_NOROUND | gobloom.BLOOM_OPT_FORCE64 | gobloom.BLOOM_OPT_NO_SCALING,
Growth: 2,
}
layered1 := NewLayeredBloom(options, defaultBuildBloomOptions)
layered1.Watch(context.Background())
layered2 := NewLayeredBloom(options, defaultBuildBloomOptions)
layered2.Watch(context.Background())
time.Sleep(1 * time.Second)
ctx := context.Background()
pathch1 := gomonkey.ApplyFunc((*redis.XStreamSliceCmd).Result, func(_ *redis.XStreamSliceCmd) ([]redis.XStream, error) {
return []redis.XStream{{Stream: "bf:test",
Messages: []redis.XMessage{{ID: "1-0",
Values: map[string]interface{}{"value": "test0103102", "key": "bf:cache:test01"}}}}}, nil
})
err := layered1.Add(ctx, "bf:cache:test01", []byte("test0103102"))
c.So(err, c.ShouldBeNil)
time.Sleep(3 * time.Second)
vals, err := layered2.Check(context.Background(), "bf:cache:test01", []byte("test0103102"))
pathch1.Reset()
c.So(err, c.ShouldBeNil)
c.So(vals, c.ShouldEqual, true)
})
}